public static string BytesToHex(byte[] bytes)
{
StringBuilder result = new StringBuilder();
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
result.Append(bytes[i].ToString("X2"));
}
}
return result.ToString();
}
这是C#转的部分串:504B03040A0000000000BD731A3F00000000000000000000000
org.apache.commons.codec.binary.Hex.decodeHex(mms.toCharArray());;
这是java转的部分串:504b0304140000000800e256273c8a28c3caf52200009a230000
现在我要怎么样才能把C#的16进制串 转成跟java的一样呢
3 个解决方案
#1
C#
java
/// <summary>
/// 16进制字符串转字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
private static byte[] Hex2Bytes(string hex)
{
byte[] result = new byte[hex.Length / 2];
for (int i = 0; i < result.Length; i++)
result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
return result;
}
/**//// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string BytesToHex(byte[] bytes)
{
StringBuilder result = new StringBuilder();
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
result.Append(bytes[i].ToString("X2"));
}
}
return result.ToString();
}
java
private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/** *//**
* 将字节数组转换为16进制字符串
* @param bcd
* @return
*/
public static final String bytesToHex(byte[] bcd) {
StringBuffer s = new StringBuffer(bcd.length * 2);
for (int i = 0; i < bcd.length; i++) {
s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
s.append(bcdLookup[bcd[i] & 0x0f]);
}
return s.toString();
}
/** *//**
* 将16进制字符串转换为字节数组
* @param s
* @return
*/
public static final byte[] hexToBytes(String s) {
byte[] bytes;
bytes = new byte[s.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16);
}
return bytes;
}
#2
wxr0323 首先感谢你的回帖 但是我现在不是要java C#字节数组转16进制字符串的方法 我是要把C#转的16进制字符串转成java能识别的16进制字符串
#3
您好楼主,您的这个问题解决了么,我也刚好遇到这个问题,能分享下您的成果么?我qq786158039,我会非常感谢您的
#1
C#
java
/// <summary>
/// 16进制字符串转字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
private static byte[] Hex2Bytes(string hex)
{
byte[] result = new byte[hex.Length / 2];
for (int i = 0; i < result.Length; i++)
result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
return result;
}
/**//// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string BytesToHex(byte[] bytes)
{
StringBuilder result = new StringBuilder();
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
result.Append(bytes[i].ToString("X2"));
}
}
return result.ToString();
}
java
private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/** *//**
* 将字节数组转换为16进制字符串
* @param bcd
* @return
*/
public static final String bytesToHex(byte[] bcd) {
StringBuffer s = new StringBuffer(bcd.length * 2);
for (int i = 0; i < bcd.length; i++) {
s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
s.append(bcdLookup[bcd[i] & 0x0f]);
}
return s.toString();
}
/** *//**
* 将16进制字符串转换为字节数组
* @param s
* @return
*/
public static final byte[] hexToBytes(String s) {
byte[] bytes;
bytes = new byte[s.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16);
}
return bytes;
}
#2
wxr0323 首先感谢你的回帖 但是我现在不是要java C#字节数组转16进制字符串的方法 我是要把C#转的16进制字符串转成java能识别的16进制字符串
#3
您好楼主,您的这个问题解决了么,我也刚好遇到这个问题,能分享下您的成果么?我qq786158039,我会非常感谢您的