开发者

Opposite Method to BitConverter.ToString?

The BitConverter.ToString gives a hexadecimal in the format 'XX-XX-XX-XX'

Is there an opposite method to this so that I can obtain开发者_如何学Go the original byte array from a string as given in this format?


No, but its easy to implement:

string s = "66-6F-6F-62-61-72";
byte[] bytes = s.Split('-')
    .Select(x => byte.Parse(x, NumberStyles.HexNumber))
    .ToArray();


Use of string.Split, then byte.Parse in a loop is the simplest way. You can squeeze out a little more performance if you know that every byte is padded to two hex digits, there's always exactly one dash in between, by skipping the string.Split and just striding through three characters at a time.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜