为什么用JAVA的算法-----------------我的16进制转字符不成功?

雄牛 2017-12-20 08:57:00

16进制的字符串从设备上读出来的,是12位长度的String数据,用C#的代码可以转成字符串,

编号:BG0100060003
对应16进制字符串: 42 47 30 31 30 30 30 36 30 30 30 33


以下是C#代码(可成功转换):
//字符串转16进制
public static string StrToHex(string mStr) //返回处理后的十六进制字符串
{
return BitConverter.ToString(
ASCIIEncoding.Default.GetBytes(mStr)).Replace("-", " ");
}

//16进制转字符串
public static string HexToStr(string mHex) // 返回十六进制代表的字符串
{
mHex = mHex.Replace(" ", "");
if (mHex.Length <= 0) return "";
byte[] vBytes = new byte[mHex.Length / 2];
for (int i = 0; i < mHex.Length; i += 2)
if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, null, out vBytes[i / 2]))
vBytes[i / 2] = 0;
return ASCIIEncoding.Default.GetString(vBytes);

}


可是用以下的JAVA的话,出来却是乱的码,什么原因呢?


/**
* 字符串转换成为16进制(无需Unicode编码)
*/
public static String StrToHexStr(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;
}


/**
* 16进制直接转换成为字符串(无需Unicode解码)
*/
public static String HexStrToStr(String s) {
if (s == null || s.equals("")) {
return null;
}
s = s.replace(" ", "");
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");
new String();
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}

...全文
153 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
什么都不能 2017-12-22
  • 打赏
  • 举报
回复

public class Test {
	public static String toHex(String txt) {
		return toHex(txt.getBytes());
	}

	public static String fromHex(String hex) {
		return new String(toByte(hex));
	}

	public static byte[] toByte(String hexString) {
		int len = hexString.length() / 2;
		byte[] result = new byte[len];
		for (int i = 0; i < len; i++)
			result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
					16).byteValue();
		return result;
	}

	public static String toHex(byte[] buf) {
		if (buf == null)
			return "";
		StringBuffer result = new StringBuffer(2 * buf.length);
		for (int i = 0; i < buf.length; i++) {
			appendHex(result, buf[i]);
		}
		return result.toString();
	}

	private final static String HEX = "0123456789ABCDEF";

	private static void appendHex(StringBuffer sb, byte b) {
		sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "424730313030303630303033";
		String hex = "BG0100060003";
		str = fromHex(str);
		System.out.println(str);
		System.out.println(toHex(hex));
	}

}

强长黑粗硬 2017-12-22
  • 打赏
  • 举报
回复
楼上正解,结贴吧。
tianfang 2017-12-20
  • 打赏
  • 举报
回复
StrToHexStr 中出问题了,s.length()是字符个数,一个汉字可能是2-3个byte,算长度时候是1 要先将string转成byte数组,再转16进制

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧