String to ascii rt 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 LZ说的是中文乱码吗?String newStr = new String(oldStr.getBytes["ISO8859-1"],"gb2312"); 有時會需要進行 ASCII 編碼與文字的互轉,小弟用C++寫了兩個小 Function 以方便進行 String 與 ASCII 的相互轉換,不過轉成的 ASCII Code 是16進位的,若有需要請自行修改//------------ String to HexAscii -----------------////Example: ABCD ==> 41424344void StringToHexAscii(char *in, char *out){ char *buf = new char(sizeof(char)*2); string str; for ( int i = 0; i < sizeof(&in); i++ ) { char c = in[i]; sprintf(buf,"%X",c); //Dec => Hex str.append(buf); } strcpy (out, str.c_str());}//------------ HexAscii to String-----------------////Example: 41424344 ==> ABCDvoid HexAsciiToString(char *in, char *out){ char *buf = new char(sizeof(char)*2); string str; for ( int i = 0; i < sizeof(&in)*2; i+=2 ) { char c[2]; c[0] = in[i]; c[1] = in[i+1]; int nub = atoi(c); nub = (nub/10)*16 + nub%10; //Dec => Hex char cc; sprintf(&cc,"%c",nub); //int => char str.append(&cc); } strcpy (out, str.c_str());}int main(){ char *cstr = "ABCD"; char cHexOut[8]; char cStrOut[4]; StringToHexAscii(cstr, cHexOut); cout << "ABCD to Hex is: " << cHexOut << endl; HexAsciiToString(cHexOut, cStrOut); cout << "Hex 41424344 to String is: " << cStrOut << endl; system("pause"); return 0;} 晕,COPY也用这么不爽直接给你网址吧http://luhychen.blogspot.com/2008/04/stringascii.html String newStr = new String(oldStr.getBytes["你本身的编码"],"gb2312"); public class Convert { public static String hexToString(String target) { char[] ch = target.toCharArray(); StringBuffer buffer = new StringBuffer(); for (int index = 0; index < ch.length; index++) { int ascii = ch[index]; buffer.append(Integer.toHexString(ascii)); } return buffer.toString(); } public static void main(String args[]) { System.out.println(Convert.hexToString("ABCD")); }} public static void main(String[] args) { // TODO Auto-generated method stub String str="abcde"; byte[] b = str.getBytes(); for(byte temp:b){ System.out.println(temp); } } public static void main(String[] args) { // TODO Auto-generated method stub String str="abcde"; char[] c = str.toCharArray(); for(char temp:c){ System.out.println((int)temp); } } public class IntToBinary { public static void main(String[] args) { int decimalNum = 123; // 用Integer类的String toBinaryString(int i)方法实现转换 String binaryStr = Integer.toBinaryString(decimalNum); System.out.println(binaryStr); } } public class Convert { public static int[] hexToAscii(String target) { char[] ch = target.toCharArray(); int[] ret = new int[ch.length]; for (int i = 0; i < ch.length; i++) { ret[i] = ch[i]; } return ret; }}昏倒。居然看错了,原来不是把他转成16进制字符串啊 char型实际上是按照ascii码进行存贮的。只要对他进行类型转换或者直接赋值char a = '1';int i = a;就可以得到其ascii 关于java导出包的问题?! static关键字问题 java线程池 有点迷惑 求教高手指点关于异常的问题 jms的接收问题 java日期计算问题(如何跳过周六周日) 从文件中读对象时抛出的异常:java.io.StreamCorruptedException: unexpected block data 请问为何运行窗口后不能看到组件? 分数太多 log4j输出异常到一个文件求教 关于strSQL="select top ? * from table1"; 的错误问题
String newStr = new String(oldStr.getBytes["ISO8859-1"],"gb2312");
有時會需要進行 ASCII 編碼與文字的互轉,小弟用C++寫了兩個小 Function 以方便進行 String 與 ASCII 的相互轉換,不過轉成的 ASCII Code 是16進位的,若有需要請自行修改//------------ String to HexAscii -----------------////Example: ABCD ==> 41424344void StringToHexAscii(char *in, char *out){ char *buf = new char(sizeof(char)*2); string str; for ( int i = 0; i < sizeof(&in); i++ ) { char c = in[i]; sprintf(buf,"%X",c); //Dec => Hex str.append(buf); } strcpy (out, str.c_str());}//------------ HexAscii to String-----------------////Example: 41424344 ==> ABCDvoid HexAsciiToString(char *in, char *out){ char *buf = new char(sizeof(char)*2); string str; for ( int i = 0; i < sizeof(&in)*2; i+=2 ) { char c[2]; c[0] = in[i]; c[1] = in[i+1]; int nub = atoi(c); nub = (nub/10)*16 + nub%10; //Dec => Hex char cc; sprintf(&cc,"%c",nub); //int => char str.append(&cc); } strcpy (out, str.c_str());}int main(){ char *cstr = "ABCD"; char cHexOut[8]; char cStrOut[4]; StringToHexAscii(cstr, cHexOut); cout << "ABCD to Hex is: " << cHexOut << endl; HexAsciiToString(cHexOut, cStrOut); cout << "Hex 41424344 to String is: " << cStrOut << endl; system("pause"); return 0;}
直接给你网址吧
http://luhychen.blogspot.com/2008/04/stringascii.html
public static String hexToString(String target) {
char[] ch = target.toCharArray();
StringBuffer buffer = new StringBuffer();
for (int index = 0; index < ch.length; index++) {
int ascii = ch[index]; buffer.append(Integer.toHexString(ascii));
}
return buffer.toString();
}
public static void main(String args[]) {
System.out.println(Convert.hexToString("ABCD"));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="abcde";
byte[] b = str.getBytes();
for(byte temp:b){
System.out.println(temp);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="abcde";
char[] c = str.toCharArray();
for(char temp:c){
System.out.println((int)temp);
}
}
public static void main(String[] args) {
int decimalNum = 123;
// 用Integer类的String toBinaryString(int i)方法实现转换
String binaryStr = Integer.toBinaryString(decimalNum);
System.out.println(binaryStr);
}
}
public static int[] hexToAscii(String target) {
char[] ch = target.toCharArray();
int[] ret = new int[ch.length];
for (int i = 0; i < ch.length; i++) {
ret[i] = ch[i];
}
return ret;
}}
昏倒。居然看错了,原来不是把他转成16进制字符串啊