Thread: String problem

  1. #1
    Registered User
    Join Date
    Jul 2007

    String problem

    I have an int variable that contains an hex number like this 084c2365. And I want to convert the first byte(65) into a decimal number (101).

    char c[100];
    int num;    //Num has the value 084c2365
    int result;
    c[0]=(char)num;  //c[0] gets the value from the firts byte (65)
    sscanf(c[0], "%x", &result);
    But I get an error at compilation the says that the 1st argument from sscanf is not an "const char *".

    How can i fix this??

  2. #2
    Registered User
    Join Date
    Oct 2001
    c[0] is a char.

    initialize your variables.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    int num = 0x084c2365;
    int result = num & 0xFF;
    printf("%d", result);
    decimal or hexdecimal - is just representetion during output - internally - it is the same number stored as binary, no need to convert
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2007
    Thanks, but does it matter if I have my num variable as an uint32_t and I just have the 84c2365 number without the 0x notation?

    So will
    int result = num & 0xFF;
    make the conversion from hex to decimal??

    Sorry I am a junior and I need to learn a lot.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you have 10 apples or 0x0A apples or 012 apples, how many apples do you have?

    The whole idea of 'base' is just a presentation issue, so whether you say 10, 012 or 0xA doesn't matter, the underlying number is still the same.

    Hex is just convenient since 2 hex digits fit exactly in one 8-bit byte, so from that point of view, it's a bit easier to imagine what is going on when you say
    int result = num & 0xFF;
    rather than
    int result = num & 255;
    But the result is exactly the same as far as the machine is concerned.

    Likewise, octal was great on certain historic machines which had say 6-bits or 9-bits per byte.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM