473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to convert byte array with hex number to equivalent decimal value

6
Hi,
I tried to convert byte array with hex to equivalent decimal value as follows but it gives me unexpected results:

byte hex_arr[] = { 0x00, 0x01, 0xab, 0x90};
unsigned long i=0;
i = hex_arr[3] + (hex_arr[2] << 8) + (hex_arr[1] << 16) + (hex_arr[0] << 24);
the output is 4294945680
Correct output should be 109456

but when I try with byte hex_arr[]={0x00,0x00,0x0f,0xff};
it gives correct out put 4095

the correct output works until the hex values is {0x00,0x00,0x7f,0xff}

Any help is appreciated.
Sep 18 '12 #1

✓ answered by Banfa

Normally by making sure that you are using unsigned types, being unsigned sign extension doesn't apply to them which is why I keep asking "what is the defined type of byte?" which it would be quite nice to get an answer to.

Something else you many wish to try is casting your bytes to the correct type before you shift them i.e. (((unsigned long)hex_arr[0]) << 24)

8 8082
Banfa
9,065 Expert Mod 8TB
Is byte defined to a sign or unsigned value. It looks like you are getting a sign extension error which suggests byte is signed.

Personally I tend to use | (bitwise or) rather than + (addition) when combining values in this sort of algorithm.
Sep 18 '12 #2
Mapel
6
Hi Banfa,

I tried use bitwise or..but the same problem..

byte hex_arr[] = { 0x00, 0x01, 0xab, 0x90};
unsigned long i=0;

i = hex_arr[3];
i |= hex_arr[2] << 8;
i |= hex_arr[1] << 16;
i |= hex_arr[0] << 24;
Sep 18 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
I may not have had enough coffee this morning but isn't a hex 10 an A? Isn't A 65? So how does a conversion work using a shift? I have always used a switch where case A got converted to 10.
Sep 18 '12 #4
Mapel
6
Hi weaknessforcats,

A is for 10.

Thanks for the attention.
Sep 18 '12 #5
Banfa
9,065 Expert Mod 8TB
@weaknessforcats this is not converting ASCII digits to their binary value but rather combining binary bytes together to form an integer with a larger number of bits as you might do if you received the data from some byte stream (network or serial line for example).

@Mapel the suggestion to use | instead of + was not meant to fix your problem, it is a little more efficient and logically more correct for the operation you are trying to do. I still believe the issue is sign extension of a signed integer value so I ask again what is the type byte defined to be.
Sep 18 '12 #6
Mapel
6
@Banfa I am not sure of what sign extension but when I try to print the output(with signed int variable) it gives negative value. I think your point is correct. The problem is from sign extension but how can I fix this problem?
Thanks.
Sep 19 '12 #7
Banfa
9,065 Expert Mod 8TB
Normally by making sure that you are using unsigned types, being unsigned sign extension doesn't apply to them which is why I keep asking "what is the defined type of byte?" which it would be quite nice to get an answer to.

Something else you many wish to try is casting your bytes to the correct type before you shift them i.e. (((unsigned long)hex_arr[0]) << 24)
Sep 19 '12 #8
Mapel
6
Thanks so much Banfa. I got it.
Sep 19 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Lou | last post by:
How do you convert a byte array to a string?
6
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing...
2
by: Jim H | last post by:
I am getting binary data form a database binary(8). I am copying it to a byte array byte. I need to covert this to a number, ulong, so I can write it to a string in hex format. Any idea on how...
2
by: Dave | last post by:
Hi, I'm trying to convert a byte array to string --This works... System.BitConverter.ToString(bytes) "EB-55-79-20-18-B2-76-4D-85-0A-93-6B-97-33-31-B8" --This doesn't, but returns...
5
by: Terry Olsen | last post by:
Looking for info on how to convert a byte array to a string, and string to byte array. Thanks.
14
by: Charles Law | last post by:
I thought this had come up before, but I now cannot find it. I have a byte array, such as Dim a() As Byte = {1, 2, 3, 4} I want to convert this to an Int32 = 01020304 (hex). If I use...
2
by: c2h | last post by:
pls help me to solve this problem : using VB to convert a base 8 number into decimal number ..... thks
0
by: velmani | last post by:
hi , i have problem with Retrieving image from DB i have a table with image datatype i store a file by using code as follows -------------------------------------------------- string strType; ...
18
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a...
1
by: Chintan Shah | last post by:
I have a byte array which looks something like 01 00 02 00 73 45 69 A5 So i have to read first 2 bytes and convert it into integer to get its value. Same for next 2 bytes and then next 4...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.