Velocity Reviews > newbie: hex, decimal, binary conversion

# newbie: hex, decimal, binary conversion

bob
Guest
Posts: n/a

 03-17-2006
Earlier in this group I posted:

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

int main()
{
int 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

Guest
Posts: n/a

 03-17-2006
On Friday 17 March 2006 18:27, bob opined (in
<(E-Mail Removed) .com>):

> Earlier in this group I posted:
>
>

************************************************** *************************
> I can't get 'number' to print like I think it should:
>
> int main()
> {
> int number;
> 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.

--

You can't erase a dream, you can only wake me up.
-- Peter Frampton

bob
Guest
Posts: n/a

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

Guest
Posts: n/a

 03-17-2006
On Friday 17 March 2006 19:35, bob opined (in
<(E-Mail Removed). com>):

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

You're not quoting any context. Read:

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

I don't think you're doing anything wrong. I took your numbers and
rearranged them as I posted previously, and got the same as Keith (my
implementation does the same as well).

Some of the missing context:

> bob wrote:
> > '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.
>

--

Go climb a gravity well!

NUPUL
Guest
Posts: n/a

 03-18-2006
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

Guest
Posts: n/a

 03-18-2006
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

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

> 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
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.

--

Preudhomme's Law of Window Cleaning:
It's on the other side.

Peter Shaggy Haywood
Guest
Posts: n/a

 03-21-2006
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("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!

"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"?