6

I am making application in c#.Here i want to convert a byte array containing hex values to decimal values.Suppose i have one byte array as

array[0]=0X4E;
array[1]=0X5E;
array[2]=0X75;
array[3]=0X49;

Here i want to convert that hex array to decimal number like i want to concatenate first all bytes values as 4E5E7549 and after that conversion of that number to decimal.I dont want to convert each separate hex number to decimal.The decimal equivalent of that hex number is 1314813257.So please help me.Thanks in advance.

1
  • 2
    BitConverter.ToInt32() was made to do this. Although you do have to observer little-endian byte order. Array.Reverse() if you have to. Commented Nov 17, 2011 at 12:11

5 Answers 5

10

The BitConverter.ToInt32 method is a good way to do this

if (BitConverter.IsLittleEndian)
    Array.Reverse(array); //need the bytes in the reverse order
int value = BitConverter.ToInt32(array, 0);
Sign up to request clarification or add additional context in comments.

2 Comments

reversing gives absolutely required result but i couldn't understand why we have to do reversing, i might be missing some point but in my view last byte in array should represent the lower byte of the number, right?
1

hex and decimal are just different ways to represent the same data you want something like

int myInt = array[0] | (array[1] << 8) | (array[2] << 16) | (array[3] <<24)

2 Comments

i tried that but i a getting error as operator || can not be applied to operands of type byte and int
oops, fixed so they aren't logical ors anymore.
1

Here's a method to convert a string containing a hexadecimal number to a decimal integer:

private int HexToDec(string hexValue)
{
    char[] values = hexValue.ToUpperInvariant().ToCharArray();
    Array.Reverse(values);
    int result = 0;
    string reference = "0123456789ABCDEF";

    for (int i = 0; i < values.Length; i++)
        result += (int)(reference.IndexOf(values[i]) * Math.Pow(16d, (double)i));

    return result;
}

You can then string the pieces together and pass them to this function to get the decimal values. If you're using very large values, you may want to change from int to ulong.

1 Comment

@JeremyChild Yes, a decimal integer. Decimal meaning base 10.
0

Mind the byte order.

int num = 
    array[0] << 8 * 3 | 
    array[1] << 8 * 2 | 
    array[2] << 8 * 1 | 
    array[3] << 8 * 0;

Comments

0

Instead of checking IsLittleEndian by yourself, you can use IPAddress.NetworkToHostOrder(value).

int value = BitConverter.ToInt32(array, 0);

value = IPAddress.NetworkToHostOrder(value);

See more:

https://learn.microsoft.com/de-de/dotnet/api/system.net.ipaddress.networktohostorder?redirectedfrom=MSDN&view=netframework-4.7.2#System_Net_IPAddress_NetworkToHostOrder_System_Int32_

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.