Computing.Net > Forums > Programming > C Program: Hex value output

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C Program: Hex value output

Reply to Message Icon

Name: cool_andy
Date: August 26, 2008 at 07:52:31 Pacific
OS: Windows XP
CPU/Ram: 512
Product: Acer
Comment:

Hi,
When I output the individual hex values of this file which contains:
81 a8 90 47 48 00 00 00 48 00 00 00 0a

I get the following:
ffffff81
ffffffa8
ffffff90
47 - (date back to front)
48 - (stock level)
00
00
00
48 - (average)
00
00
00
0a - (end of line)

The prefixed "ffffff" values I have to remove before running my HexToDec function.

I get my expected values (contains, date, stock level and average) but why does this printf statement show values such as ffffff81, and ffffffa8 but are invisible in the file contents?

printf("%.2x\n",((char *)buffer)[i]);

This was previously loaded into a buffer char variable by reading the binary file from start to finish.

Thanks for your help
Andrew



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: August 26, 2008 at 18:41:45 Pacific
Reply:

Just glancing at your output, I'd say you're converting signed chars into signed ints.


0

Response Number 2
Name: cool_andy
Date: August 27, 2008 at 04:58:35 Pacific
Reply:

This seems to have solved the problem:
printf("%.2x\n",((unsigned char *)buffer[i]);

I then store all these hex values as strings and convert to decimal values.

If I use (unsigned int *), this works for the first line but I get unwanted values because line feed 0a runs into the date on the next line.

e.g (2 line file)
81 a8 90 47 48 00 00 00 48 00 00 00 0a 02 85 94
47 5c 00 00 00 a5 00 00 00 0a

The second date becomes as unsigned int 9485020a. This should be 47948502.

Therefore I prefer the unsigned char method so I can treat each individual hex value and store in overall date string, stock level and average.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C Program: Hex value output

C Programming Problem www.computing.net/answers/programming/c-programming-problem/1268.html

C Programming Problem , www.computing.net/answers/programming/c-programming-problem-/942.html

C++ Program www.computing.net/answers/programming/c-program/11402.html