String data into byte array without hex conversion
-
How do I get string data into a byte array, programmatically, without doing a hex conversion. i.e., byte[i] = (???)string.Substring(0,2); I need the programmatic equivelent of: byte[] bytes = {0x14, 0x55, 0x90, 0x00, 0x01}; or byte[i] = {0x + string} I can't make any of this work. Need Guru! TIA, Steve
-
How do I get string data into a byte array, programmatically, without doing a hex conversion. i.e., byte[i] = (???)string.Substring(0,2); I need the programmatic equivelent of: byte[] bytes = {0x14, 0x55, 0x90, 0x00, 0x01}; or byte[i] = {0x + string} I can't make any of this work. Need Guru! TIA, Steve
Hi there, Maybe this will put you in the right track:
string StringToConvert = "abc"; //This call will return a byte array with the numeric values of the chars byte[] bytes = StringToByteArray(StringToConvert); private byte[] StringToByteArray(string str) { char[] chars = str.ToCharArray(); byte[] bytes = Array.ConvertAll(chars, new Converter(CharToByte)); return bytes; } private byte CharToByte(char ch) { return (byte)ch; }
Hope it helps. Danny -
Hi there, Maybe this will put you in the right track:
string StringToConvert = "abc"; //This call will return a byte array with the numeric values of the chars byte[] bytes = StringToByteArray(StringToConvert); private byte[] StringToByteArray(string str) { char[] chars = str.ToCharArray(); byte[] bytes = Array.ConvertAll(chars, new Converter(CharToByte)); return bytes; } private byte CharToByte(char ch) { return (byte)ch; }
Hope it helps. DannyHi Danny, This looks like what I'm after except I'm getting two errors when implemented: Error 1 Using the generic type 'System.Converter' requires '2' type arguments Error 2 The type arguments for method 'System.Array.ConvertAll(TInput[], System.Converter)' cannot be inferred from the usage. Try specifying the type arguments explicitly. What did I do wrong?
-
How do I get string data into a byte array, programmatically, without doing a hex conversion. i.e., byte[i] = (???)string.Substring(0,2); I need the programmatic equivelent of: byte[] bytes = {0x14, 0x55, 0x90, 0x00, 0x01}; or byte[i] = {0x + string} I can't make any of this work. Need Guru! TIA, Steve
-
Hi Danny, This looks like what I'm after except I'm getting two errors when implemented: Error 1 Using the generic type 'System.Converter' requires '2' type arguments Error 2 The type arguments for method 'System.Array.ConvertAll(TInput[], System.Converter)' cannot be inferred from the usage. Try specifying the type arguments explicitly. What did I do wrong?
Sorry, I guess the pasting went wrong, the function should look like this: private byte[] StringToByteArray(string str) { char[] chars = str.ToCharArray(); byte[] bytes = Array.ConvertAll<char, byte>(chars, new Converter<char, byte>(CharToByte)); return bytes; }
-
Hi Danny, This looks like what I'm after except I'm getting two errors when implemented: Error 1 Using the generic type 'System.Converter' requires '2' type arguments Error 2 The type arguments for method 'System.Array.ConvertAll(TInput[], System.Converter)' cannot be inferred from the usage. Try specifying the type arguments explicitly. What did I do wrong?
private byte[] StringToByteArray(string str) { char[] chars = str.ToCharArray(); byte[] bytes = Array.ConvertAll<char, byte>(chars, new Converter<char, byte>(CharToByte)); return bytes; }