Scenario is i need to load byte stings from the file and convert them into byte array and process it. Problem is when i convert the byte string into bytes the values Byte array holds already converted into decimal, which I don't want
string HexMessage = "00790b1a"
Equivalent in Bytes
byte[] HexBytes = { 0x00, 0x79, 0x0b, 0x1a }
OR when i convert it using the specified funtion
byte[] HexBytes = StringToByteArray(HexMessage);
But when u access the elements its converted into decimal and stored in byte arrays like
HexBytes[0] = 0;
HexBytes[2] = 121;
HexBytes[3] = 11;
HexBytes[4] = 26;
I need the original bytes to be stored in the array so when i want to convert 2 bytes i could get correct value for shortint.
HexBytes[0] = 0;
HexBytes[2] = 79;
HexBytes[3] = 0b;
HexBytes[4] = 1a;
FUNCTION IM USING IS
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
}
I have been banging my head for 2 days couldnt find the solution to it. Let me know if anyone has already figured this out. Thanks In advance