Reputation: 527
I am trying to convert hex string into binary. My code looks as follows:
sw.Write(Convert.ToString(Convert.ToInt32(value, 16), 2));
However this works for most of the values; But when I convert hex string 0x101 to binarystring, my result is 100000001, rathen than 000100000001. Please help me.
Upvotes: 3
Views: 11950
Reputation: 116108
string Hex = "001";
var s = String.Join("",
Hex.Select(x => Convert.ToString(Convert.ToInt32(x+"", 16), 2).PadLeft(4,'0')));
Upvotes: 5
Reputation: 5189
How about using String.PadLeft() ?
string value = "0x001";
string binary = Convert.ToString(Convert.ToInt32(value, 16), 2).PadLeft(12, '0');
Upvotes: -1