开发者

How to change byte[] to hex? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicates:

How do you convert Byte Array to Hexadecimal String, and vice versa, in C#?

C# byte[] to hex string

I need to take this:

byte[] data = new byte[] { 1, 2, 3, 4 }

And turn it into something like this:

0x01020304

What is the best way to do this 开发者_如何学Cin C#?


StringBuilder sb = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
{
       sb.AppendFormat("{0:x2}", b)
}
return sb.ToString();


For a single value:

String.Format("{0:X2}", value);

Depending on what the array represents you can then do some string concatenating to put all the bits together.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜