Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
const char* hexstring = "deadbeef";
size_t length = sizeof(hexstring);
unsigned char bytearray[length / 2];
for (size_t i = 0, j = 0; i < (length / 2); i++, j += 2)
bytearray[i] = (hexstring[j] % 32 + 9) % 25 * 16 + (hexstring[j+1] % 32 + 9) % 25;
byte[] a = new byte[s.Length/2];
for (int i = 0, h = 0; h < s.Length; i++, h += 2)
{
a[i] = (byte) Int32.Parse(s.Substring(h, 2), System.Globalization.NumberStyles.HexNumber);
}
s
.split('')
.map((el, ix, arr) => ix % 2 ? null : el + arr[ix + 1])
.filter(el => el !== null)
.map(x => parseInt(x, 16))
public static byte[] hexToByteArray(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;
}
$a = array_map('hexdec', str_split($s, 2));
my @a = split '', pack 'H*', $s;
a = bytearray.fromhex(s)
a = [s].pack("H*").unpack("C*")