Click here to Skip to main content
15,566,522 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
C#
string HexMessage = "00790b1a"

Equivalent in Bytes
C#
byte[] HexBytes = { 0x00, 0x79, 0x0b, 0x1a }

OR when i convert it using the specified funtion
C#
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
C#
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
Posted
Updated 19-Jan-16 21:47pm
v2
Comments
Tomas Takac 20-Jan-16 2:38am    
Do you realize that 121 dec = 79 hex and 11 dec = B hex? The last one is wrong though, because 26 dec = 1A hex, but I think that's a mistake on your part when transcribing the values.
Tomas Takac 20-Jan-16 2:54am    
As far as I can see you don't have a problem. The values are parsed correctly. What you are talking about is the representation of a number. So I guess you are converting it back to a string at some point?
pakistanmylove 20-Jan-16 3:17am    
I belive u didnt understand the issue. These HexStrings are stored in the file. I want to load these strings as Byte array without converting each in Decimal, Why, let me explain u. I want to read two bytes at a time to get decimal values. In my case I will get Two decimals combining each byte in pair and converting the value, So if values are

value[0] = 0x00;
value[1] = 0x79;

int return = BitConverter.ToInt16(value, 0);
//Return is 121

value[0] = 0x0B;
value[1] = 0x1A;
int return = BitConverter.ToInt16(value, 2);
//Return is 2842

What happening is I get the bytes already converted in decimal

value[0] = 0;
value[1] = 121;

int return = BitConverter.ToInt16(value, 0);
//Return is 121

value[0] = 11;
value[1] = 26;
int return = BitConverter.ToInt16(value, 2);
//Return is 4390

Which is wrong. I need byte array to hold its hexadecimal value in order to get correct value when converting one or more the one bytes into equivalent value.
Tomas Takac 20-Jan-16 3:46am    
Well 0B1A hex = 2842 dec while 1126 hex = 4390 dec. So your last example is wrong. Somewhere there lays your answer because the StringToByteArray method is working correctly. Funny enough you mentioned the byte order because for me BitConverter works in little-endian while you claim yours uses big-endian. Strange, isn't it?
F. Xaver 20-Jan-16 4:49am    
hm maybee check BitConverter.IsLittleEndian, mine is false. and BitConverter.ToInt16(new Byte[] { 0x0, 0x79 }, 0)) returns 30976 for me.



like Sergey and Tomas mentioned in the Comments there is nothing wrong

dec / hex / bin
121 = 79 = 0111 1001
26 = 1A = 0001 1010

the value in the array is already what you want.
It's only displayed in decimal where you wanted (or better thought it should be) hex

if you wont to display your value as Hex String just use .ToString("X")
but don't use that String for math or other stuff! only for display purposes its only a representation of the Data you already have

Bits of 121(dec) are: 0111 1001
stored as "Hex-String"(79) bits are: 0011 0111 0011 1001
totally different thing!
 
Share this answer
 
v2
Comments
pakistanmylove 20-Jan-16 3:23am    
These HexStrings are stored in the file. I want to load these strings as Byte array without converting each in Decimal, Why, let me explain u. I want to read two bytes at a time to get decimal values. In my case I will get Two decimals combining each byte in pair and converting the value, So if values are

value[0] = 0x00;
value[1] = 0x79;

int return = BitConverter.ToInt16(value, 0);
//Return is 121

value[0] = 0x0B;
value[1] = 0x1A;
int return = BitConverter.ToInt16(value, 2);
//Return is 2842

What happening is I get the bytes already converted in decimal

value[0] = 0;
value[1] = 121;

int return = BitConverter.ToInt16(value, 0);
//Return is 121

value[0] = 11;
value[1] = 26;
int return = BitConverter.ToInt16(value, 2);
//Return is 4390

Which is wrong. I need byte array to hold its hexadecimal value in order to get correct value when converting one or more the one bytes into equivalent value.
F. Xaver 20-Jan-16 4:59am    
at first.. you change your array at index 0 and 1 .. and then tell BitConverter to start in the array at index 2. but again.. there is no decimal or hexadecimal value. The program saves all values in binary. its just displayed for you as decimal as this is the system we all tend to use. There is simply no Type witch displays its Data as HEX.. for that.. System.Byte.MaxValue returns 255 and not FF as its the same, just different representations as its still saved as 1111 1111 for the computer.
Sergey Alexandrovich Kryukov 20-Jan-16 11:32am    
My 5, but, as you can probably see, so far it all looks hopeless.
Would you also look at the new inquirer's comments to the question itself...
(Sigh...)
—SA
F. Xaver 20-Jan-16 18:04pm    
yup, I saw..
Thank you
CPallini 20-Jan-16 11:39am    
5.
Decimal and hexadecimal are two different representations of a number.
C#
HexBytes[3] = 11; //(1) this is fine

HexBytes[3] = b; //(2) such a statement is not valid

HexBytes[3] = 0xb; //(3) this is fine and completely equivalent to (1)


Please note that (1) and (3) are translated to exactly the same statement by the compiler.
 
Share this answer
 
Comments
pakistanmylove 20-Jan-16 3:33am    
That is Correct

byte[] HexBytes = { 0x00, 0x79, 0x0b, 0x1a };

But when u check its element they are already converted in decimal.
Tomas Takac 20-Jan-16 4:27am    
No, they are just displayed to you in decimal. That's a big difference.
Sergey Alexandrovich Kryukov 20-Jan-16 11:31am    
My 5, but, as you can probably see, so far it all looks hopeless.
Would you also look at the new inquirer's comments to the question itself...
(Sigh...)
—SA
CPallini 20-Jan-16 11:39am    
Yes, I see.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900