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)
CoderTao Aug 04 '09 at 23:00 2009-08-04 23:00
source share