Thread: Wierd Hex Output

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    Wierd Hex Output

    Hi Guys,

    This is my first post here. I have this code and it does everything i want to but prints a "ffffff" infront of the output for some reason. Can you let me know why it is doing that and how to get around it?

    The code is:

    Code:
    #include<stdio.h>
    
    void main() {
    int j=51712,*y;
    char a, *z;
    y=&j;
    printf ("Size of an Integer in UltraSPARC-IIIi is %d bytes\n", sizeof(j));
    printf ("Size of a Character in UltraSPARC-IIIi is %d byte\n", sizeof(a));
    printf ("Address of variable = %x\n", y);
    printf ("--------------------------------------\n");
    z=(char *)y;
    printf ("Address\t\tValue\n");
    printf ("--------------------------------------\n");
    printf ("%x\t %02x\n", z, *z);
    printf ("%x\t %02x\n", z+1, *(z+1));
    printf ("%x\t %02x\n", z+2, *(z+2));
    printf ("%x\t %02x\n", z+3, *(z+3));
    printf ("Value of varible is %d and is %x in hex\n", j,j);
    }

    And the output is:


    Size of an Integer in UltraSPARC-IIIi is 4 bytes
    Size of a Character in UltraSPARC-IIIi is 1 byte
    Address of variable = ffbffd54
    --------------------------------------
    Address Value
    --------------------------------------
    ffbffd54 00
    ffbffd55 00
    ffbffd56 ffffffca
    ffbffd57 00
    Value of varible is 51712 and is ca00 in hex



    Now i dont know why it is prepending that "ffffff" infront of "ca"

    Thanks for all the help

    Thanks
    Mike

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's because you are translating a signed char into a signed integer, and as %x is treating the value as unsigned, you get a large positive number instead of a small negative one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    Oh. Thanks a lot for the reply

    Then how do i fix that?

    Thanks
    Mike

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Tell the compiler to cast the value to unsigned before you pass it to printf().
    Code:
    printf ("&#37;x\t %02x\n", z+2, (unsigned)*(z+2));
    Suggestion: don't use void main. cpwiki.sf.net/Void_main
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    printf ("&#37;x\t %p\n", z+2, *(z+2));

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    printf ("%x\t %p\n", z+2, *(z+2));
    You mean:
    Code:
    printf ("%p\t %x\n", z+2, *(z+2));
    And strictly sticking to the standard:
    Code:
    printf ("%p\t %x\n", (void *)(z+2), *(z+2));
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I did mean that. Swift and careless, that is my motto. Thanks Mats.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I like:
    Code:
    printf("&#37;p\t %x\n", (void*)&z[2], (unsigned)z[2]);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I also like titles that spell weird correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  2. Hex dump
    By Banana Man in forum C Programming
    Replies: 17
    Last Post: 01-06-2008, 11:03 AM
  3. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  4. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  5. Replies: 3
    Last Post: 01-23-2006, 07:25 PM