java中16进制转字符串的问题

为什么我把16进制的DE 03 03 FF 00转成字符串之后,再转回来 就变成3f33f了 不是DE 03 03 FF 00
public class Math01 {

/**
* @throws UnsupportedEncodingException
*/
public static void main(String [] args) throws UnsupportedEncodingException {
// log.log(str2hexstr("0"));
String s = "DE 03 03 FF 20 20";
log.log(gb2312hex2str(s.replace(" ", ""))+"16进制字符串转换字符");/**----------->为什么这个转换出来是乱码‘?? ’。*/
String c =gb2312hex2str(s.replace(" ", ""));
log.log(str2hexgb2312str(c)+"将字符串转换gb2312回来");//------------------>为什么转换回来结果不是DE 03 03 FF 20 20,而是3f33f20
// log.log("转换成GB2312编码:"+str2hexgb2312str(s));
// log.log("GB2312的16进制编码转换成GBK字符集的字符:"+gb2312hex2str(str2hexgb2312str(s).replace(" ", "")));

// log.log(gethex(s.getBytes()));
}
下面是2个方法

/**
* 将字符串转化为对应的GB2312B编码
*/
public static String str2hexgb2312str(String s){
String str="";
try {
byte [] tmp = s.getBytes("gb2312");
for(byte a : tmp){
str += Integer.toHexString(a&0xff);//可以添加大小写,可以添加" "显示格式
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}还有1个方法贴不上了 字符限制

你代码有问题,看我的。、

package com.weixin.util;

import java.io.ByteArrayOutputStream;

public class ShiLiuJinZhi {


/** 
* @date:2015年5月14日 上午10:36:02
* @Description:转化字符串为十六进制编码
* @param s
* @return 
*/ 
public static String toHexString(String s) {
   String str = "";
   for (int i = 0; i < s.length(); i++) {
    int ch = (int) s.charAt(i);
    String s4 = Integer.toHexString(ch);
    str = str + s4;
   }
   return str;
}

/** 
* @date:2015年5月14日 上午10:35:53
* @Description:转化十六进制编码为字符串
* @param s
* @return 
*/ 
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 & Integer.parseInt(s.substring(
       i * 2, i * 2 + 2), 16));
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   try {
    s = new String(baKeyword, "utf-8");// UTF-16le:Not
   } catch (Exception e1) {
    e1.printStackTrace();
   }
   return s;
}
// 转化十六进制编码为字符串
public static String toStringHex2(String s) {
   byte[] baKeyword = new byte[s.length() / 2];
   for (int i = 0; i < baKeyword.length; i++) {
    try {
     baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
       i * 2, i * 2 + 2), 16));
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   try {
    s = new String(baKeyword, "utf-8");// UTF-16le:Not
   } catch (Exception e1) {
    e1.printStackTrace();
   }
   return s;
}
public static void main(String[] args) {
   System.out.println(encode("aaa"));
   System.out.println(decode(encode("aaa")));
}
/*
* 16进制数字字符集
*/
private static String hexString = "0123456789ABCDEF";
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(String str) {
   // 根据默认编码获取字节数组
   byte[] bytes = str.getBytes();
   StringBuilder sb = new StringBuilder(bytes.length * 2);
   // 将字节数组中每个字节拆解成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());
}

// 第二种方法:
// 将指定byte数组以16进制的形式打印到控制台
// 复制代码 代码如下:
/*public class Util {
   public Util() {
   }
   *//**
   * 将指定byte数组以16进制的形式打印到控制台
   * @param hint String
   * @param b byte[]
   * @return void
   *//*
   public static void printHexString(String hint, byte[] b) {
    System.out.print(hint);
    for (int i = 0; i < b.length; i++) {
     String hex = Integer.toHexString(b[i] & 0xFF);
     if (hex.length() == 1) {
      hex = '0' + hex;
     }
     System.out.print(hex.toUpperCase() + " ");
    }
    System.out.println("");
   }
   *//**
   * @param b byte[]
   * @return String
   *//*
   public static String Bytes2HexString(byte[] b) {
    String ret = "";
    for (int i = 0; i < b.length; i++) {
     String hex = Integer.toHexString(b[i] & 0xFF);
     if (hex.length() == 1) {
      hex = '0' + hex;
     }
     ret += hex.toUpperCase();
    }
    return ret;
   }
   *//**
   * 将两个ASCII字符合成一个字节; 如:"EF"--> 0xEF
   * @param src0 byte
   * @param src1 byte
   * @return byte
   *//*
   public static byte uniteBytes(byte src0, byte src1) {
    byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
      .byteValue();
    _b0 = (byte) (_b0 << 4);
    byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
      .byteValue();
    byte ret = (byte) (_b0 ^ _b1);
    return ret;
   }
   *//**
   * 将指定字符串src,以每两个字符分割转换为16进制形式 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9}
   * @param src String
   * @return byte[]
   *//*
   public static byte[] HexString2Bytes(String src) {
    byte[] ret = new byte[8];
    byte[] tmp = src.getBytes();
    for (int i = 0; i < 8; i++) {
     ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
    }
    return ret;
   }
}*/
}

追问

不行额 你这个转成16进制码 占位三个,我需要的是占位2个的GBK码

追答

你把utf-8改成gbk不就行了么。

温馨提示:答案为网友推荐,仅供参考
相似回答