| | |
Converting hex to decimal
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 11
Reputation:
Solved Threads: 0
So, I am learning C and have this newby problem. This is my snippet:
This prints:
Size of char: 4-bits
Size of int: 16-bits
0xFFFFFFFF in decimal: -1
0xFFFF in decimal: 65535
Question:
a. Why does the program deliver a 8-bit hex number (0xFFFFFFFF) when the input is 0xFF and when the sizeof() delivers char as 4-bit ?
b. The program returns 0xFFFFFFFF as -1 in decimal. However when I use a Hex-Decimal converter I get 0xFF as 255 and 0xFFFFFFFF as 4294967295?
C Syntax (Toggle Plain Text)
#include <stdio.h> main() { char x; unsigned y; x=0xFF; y=0xFFFF; printf("Size of char: %d-bits\n", sizeof(char)*4); printf("Size of int: %d-bits\n", sizeof(int)*4); printf("\n0x%X in decimal: %d\n", x, x); printf("\n0x%X in decimal: %d\n", y, y); return 0; }
Size of char: 4-bits
Size of int: 16-bits
0xFFFFFFFF in decimal: -1
0xFFFF in decimal: 65535
Question:
a. Why does the program deliver a 8-bit hex number (0xFFFFFFFF) when the input is 0xFF and when the sizeof() delivers char as 4-bit ?
b. The program returns 0xFFFFFFFF as -1 in decimal. However when I use a Hex-Decimal converter I get 0xFF as 255 and 0xFFFFFFFF as 4294967295?
•
•
•
•
Question:
a. Why does the program deliver a 8-bit hex number (0xFFFFFFFF) when the input is 0xFF and when the sizeof() delivers char as 4-bit ?
c Syntax (Toggle Plain Text)
printf("Size of char: %d-bits\n", sizeof(char)*4);
sizeof(char) ), so this multiplied by 4 will always give 4 as the result.The correct way to get the correct size in bits per byte (a char is always 1 byte) is by using the CHAR_BIT macro as defined in the limits.h header (you have to include this file if you want to use the CHAR_BIT macro, you can do this by adding this directive to the top of your program:
#include <limits.h> ).The correct way of getting the size (in bits) of a variable of type char and or integer in C:
char:
c Syntax (Toggle Plain Text)
printf("Size of a variable of type char (in bits): %lu", CHAR_BIT);
integer:
C Syntax (Toggle Plain Text)
printf("Size of a variable of type integer (in bits): %lu", CHAR_BIT*sizeof(int));
What exactly does CHAR_BIT represent?
The CHAR_BIT macro represents the number of bits in a byte (a variable of type char does always have the size of one byte, as defined per standard, but this isn't the case for variables of other types, they depend on your C-implementation).
So multiplying the value of CHAR_BIT by the sizeof of a certain variable type (int, double, float...) will give you the number of bytes that type consists of.
Note that sizeof(type) returns the size (in bytes) of a variable type.
Also notice that I used the %lu format specifier in the format string passed to the printf() function, this is because the sizeof operator returns a value of an unsigned integral datatype (as defined per standard), to produce portable code I added the %lu format specifier (which will ensure a cast to an unsigned long, the biggest unsigned integral type) to make sure that the size will always be displayed correctly on the screen.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
But, what about my other question. Why do I get 0xFFFFFFF and -1 as the decimal?

Just as a convenience for anyone who's reading the thread:
•
•
•
•
b. The program returns 0xFFFFFFFF as -1 in decimal. However when I use a Hex-Decimal converter I get 0xFF as 255 and 0xFFFFFFFF as 4294967295?
int a_variable_name ).As int exists in both signed and unsigned flavors, your hex-converter was using an unsigned int to do the conversion, while in your code you have a signed int, as the name tells you it's signed, which means that it can have a sign, this isn't the case with unsigned integers which cannot have a sign.
A very non-technical explanation (but maybe less correct) is: with signed integers the high-order bit signifies whether the number is negative (high-order bit is one), or positive (high-order bit is zero).
With unsigned integers, the high-order bit is used to store another range of possible numbers.
Important to mention is that both signed and unsigned types do consume the same amount of bytes, this is also why an unsigned integer variable can store twice as huge positive values as a signed one, while a signed one can also store negative numbers.
Maybe something interesting for you to google up is: two's complement, this will give you a better insight in how the whole thing (with sign bits, etc.) works.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
(which will ensure a cast to an unsigned long, the biggest unsigned integral type)
C Syntax (Toggle Plain Text)
printf("%lu\n", (unsigned long)sizeof(something));
•
•
•
•
to make sure that the size will always be displayed correctly on the screen.
C Syntax (Toggle Plain Text)
printf("%zu\n", sizeof(something));
-Tommy (For Great Justice!) Gunn
•
•
•
•
b. The program returns 0xFFFFFFFF as -1 in decimal. However when I use a Hex-Decimal converter I get 0xFF as 255 and 0xFFFFFFFF as 4294967295?
0xFFFFFFFF has a binary representation wherein every bit is one, if such a binary value goes into a signed integer variable, then the variable contains a value equivalent to -1 in decimal.If you read about the two's complement (A must read! Google it up), you'll be able to understand why this is the case

Also thanks to Tom Gunn for pointing out a mistake in my post and providing an excellent correction
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Nov 2008
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
In my previous post I forgot to point out that0xFFFFFFFFhas a binary representation wherein every bit is one, if such a binary value goes into a signed integer variable, then the variable contains a value equivalent to -1 in decimal.
If you read about the two's complement (A must read! Google it up), you'll be able to understand why this is the case
Also thanks to Tom Gunn for pointing out a mistake in my post and providing an excellent correction
However, the strange thing, it doesn't matter whether I have the int variable signed or unsigned - the program always returns the same number:
int y=0xFFFF returns 65,535
unsigned y=0xFFFF returns 65,535 (should return -1)
Any advice?
I'm rather curious why you think an unsigned value should be negative, but do remember to try to use the correct format specifiers with printf for the data type being used.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Nov 2008
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
I'm rather curious why you think an unsigned value should be negative, but do remember to try to use the correct format specifiers with printf for the data type being used.
The printf is as follows:
printf("\n0x%X in decimal: %d\n", y, y);
(printf("\n0x%X in decimal: %u\n", y, y)
returns the same (65.535)Do I need a different format specifier in printf ?
thanks,
![]() |
Similar Threads
- Converting Binary to Decimal (C++)
- hex string to decimal (C)
- Hex to Decimal (C)
- Illegal hex digit (Perl)
- 8086, hex to decimal program (Assembly)
- Convert string decimal to HEX, reverse, then convert back to string decimal. (MS SQL)
- Converting ASCII to Decimal (Assembly)
Other Threads in the C Forum
- Previous Thread: Byte Reversal
- Next Thread: Functions with Pointers
Views: 1573 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






