BitConverter.ToString () in reverse order?

I have an array of bytes that I would like to store as a string. I can do it like this:

byte[] array = new byte[] { 0x01, 0x02, 0x03, 0x04 }; string s = System.BitConverter.ToString(array); // Result: s = "01-02-03-04" 

So far so good. Does anyone know how I will return to the array? There is no overload of BitConverter.GetBytes () that takes a string, and it seems like it is an unpleasant workaround to split a string into an array of strings and then convert each one.

The array in question may have a variable length, probably about 20 bytes.

+20
c #
Aug 04 '09 at 22:54
source share
7 answers

Not an inline method, but an implementation. (This can be done without a split, though).

 String[] arr=str.Split('-'); byte[] array=new byte[arr.Length]; for(int i=0; i<arr.Length; i++) array[i]=Convert.ToByte(arr[i],16); 

Non-split method: (makes a lot of assumptions about the format of the string)

 int length=(s.Length+1)/3; byte[] arr1=new byte[length]; for (int i = 0; i < length; i++) arr1[i] = Convert.ToByte(s.Substring(3 * i, 2), 16); 

And one more method, without division or substring. However, you can be shot if you pass this to the original control. I am not responsible for such health problems.

 int length=(s.Length+1)/3; byte[] arr1=new byte[length]; for (int i = 0; i < length; i++) { char sixteen = s[3 * i]; if (sixteen > '9') sixteen = (char)(sixteen - 'A' + 10); else sixteen -= '0'; char ones = s[3 * i + 1]; if (ones > '9') ones = (char)(ones - 'A' + 10); else ones -= '0'; arr1[i] = (byte)(16*sixteen+ones); } 

(basically an implementation of base16 conversion on two characters)

+19
Aug 04 '09 at 23:00
source share

You can analyze the line yourself:

 byte[] data = new byte[(s.Length + 1) / 3]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)( "0123456789ABCDEF".IndexOf(s[i * 3]) * 16 + "0123456789ABCDEF".IndexOf(s[i * 3 + 1]) ); } 

The most accurate solution, although I believe it uses extensions:

 byte[] data = s.Split('-').Select(b => Convert.ToByte(b, 16)).ToArray(); 
+18
Aug 04 '09 at 23:09
source share

If you do not need this particular format, try using Base64 , for example:

 var bytes = new byte[] { 0x12, 0x34, 0x56 }; var base64 = Convert.ToBase64String(bytes); bytes = Convert.FromBase64String(base64); 

Base64 will also be significantly shorter.

If you need to use this format, this clearly will not help.

+13
Aug 04 '09 at 23:32
source share
 byte[] data = Array.ConvertAll<string, byte>(str.Split('-'), s => Convert.ToByte(s, 16)); 
+3
Aug 6 '13 at 14:28
source share

I believe the following will resolve it decisively.

 public static byte[] HexStringToBytes(string s) { const string HEX_CHARS = "0123456789ABCDEF"; if (s.Length == 0) return new byte[0]; if ((s.Length + 1) % 3 != 0) throw new FormatException(); byte[] bytes = new byte[(s.Length + 1) / 3]; int state = 0; // 0 = expect first digit, 1 = expect second digit, 2 = expect hyphen int currentByte = 0; int x; int value = 0; foreach (char c in s) { switch (state) { case 0: x = HEX_CHARS.IndexOf(Char.ToUpperInvariant(c)); if (x == -1) throw new FormatException(); value = x << 4; state = 1; break; case 1: x = HEX_CHARS.IndexOf(Char.ToUpperInvariant(c)); if (x == -1) throw new FormatException(); bytes[currentByte++] = (byte)(value + x); state = 2; break; case 2: if (c != '-') throw new FormatException(); state = 0; break; } } return bytes; } 
+1
Aug 04 '09 at 23:23
source share

seems like an unpleasant workaround to split a string into an array of strings and then convert each one of them.

I don’t think there is another way ... the format created by BitConverter.ToString is quite specific, so if there is no existing method to parse it to byte [], I think you have to do it yourself

0
Aug 04 '09 at 23:03
source share

the ToString method is not really intended to be converted, but to provide a readable format for debugging, simple printing, etc.
I would revise the byte requirement [] - String - byte [] and would probably prefer the SLaks Base64 solution

0
Dec 21 '09 at 10:05
source share



All Articles