newbie: hex, decimal, binary conversion

B

bob

Earlier in this group I posted:

***************************************************************************
I can't get 'number' to print like I think it should:

int main()
{
int number;
printf("two digit number please:\n");
fread(&number, sizeof(int), 1, stdin);
printf("your number is: %d\n", number);
return 0;
}
***************************************************************************
and Keiths response was:

When I tried this program myself, I entered "45" followed by a
newline, then I had to enter another newline to make a total of 4
input characters (sizeof(int) is 4 on my system). The value stored in
"number" was 168441140, the result of storing the integer values of
'4', '5', '\n', and '\n' into a 4-byte integer. Your results might
differ, but see if you can figure out why you got the result you did.

******************************************************************************************

So I tried to figure out what '4' '5' \n \n would be in hex, binary,
and decimal. I got

'4' = 00110100 0x34
'5' = 00110101 0x35
\n = 00000110 0x0a

so that pasted together should be: 0x34350a0a = 875891210

00110100,00110101,00000110,00000110 = 875890182
( I put commas in to
make it less painful to look at)

I used this calculator
(http://www.microcontroller.com/Embedded.asp?did=92) for the binary to
decimal and hex to decimal conversion.

Can someone tell me where I went wrong? Thanks
 
V

Vladimir S. Oka

Earlier in this group I posted:

***************************************************************************
I can't get 'number' to print like I think it should:

int main()
{
int number;
printf("two digit number please:\n");
fread(&number, sizeof(int), 1, stdin);
printf("your number is: %d\n", number);
return 0;
}
***********************************************************
and Keiths response was:

When I tried this program myself, I entered "45" followed by a
newline, then I had to enter another newline to make a total of 4
input characters (sizeof(int) is 4 on my system). The value stored in
"number" was 168441140, the result of storing the integer values of
'4', '5', '\n', and '\n' into a 4-byte integer. Your results might
differ, but see if you can figure out why you got the result you did.

*************************************************************

So I tried to figure out what '4' '5' \n \n would be in hex, binary,
and decimal. I got

'4' = 00110100 0x34
'5' = 00110101 0x35
\n = 00000110 0x0a

so that pasted together should be: 0x34350a0a = 875891210

00110100,00110101,00000110,00000110 = 875890182
( I put commas in to
make it less painful to look at)

I used this calculator
(http://www.microcontroller.com/Embedded.asp?did=92) for the binary to
decimal and hex to decimal conversion.

Can someone tell me where I went wrong? Thanks

You assumed that Keith's machine's endianness is the other way around.
Try 0x0A0A3534 instead. NB, /your/ machine may be different.
 
B

bob

Thanks, I see how Keith got his value. But I'm still confused about the
binary part. What am I doing wrong there?
 
V

Vladimir S. Oka

N

NUPUL

hi bob,

well i don't know the purpose of your code...i presume you are tryin to
print the nos in hex, bin, dec....

for hex printing you can try %x in printf as a format string...it gives
you a hex out put.

as for binary....you can easily create your own showbits() func...using
the bit wise operators

and &
or |
one's complement ~
exor ^
left shift <<
rt shift >>

hope you have fun exploring this new concept.

nupul
 
V

Vladimir S. Oka

NUPUL opined:
well i don't know the purpose of your code...

And we don't know the purpose of your post, as you did not quote
any context. Read:

<http://cfaj.freeshell.org/google/>
i presume you are
tryin to print the nos in hex, bin, dec....

No, that was in an earlier thread, and has been answered. In
this thread, bob was asking about the confusion with
endianness.
for hex printing you can try %x in printf as a format
string...it gives you a hex out put.

as for binary....you can easily create your own showbits()
func...using the bit wise operators

Why `showbits` in particular?
and &
or |
one's complement ~
exor ^

If you're posting this for someone that may find these a "new
concept", spelling out "exclusive or" might have been better.
left shift <<
rt shift >>

Also, what is "rt" short for? Rotary, right, or respect?
hope you have fun exploring this new concept.

Lurk for a while in c.l.c, before exploring the concept of
posting again.
 
P

Peter Shaggy Haywood

Groovy hepcat bob was jivin' on 17 Mar 2006 10:27:57 -0800 in
comp.lang.c.
newbie: hex, decimal, binary conversion's a cool scene! Dig it!
Earlier in this group I posted:

***************************************************************************
I can't get 'number' to print like I think it should:

int main()
{
int number;
printf("two digit number please:\n");
fread(&number, sizeof(int), 1, stdin);
printf("your number is: %d\n", number);
return 0;
}
***************************************************************************
and Keiths response was:

When I tried this program myself, I entered "45" followed by a
newline, then I had to enter another newline to make a total of 4
input characters (sizeof(int) is 4 on my system). The value stored in
"number" was 168441140, the result of storing the integer values of
'4', '5', '\n', and '\n' into a 4-byte integer. Your results might
differ, but see if you can figure out why you got the result you did.

******************************************************************************************

So I tried to figure out what '4' '5' \n \n would be in hex, binary,
and decimal. I got

'4' = 00110100 0x34
'5' = 00110101 0x35
\n = 00000110 0x0a

Binary 00000110 is 0x06.
so that pasted together should be: 0x34350a0a = 875891210

00110100,00110101,00000110,00000110 = 875890182

Are you on a big-endian machine? (Clue, a PC is *not* big-endian.)
If you are, then you should find that your result (assuming your
machine uses ASCII and has a 4 byte int) is 875891210 (since 0x0a is
binary 1010, not binary 110).
Keith is apparently using an ASCII based little-endian machine with
a 4 byte int. His result is this:

'4' = 0x34 = binary 00110100
'5' = 0x35 = binary 00110101
'\n' = 0x0a = binary 00001010

=>

0x0a0a3534 = binary 00001010000010100011010100110100 = 168441140
Can someone tell me where I went wrong? Thanks

You failed to take endianness into account. Endianness is a
description of the order in which bytes are stored and interpreted.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top