I'm trying to convert the hexadecimal representation of a 64-bit number (e.g., the string "FFFFFFFFF"
) to binary representation ("11111..."
).
I've tried
string result = Convert.ToString(Convert.ToUInt64(value, 16), 2);
but this results in a confusing compiler error:
The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments
Argument 2: cannot convert from 'int' to 'System.IFormatProvider'
What's wrong with the following code?
string hex = "FFFFFFFFFFFFFFFF";
// Returns -1
long longValue = Convert.ToInt64(hex, 16);
// Returns 1111111111111111111111111111111111111111111111111111111111111111
string binRepresentation = Convert.ToString(longValue, 2);
Pretty much what you wrote (only fixed the ulong
to long
cast), and returns what you expect.
Edit:
undeleted this answer, as even if the long
representation is signed, the binary representation is actually what you expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With