SanVEE
SanVEE

Reputation: 2060

How to convert large Binary string to Hexa decimal string format in C#?

I'm trying to convert binary data (string) to hexa decimal data (string)

 string BinaryData = 1011000000001001001000110100010101100111100000000001000001111011100010101011";

 string HexaDecimalData = Convert.ToInt64 ( BinaryData, 2 ).ToString ( "X" );

I get a OverflowException : Value was either too large or too small for a UInt64. I can understand that the binary string is big, but at the same I cant think of any bigger data type than Int64.

any suggestions?

Upvotes: 2

Views: 366

Answers (1)

EZI
EZI

Reputation: 15354

string BinaryData = "1011000000001001001000110100010101100111100000000001000001111011100010101011";

int count = 0;
var hexstr = String.Concat(
                BinaryData.GroupBy(_ => count++ / 4)
                          .Select(x => string.Concat(x))
                          .Select(x => Convert.ToByte(x, 2).ToString("X"))
             );

Upvotes: 5

Related Questions