newbie: hex, decimal, binary conversion

Discussion in 'C Programming' started by bob, Mar 17, 2006.

  1. bob

    bob Guest

    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
    bob, Mar 17, 2006
    #1
    1. Advertising

  2. On Friday 17 March 2006 18:27, bob opined (in
    <>):

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

    --
    BR, Vladimir

    You can't erase a dream, you can only wake me up.
    -- Peter Frampton
    Vladimir S. Oka, Mar 17, 2006
    #2
    1. Advertising

  3. bob

    bob Guest

    Thanks, I see how Keith got his value. But I'm still confused about the
    binary part. What am I doing wrong there?
    bob, Mar 17, 2006
    #3
  4. On Friday 17 March 2006 19:35, bob opined (in
    <>):

    > 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://cfaj.freeshell.org/google/>
    <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:

    Vladimir wrote:
    > 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.
    > Try 0x0A0A3534 instead. NB, your machine may be different.
    >


    --
    BR, Vladimir

    Go climb a gravity well!
    Vladimir S. Oka, Mar 17, 2006
    #4
  5. bob

    NUPUL Guest

    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
    NUPUL, Mar 18, 2006
    #5
  6. 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/>
    <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
    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.

    --
    BR, Vladimir

    Preudhomme's Law of Window Cleaning:
    It's on the other side.
    Vladimir S. Oka, Mar 18, 2006
    #6
  7. 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"?
    Peter Shaggy Haywood, Mar 21, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,147
    Neredbojias
    Aug 19, 2005
  2. Brad

    Large Decimal to Hex Conversion in C

    Brad, Jul 10, 2003, in forum: C Programming
    Replies:
    18
    Views:
    960
  3. Bengt Richter
    Replies:
    6
    Views:
    450
    Juha Autero
    Aug 19, 2003
  4. http://vmdd.tech.mylinuxisp.com/catalog/

    VMDD Tech announces the availability of CS101 - Decimal/Hex/Binary calculator

    http://vmdd.tech.mylinuxisp.com/catalog/, Jan 8, 2005, in forum: C Programming
    Replies:
    4
    Views:
    346
    dandelion
    Jan 10, 2005
  5. Ven
    Replies:
    3
    Views:
    1,307
Loading...

Share This Page