How can I convert a hex string to a byte array?
Here's a nice fun LINQ example.
public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
hexadecimal string to byte array in python
Suppose your hex string is something like
>>> hex_string = "deadbeef"
Convert it to a bytearray (Python 3 and 2.7):
>>> bytearray.fromhex(hex_string)
bytearray(b'\xde\xad\xbe\xef')
Convert it to a bytes object (Python 3):
>>> bytes.fromhex(hex_string)
b'\xde\xad\xbe\xef'
Note that bytes
is an immutable version of bytearray
.
Convert it to a string (Python ≤ 2.7):
>>> hex_data = hex_string.decode("hex")
>>> hex_data
"\xde\xad\xbe\xef"
Convert a string representation of a hex dump to a byte array using Java?
Update (2021) - Java 17 now includes java.util.HexFormat
(only took 25 years):
HexFormat.of().parseHex(s)
For older versions of Java:
Here's a solution that I think is better than any posted so far:
/* s must be an even-length string. */
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
Reasons why it is an improvement:
Safe with leading zeros (unlike BigInteger) and with negative byte values (unlike Byte.parseByte)
Doesn't convert the String into a
char[]
, or create StringBuilder and String objects for every single byte.No library dependencies that may not be available
Feel free to add argument checking via assert
or exceptions if the argument is not known to be safe.
Hex String To Byte Array C#
I believe you can use Convert.ToByte(), you might have to slice your string in pairs and loop through it.
If you do a quick search there are many topics on this already on stackoverflow
How do you convert Byte Array to Hexadecimal String, and vice versa?
You can also look at this MS example, it is to convert to int, but the idea is the same.
http://msdn.microsoft.com/en-us/library/bb311038.aspx
C# Convert Hex String Array to Byte Array
You have to convert (or parse) string
in order to get byte
since string
and byte
are different types:
// 10 == 10d
byte b = Convert.ToByte("10"); // if "10" is a decimal representation
// 16 == 0x10
byte b = Convert.ToByte("10", 16); // if "10" is a hexadecimal representation
If you want to process an array, you can try a simple Linq:
using System.Linq;
...
string[] hexValues = new string[] {
"10", "0F", "3E", "42"};
byte[] result = hexValues
.Select(value => Convert.ToByte(value, 16))
.ToArray();
If you want to print out result
as hexadecimal, use formatting ("X2"
format string - at least 2
hexadecimal digits, use captital letters):
// 10, 0F, 3E, 42
Console.Write(string.Join(", ", result.Select(b => b.ToString("X2"))));
Compare with same array but in a different format ("d2"
- at least 2
decimal digits)
// 16, 15, 62, 66
Console.Write(string.Join(", ", result.Select(b => b.ToString("d2"))));
If no format provided, .Net uses default one and represents byte
in decimal:
// 16, 15, 62, 66
Console.Write(string.Join(", ", result));
Converting a hex string to a byte array
This ought to work:
int char2int(char input)
{
if(input >= '0' && input <= '9')
return input - '0';
if(input >= 'A' && input <= 'F')
return input - 'A' + 10;
if(input >= 'a' && input <= 'f')
return input - 'a' + 10;
throw std::invalid_argument("Invalid input string");
}
// This function assumes src to be a zero terminated sanitized string with
// an even number of [0-9a-f] characters, and target to be sufficiently large
void hex2bin(const char* src, char* target)
{
while(*src && src[1])
{
*(target++) = char2int(*src)*16 + char2int(src[1]);
src += 2;
}
}
Depending on your specific platform there's probably also a standard implementation though.
How to convert hex string into a bytes array, and a bytes array in the hex string?
Update: Scroll down for solution... Live Demo
The issue: you are using a lossy conversion to hex, which cannot be reversed.
var p = parseHexString(createHexString(rsa_privk[0]));
This will never be same as rsa_privk[0]
.
Because, createHexString()
only uses the last 2 bytes
from each array element.
Example:
rsa_privk[0] : [123676133, 198914513, 129998601, 245147334, 11918451, 206998232, 96766191, 75984899, 177840095, 106709334, 10180427, 208237547, 119814814, 127003446, 189062377, 84099480, 220452154, 250519075, 267883908, 115471915, 165124106, 238628722, 169382478, 42320122, 95982405, 80725759, 89608310, 85166267, 200925925, 254033325, 86971506, 191278317, 127411298, 180195794, 142776693, 188738169, 39016]
createHexString(rsa_privk[0]) : e5d109c673d8ef03df564beb9e36e9983a23842b0a724efa45ff76bbe5ad72ed62d2757968
parseHexString(createHexString(rsa_privk[0])) : [229, 209, 9, 198, 115, 216, 239, 3, 223, 86, 75, 235, 158, 54, 233, 152, 58, 35, 132, 43, 10, 114, 78, 250, 69, 255, 118, 187, 229, 173, 114, 237, 98, 210, 117, 121, 104]
Update : Working Solution...
The two functions... the hex always contains 8 byte blocks, each for each element in the array...
function parseHexString(str) {
var result = [];
while (str.length >= 8) {
result.push(parseInt(str.substring(0, 8), 16));
str = str.substring(8, str.length);
}
return result;
}
function createHexString(arr) {
var result = "";
var z;
for (var i = 0; i < arr.length; i++) {
var str = arr[i].toString(16);
z = 8 - str.length + 1;
str = Array(z).join("0") + str;
result += str;
}
return result;
}
Test code...
function test() {
a = [123676133, 198914513, 129998601, 245147334, 11918451, 206998232, 96766191, 75984899, 177840095, 106709334, 10180427, 208237547, 119814814, 127003446, 189062377, 84099480, 220452154, 250519075, 267883908, 115471915, 165124106, 238628722, 169382478, 42320122, 95982405, 80725759, 89608310, 85166267, 200925925, 254033325, 86971506, 191278317, 127411298, 180195794, 142776693, 188738169, 39016];
console.log("Input");
console.log(a);
b = createHexString(a);
console.log("Hex");
console.log(b);
c = parseHexString(b);
console.log("Output");
console.log(c);
if(checkIfEqual(a, c)) {
alert("Same");
}
}
function checkIfEqual(arr1, arr2) {
if (arr1.length != arr2.length) {
return false;
}
//sort them first, then join them and just compare the strings
return arr1.sort().join() == arr2.sort().join();
}
C# - Convert hex string to byte array of hex values
string hexIpAddress = "0A010248"; // 10.1.2.72 => "0A010248"
byte[] bytes = new byte[hexIpAddress.Length / 2];
for (int i = 0; i < hexIpAddress.Length; i += 2)
bytes[i/2] = Convert.ToByte(hexIpAddress.Substring(i, 2), 16);
This results in this array:
bytes = {0x0A, 0x01, 0x02, 0x48};
or represented as decimals:
bytes = {10, 1, 2, 72};
or as binaries 00001010
, 000000001
, 00000010
, 01001000
(binary literals are still not supported in C#6).
The values are the same, there is no representation in any base in byte
. You can only decide how the values should be represented when converting them to strings again:
foreach(byte b in bytes)
Console.WriteLine("{0} {0:X}", b);
results in
10 A
1 1
2 2
72 48
Hexadecimal string to byte array in C
As far as I know, there's no standard function to do so, but it's simple to achieve in the following manner:
#include <stdio.h>
int main(int argc, char **argv) {
const char hexstring[] = "DEadbeef10203040b00b1e50", *pos = hexstring;
unsigned char val[12];
/* WARNING: no sanitization or error-checking whatsoever */
for (size_t count = 0; count < sizeof val/sizeof *val; count++) {
sscanf(pos, "%2hhx", &val[count]);
pos += 2;
}
printf("0x");
for(size_t count = 0; count < sizeof val/sizeof *val; count++)
printf("%02x", val[count]);
printf("\n");
return 0;
}
Edit
As Al pointed out, in case of an odd number of hex digits in the string, you have to make sure you prefix it with a starting 0. For example, the string "f00f5"
will be evaluated as {0xf0, 0x0f, 0x05}
erroneously by the above example, instead of the proper {0x0f, 0x00, 0xf5}
.
Amended the example a little bit to address the comment from @MassimoCallegari
Convert hex string to byte []
Convert hex to byte and byte to hex.
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len/2];
for(int i = 0; i < len; i+=2){
data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
}
return data;
}
final protected static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String byteArrayToHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length*2];
int v;
for(int j=0; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j*2] = hexArray[v>>>4];
hexChars[j*2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
Related Topics
How to Call a C# Library from Native C++ (Using C++\Cli and Ijw)
What Are the Differences Between a Multidimensional Array and an Array of Arrays in C#
How to Limit the Amount of Concurrent Async I/O Operations
Combining Two Expressions (Expression≪Func≪T, Bool≫≫)
C# Datetime to "Yyyymmddhhmmss" Format
Why Is There a Default Instance of Every Form in Vb.Net But Not in C#
Memory Leak Using Streamreader and Xmlserializer
What Does Void Mean in C, C++, and C#
Why Are Mutable Structs "Evil"
How to Save Application Settings in a Windows Forms Application
Should 'Using' Directives Be Inside or Outside the Namespace
Typenamehandling Caution in Newtonsoft Json