Prev: ComboBox with AutoCompleteMode = SuggestAppend and custom search (Contains)?
Next: Question about sorting
From: Arne Vajhøj on 21 Apr 2010 19:49 On 21-04-2010 08:18, Tony Johansson wrote: > If I have some bytes that are represented in hex like this. > 12, bd,8f,7e,4e,4d. How do I assign all these bytes to a byte array ? > If the bytes were represented in decimal I would have been able to use this > statement > byte[] myBytes = new byte[]{ here I could have specified all the bytes with > comma in between }; > > But now when I have hex it's not possible. I could translate the hax value > into decimal but I hope there must be a better way. There are several ways. One of the nice is: public static byte[] FromHex(string s) { byte[] ba = new byte[(s.Length+1)/3]; for(int i = 0; i < ba.Length; i++) { ba[i] = byte.Parse(s.Substring(3 * i, 2), NumberStyles.HexNumber); } return ba; } byte[] b = FromHex(s); One of the short is: byte[] b = s.Split(',').Select((s) => byte.Parse(s, NumberStyles.HexNumber)).ToArray(); Arne
First
|
Prev
|
Pages: 1 2 Prev: ComboBox with AutoCompleteMode = SuggestAppend and custom search (Contains)? Next: Question about sorting |