String problem

This is a discussion on String problem within the C Programming forums, part of the General Programming Boards category; I have an int variable that contains an hex number like this 084c2365. And I want to convert the first ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    19

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

    Code:
    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??
    Thanks.

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

    initialize your variables.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    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
    Code:
    int result = num & 0xFF;
    make the conversion from hex to decimal??

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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,910
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10: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, 10:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21