java中 hex 转换成字符串 怎么转

作者&投稿:承穆 (若有异议请与网页底部的电邮联系)
java中 hex 转换成字符串 怎么转~

java中 hex 转换成字符串方法:
public class Test {

private static String hexString = "0123456789ABCDEFabcdef";
public static void main(String[] args) {
String msg= "亲,你好";
System.out.println(encode(msg));
System.out.println(decode(encode(msg)));
}

public static String encode(String str) {
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
//转换hex编码
for (byte b : bytes) {
sb.append(Integer.toHexString(b + 0x800).substring(1));
}
str = sb.toString();
return str;
}
//把hex编码转换为string
public static String decode(String bytes) {
bytes = bytes.toUpperCase();
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
}

public class Test {
private static String hexString = "0123456789ABCDEFabcdef";
public static void main(String[] args) {
String msg= "亲,你好";
System.out.println(encode(msg));
System.out.println(decode(encode(msg)));
}

public static String encode(String str) {
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
//转换hex编码
for (byte b : bytes) {
sb.append(Integer.toHexString(b + 0x800).substring(1));
}
str = sb.toString();
return str;
}
//把hex编码转换为string
public static String decode(String bytes) {
bytes = bytes.toUpperCase();
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
}

public class Test {
    private static String hexString = "0123456789ABCDEFabcdef";
    public static void main(String[] args) {
        String msg= "亲,你好";
        System.out.println(encode(msg));
        System.out.println(decode(encode(msg)));
    }

    public static String encode(String str) {
        byte[] bytes = str.getBytes();
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        //转换hex编码
        for (byte b : bytes) {
            sb.append(Integer.toHexString(b + 0x800).substring(1));
        }
        str = sb.toString();
        return str;
    }
    //把hex编码转换为string
    public static String decode(String bytes) {
        bytes = bytes.toUpperCase();
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
        // 将每2位16进制整数组装成一个字节
        for (int i = 0; i < bytes.length(); i += 2)
            baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
        return new String(baos.toByteArray());
    }
}


private static String hexString = "0123456789ABCDEF";
public static void main(String[] args) {
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(String str) {
// 根据默认编码获取<a href="https://www.baidu.com/s?wd=%E5%AD%97%E8%8A%82%E6%95%B0%E7%BB%84&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydn1D4nWDvuWN9mvRvnWDv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHT4rjR3n101njc4PHnLrHTYPs" target="_blank" class="baidu-highlight">字节数组</a>
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// 将<a href="https://www.baidu.com/s?wd=%E5%AD%97%E8%8A%82%E6%95%B0%E7%BB%84&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydn1D4nWDvuWN9mvRvnWDv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHT4rjR3n101njc4PHnLrHTYPs" target="_blank" class="baidu-highlight">字节数组</a>中每个字节拆解成2位16进制整数
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
}

/*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static String decode(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}

将hex转换为byte,然后再将byte转换为字符串,就可以了


茅箭区18551533615: java中 hex 转换成字符串 怎么转 -
贾府鼻炎: java中 hex 转换成字符串方法: public class Test {private static String hexString = "0123456789ABCDEFabcdef"; public static void main(String[] args) { String msg= "亲,你好"; System.out.println(encode(msg)); System.out.println(decode(...

茅箭区18551533615: java中hex转换成字符串怎么转 -
贾府鼻炎: public class Test { private static String hexString = "0123456789ABCDEFabcdef"; public static void main(String[] args) { String msg= "亲,你好"; System.out.println(encode(msg)); System.out.println(decode(encode(msg))); } public static ...

茅箭区18551533615: 请教JAVA怎么将十六进制转换为字符串,多谢 -
贾府鼻炎: private static String hexString = "0123456789ABCDEF"; public static void main(String[] args) {System.out.println(encode("中文"));System.out.println(decode(encode("中文"))); } /** 将字符串编码成16进制数字,适用于所有字符(包...

茅箭区18551533615: 如何将16进制数字转换成字符串 java -
贾府鼻炎: 使用这个方法可以传进去的16进制的数字组成的字符串转化为utf-8格式的字符串public static String toStringHex1(String s) { byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & ...

茅箭区18551533615: java获得字符utf - 8值并转成字符串 -
贾府鼻炎: String s="家"; byte[] b=s.getBytes("UTF-8"); for(int i=0;i System.out.println(Integer.toHexString(b[i])); }

茅箭区18551533615: 在java中如何将字符型数组转换到字符串中 -
贾府鼻炎: java可以使用两种方法直接将字符数组转为字符串方法1:直接在构造String时转换. char[] data = {'a', 'b', 'c'}; String str = new String(data);方法2:调用String类的方法转换. String.valueOf(char[] ch)

茅箭区18551533615: java 将十进制转换成十六进制字符串
贾府鼻炎: import java.awt.*; import java.awt.event.*; class te11 extends Frame implements ActionListener{ TextField t1,t2; Label l; Button b; static String s; te11(){ super("将十进制转换为十六进制"); t1=new TextField(10); t2=new TextField(10); t2....

茅箭区18551533615: JAVA 方法public String byte2hex(byte[] b) //二行制转字符串 作用 -
贾府鼻炎: 能使这个算式简便,public String byte2hex(byte[] b) //二行制转字符串 { String hs=""; String stmp=""; for (int n=0;n<b.length;n++)加到 else hs=hs+stmp; if (n<b.length-1) hs=hs+"%";里就行了

茅箭区18551533615: java中二进制的数字怎么转换成字符串 -
贾府鼻炎: public class Zhuang { public static void main(String[] args) { String str = "110"; int s=Integer.parseInt(str,2);//使用第二个参数指定的基数,将字符串参数解析为有符号的整数!System.out.println(s); } }

茅箭区18551533615: java怎么把数字转换成字符串 -
贾府鼻炎: 数字转换成字符串的方法: 1 直接用 "" + 124即可, 即直接用字符串相加的方式. 2 用String的API String.valueOf(1243.44);

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网