当前位置:编程学习 > wap >>

java 怎么把一个byte数组转化为想要的字符串

譬如一个数组byte[] bytearrary ={0x51,0x68,0x90,0xE8},
unicode编码是“全部”

怎么变为一个字符串,使它调用System.out.println让控制台显示“全部”

给位大侠,请多多赐教啦! java --------------------编程问答--------------------  new String(bytearrary)
另外0xE8>127了,不合法 --------------------编程问答-------------------- LS正解   如果结果不正确的话  还需要加上charset 
即  new String(byte[],charset) --------------------编程问答--------------------

package com.luxtone.test;


public class ByteTest {
public static void main(String[] args) {
byte[] bytearrary = { 0x51, 0x68};
System.out.println(deUnicode(byte2hex(bytearrary)));
}

/**
     * java字节码转字符串
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) { //一个字节的数,
        // 转成16进制字符串
        String hs = "";
        String tmp = "";
        for (int n = 0; n < b.length; n++) {
            //整数转成十六进制表示
            tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (tmp.length() == 1) {
                hs = hs + "0" + tmp;
            } else {
                hs = hs + tmp;
            }
        }
        tmp = null;
        return hs.toUpperCase(); //转成大写
    }

public static String deUnicode(String content) {// 将16进制数转换为汉字
String enUnicode = null;
String deUnicode = null;
for (int i = 0; i < content.length(); i++) {
if (enUnicode == null) {
enUnicode = String.valueOf(content.charAt(i));
} else {
enUnicode = enUnicode + content.charAt(i);
}
if (i % 4 == 3) {
if (enUnicode != null) {
if (deUnicode == null) {
deUnicode = String.valueOf((char) Integer.valueOf(
enUnicode, 16).intValue());
} else {
deUnicode = deUnicode
+ String.valueOf((char) Integer.valueOf(
enUnicode, 16).intValue());
}
}
enUnicode = null;
}

}
return deUnicode;
}
}


你的后两个byte值有问题,写的不对
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,