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’
There might be a better solution, but check if this works:
The most convenient way would be to use
Convert.ToString(Int64, Int32), but there is no overload for ulong. Another solution isConvert.ToString(UInt64, IFormatProvider)and write your own IFormatProvider. By looking at the examples I found an IFormatProvider that formats numbers in binary, octal and hex string representation: http://msdn.microsoft.com/en-us/library/system.icustomformatter.aspx.The code there looks very similar to what I provided, so I thinks its not a bad solution.