rt

解决方案 »

  1.   

    LZ说的是中文乱码吗?
    String newStr = new String(oldStr.getBytes["ISO8859-1"],"gb2312");
      

  2.   


    有時會需要進行 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;} 
      

  3.   

    晕,COPY也用这么不爽
    直接给你网址吧
    http://luhychen.blogspot.com/2008/04/stringascii.html
      

  4.   

    String newStr = new String(oldStr.getBytes["你本身的编码"],"gb2312");
      

  5.   

    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"));
    }
    }
      

  6.   


    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);
     }
    }
      

  7.   


    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);
     }
    }
      

  8.   

    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); 


      

  9.   

    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进制字符串啊
      

  10.   

    char型实际上是按照ascii码进行存贮的。只要对他进行类型转换或者直接赋值char a = '1';int i = a;就可以得到其ascii