I used the -r and -p switches for xxd:
$ echo '0006303030304e43' | xxd -r -p | nc -l localhost 8181
Thanks to inspiration from @Gilles' answer, here's a Perl version:
$ echo '0006303030304e43' | perl -e 'print pack "H*", <STDIN>' | nc -l localhost 8181
Answer from Chris Snow on Stack Exchangepython - Convert hex to binary - Stack Overflow
java - Convert hexadecimal string (hex) to a binary string - Stack Overflow
Help needed C, hex string to binary string.
How to convert a hexadecimal string to a binary string in C - Stack Overflow
I used the -r and -p switches for xxd:
$ echo '0006303030304e43' | xxd -r -p | nc -l localhost 8181
Thanks to inspiration from @Gilles' answer, here's a Perl version:
$ echo '0006303030304e43' | perl -e 'print pack "H*", <STDIN>' | nc -l localhost 8181
Here a solution without xxd or perl:
If the echo builtin of your shell supports it (bash and zsh do, but not dash), you just need to use the right backslash escapes:
echo -ne '\x00\x06\x30\x30\x30\x30\x4e\x43' | nc -l localhost 8181
If you have /bin/echo from GNU coreutils (nearly standard on Linux systems) or from busybox you can use it, too.
With sed you can generate a escaped pattern:
$ echo '0006303030304e43' | sed -e 's/../\\x&/g'
\x00\x06\x30\x30\x30\x30\x4e\x43
Combined:
echo -ne "$(echo '0006303030304e43' | sed -e 's/../\\x&/g')" | nc -l localhost 8181
For solving the left-side trailing zero problem:
my_hexdata = "1a"
scale = 16 ## equals to hexadecimal
num_of_bits = 8
bin(int(my_hexdata, scale))[2:].zfill(num_of_bits)
It will give 00011010 instead of the trimmed version.
Convert hex to binary
I have ABC123EFFF.
I want to have 001010101111000001001000111110111111111111 (i.e. binary repr. with, say, 42 digits and leading zeroes).
Short answer:
The new f-strings in Python 3.6 allow you to do this using very terse syntax:
>>> f'{0xABC123EFFF:0>42b}'
'001010101111000001001000111110111111111111'
or to break that up with the semantics:
>>> number, pad, rjust, size, kind = 0xABC123EFFF, '0', '>', 42, 'b'
>>> f'{number:{pad}{rjust}{size}{kind}}'
'001010101111000001001000111110111111111111'
Long answer:
What you are actually saying is that you have a value in a hexadecimal representation, and you want to represent an equivalent value in binary.
The value of equivalence is an integer. But you may begin with a string, and to view in binary, you must end with a string.
Convert hex to binary, 42 digits and leading zeros?
We have several direct ways to accomplish this goal, without hacks using slices.
First, before we can do any binary manipulation at all, convert to int (I presume this is in a string format, not as a literal):
>>> integer = int('ABC123EFFF', 16)
>>> integer
737679765503
alternatively we could use an integer literal as expressed in hexadecimal form:
>>> integer = 0xABC123EFFF
>>> integer
737679765503
Now we need to express our integer in a binary representation.
Use the builtin function, format
Then pass to format:
>>> format(integer, '0>42b')
'001010101111000001001000111110111111111111'
This uses the formatting specification's mini-language.
To break that down, here's the grammar form of it:
[[fill]align][sign][#][0][width][,][.precision][type]
To make that into a specification for our needs, we just exclude the things we don't need:
>>> spec = '{fill}{align}{width}{type}'.format(fill='0', align='>', width=42, type='b')
>>> spec
'0>42b'
and just pass that to format
>>> bin_representation = format(integer, spec)
>>> bin_representation
'001010101111000001001000111110111111111111'
>>> print(bin_representation)
001010101111000001001000111110111111111111
String Formatting (Templating) with str.format
We can use that in a string using str.format method:
>>> 'here is the binary form: {0:{spec}}'.format(integer, spec=spec)
'here is the binary form: 001010101111000001001000111110111111111111'
Or just put the spec directly in the original string:
>>> 'here is the binary form: {0:0>42b}'.format(integer)
'here is the binary form: 001010101111000001001000111110111111111111'
String Formatting with the new f-strings
Let's demonstrate the new f-strings. They use the same mini-language formatting rules:
>>> integer = 0xABC123EFFF
>>> length = 42
>>> f'{integer:0>{length}b}'
'001010101111000001001000111110111111111111'
Now let's put this functionality into a function to encourage reusability:
def bin_format(integer, length):
return f'{integer:0>{length}b}'
And now:
>>> bin_format(0xABC123EFFF, 42)
'001010101111000001001000111110111111111111'
Aside
If you actually just wanted to encode the data as a string of bytes in memory or on disk, you can use the int.to_bytes method, which is only available in Python 3:
>>> help(int.to_bytes)
to_bytes(...)
int.to_bytes(length, byteorder, *, signed=False) -> bytes
...
And since 42 bits divided by 8 bits per byte equals 6 bytes:
>>> integer.to_bytes(6, 'big')
b'\x00\xab\xc1#\xef\xff'
BigInteger.toString(radix) will do what you want. Just pass in a radix of 2.
static String hexToBin(String s) {
return new BigInteger(s, 16).toString(2);
}
Fast, and works for large strings:
private String hexToBin(String hex){
hex = hex.replaceAll("0", "0000");
hex = hex.replaceAll("1", "0001");
hex = hex.replaceAll("2", "0010");
hex = hex.replaceAll("3", "0011");
hex = hex.replaceAll("4", "0100");
hex = hex.replaceAll("5", "0101");
hex = hex.replaceAll("6", "0110");
hex = hex.replaceAll("7", "0111");
hex = hex.replaceAll("8", "1000");
hex = hex.replaceAll("9", "1001");
hex = hex.replaceAll("A", "1010");
hex = hex.replaceAll("B", "1011");
hex = hex.replaceAll("C", "1100");
hex = hex.replaceAll("D", "1101");
hex = hex.replaceAll("E", "1110");
hex = hex.replaceAll("F", "1111");
return hex;
}
Learning C currrently could use a little help. I'm still new to programming so the simpler the better.
So I'm having a lot of difficulty wrapping my head around this problem.
I'm trying to write a function that will convert a hexadecimal string (0123456789abcdef) into binary (0000 0001 .......).
Been stuck for several hours trying to figure out a solution. Any help will be greatly appreciated.
It's quite easy, really, because the translation goes digit-by-digit.
0 - 0000
1 - 0001
2 - 0010
3 - 0011
4 - 0100
5 - 0101
6 - 0110
7 - 0111
8 - 1000
9 - 1001
A - 1010
B - 1011
C - 1100
D - 1101
E - 1110
F - 1111
So, for example, the hex number FE2F8 will be 11111110001011111000 in binary
const char input[] = "..."; // the value to be converted
char res[9]; // the length of the output string has to be n+1 where n is the number of binary digits to show, in this case 8
res[8] = '\0';
int t = 128; // set this to s^(n-1) where n is the number of binary digits to show, in this case 8
int v = strtol(input, 0, 16); // convert the hex value to a number
while(t) // loop till we're done
{
strcat(res, t < v ? "1" : "0");
if(t < v)
v -= t;
t /= 2;
}
// res now contains the binary representation of the number
As an alternative (this assumes there's no prefix like in "0x3A"):
const char binary[16][5] = {"0000", "0001", "0010", "0011", "0100", ...};
const char digits = "0123456789abcdef";
const char input[] = "..." // input value
char res[1024];
res[0] = '\0';
int p = 0;
while(input[p])
{
const char *v = strchr(digits, tolower(input[p++]));
if (v)
strcat(res, binary[v - digits]);
}
// res now contains the binary representation of the number