I'm saving a bitarray
(40 bits) from Python (using bitarray lib) to Redis.
When I retrieve this value from Redis, I get: \xe8\x00\x00\x00\x00
How do I convert this value to "01010101" in C#?
Thank you!
EDIT:
When i use this form: http://easycalculation.com/hex-converter.php, the binary value returned is what i'm expecting.
You could do this:
// Chop up the string into individual hex values
string[] hexStrings = hexString.Split(new[] { "\\x" }, StringSplitOptions.RemoveEmptyEntries);
// Convert the individual hex strings into integers
int[] values = hexStrings.Select(s => Convert.ToInt32(s, 16)).ToArray();
// Convert the integers into 8-character binary strings
string[] binaryStrings = values.Select(v => Convert.ToString(v, 2).PadLeft(8, '0')).ToArray();
// Join the strings together
string binaryString = string.Join("", binaryStrings);
EDIT - Here's an example of what you could do if you want to use a BitArray
:
// Chop up the string into individual hex values
string[] hexStrings = hexString.Split(new[] { "\\x" }, StringSplitOptions.RemoveEmptyEntries);
// Convert the individual hex strings into bytes
byte[] bytes = hexStrings.Select(s => Convert.ToByte(s, 16)).ToArray();
BitArray bitArray = new BitArray(bytes);