Convert (Decode) Hexadecimal String to Binary String

Convert hexadecimal string (hex) to a binary string

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);
}

Convert hex string to binary string

You need to tell Java that the int is in hex, like this:

String hexToBinary(String hex) {
int i = Integer.parseInt(hex, 16);
String bin = Integer.toBinaryString(i);
return bin;
}

How to convert a hexadecimal string to a binary string in C


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

C# how convert large HEX string to binary

You can just convert each hexadecimal digit into four binary digits:

string binarystring = String.Join(String.Empty,
hexstring.Select(
c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')
)
);

You need a using System.Linq; a the top of the file for this to work.

How does flutter convert a hex string to a binary string?

You can specify the radix for int.parse and toRadixString

print(int.parse('AA',radix: 16).toRadixString(2));

Decoding a hex string into 4 pieces that maps to a binary map of values

Since 4 bytes fit into the storage for integer type on almost all platforms (32-bit and higher), you can convert the hex string to integer, then use the bitwise operators to check if specific bit is set:

$hex_str = '0001003F';
$flags = base_convert($hex_str, 16, 10);

foreach (range(0, 31) as $bit) {
printf("Bit %d: %d\n", $bit, (bool) ($flags & (1 << $bit)));
}

Output

Bit 0: 1
Bit 1: 1
Bit 2: 1
Bit 3: 1
Bit 4: 1
Bit 5: 1
Bit 6: 0
...
Bit 15: 0
Bit 16: 1
Bit 17: 0
...
Bit 31: 0

If bit $bit is set (1), then the state corresponding to this bit is on.

The code converts the hex string $hex_str to an integer $flags with the help of base_convert function. The loop iterates bit numbers in range [0;31] (starting from the least significant bit). The (1 << $bit) expression is the value 1 shifted to the left by $bit bits. Thus, if bit number $bit is set, then the result of the bitwise AND operation is a non-zero integer. The result is cast to boolean type to produce 1, if the result is non-zero, and 0 otherwise.

It is easy to see that you can test a number of bits with a single bitwise AND operation, e.g.:

// Check if at least one of three bits is set, 3rd, 10th, or 11th
$mask = (1 << 3) | (1 << 10) | (1 << 11);
if ($flags & $mask)
printf("At least one of the bits from mask 0x%x is set\n", $mask);

Output

At least one of the bits from mask 0xc08 is set


Related Topics



Leave a reply



Submit