rt
解决方案 »
- applet问题
- 多线程问题
- 求助 读取一组 checkbox类 ???
- 一个io输入输出流的问题[希望大家帮帮我吧]
- 怎样去掉字符串最后两个字符?
- 关于组建的属性
- 各位看看我这样的能力,能找到java程序员吗?
- 如何通过正则表达式将select t1.c1,t2.c2 from table1 t1 中select 与 from之间的字符串提取出来或换成其他字符串?
- sun j2sdkee1.3.1 启动不起来,WHY ?在线等待
- 四年java,分享零基础到工作的知识、技术
- 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进制字符串啊