字符串转16进制byte数组
时间: 2024-02-06 07:02:22 浏览: 83
字符串与字节数组之间的转换
5星 · 资源好评率100%
以下是两种字符串转16进制字节数组的方法:
1. Java方法:
```java
public byte[] hexStrToBytes(String hexStr){
int len = hexStr.length()/2;
byte[] bytes = new byte[len];
char[] chars = hexStr.toCharArray();
for(int i=0;i<len;i++){
bytes[i] = (toByte(chars[i])<<4) | toByte(chars[i*2]);
}
return bytes;
}
public byte toByte(char c){
byte b = "0123456789ABCDEF".indexOf(c);
return b;
}
```
2. C#方法:
```csharp
private byte[] StringToHexByteArray(string message, Encoding encode) {
byte[] bytes = encode.GetBytes(message);
byte[] result = new byte[bytes.Length];
for (int i = 0; i < bytes.Length; i++) {
result[i] = Convert.ToByte(Convert.ToString(bytes[i], 16));
}
return result;
}
```
阅读全文