Thread: Convert string to signed hex

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30

    Convert string to signed hex

    Hi, as part of my program i have to read in 6 digit (24bit) hex values as strings and then convert them. This works fine with the strtol function until i hit values where the most significant digit is greater than 7 (i.e. a negative value).

    Eg. I read in FFFFFF, in my program this should be read as -1 however
    Code:
    strtol(string, NULL, 16)
    returns 16777215.

    Is there a function which takes the sign bit into account? Also sorry if i'm making no sense, i'm a tard when it comes to sign bits

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    FFFFFF is not equal to -1 on most systems.
    FFFFFFFF is equal to -1.

    So the most significant bit isn't set to 1; it has 8 more bits that are 0.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Alright, well then how do i get a function to work with 24bits instead of 32?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Well... you could compare the result with 0x7FFFFF. If it's greater than that, then you can change the result by subtracting 0x1000000 from it.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by boblettoj99 View Post
    Alright, well then how do i get a function to work with 24bits instead of 32?
    strtol() doesn't even work that way for 32 bit numbers since a long is often 8 bytes.
    Last edited by MK27; 05-10-2010 at 10:06 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by EVOEx View Post
    Well... you could compare the result with 0x7FFFFF. If it's greater than that, then you can change the result by subtracting 0x1000000 from it.
    Or, you could test the hob of the 24 bit number, and pad to the left with X'FF's if on.

    As in
    Code:
    if (24_bit_value & 0x00800000) 24_bit_value |= 0xFF000000 ;
    Or, since you are working with strings, you could test the first char too:

    Code:
    char newstring[9] = {0} ; 
    if ( string[0] == '8' || string[0]== '9' || (toupper(string[0] >= 'A' && toupper(string[0] <= 'F') ) { 
        strcat(newstring, "FF") ; 
        strcat(newstring, string) ; 
    }
    else  strcpy(newstring, string) ;
    (not syntax checked)
    Last edited by Dino; 05-10-2010 at 08:16 AM. Reason: add string option
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    @Dino: that makes further assumptions that aren't completely portable. First of all, MK27's point that a long is usually not even 32 bits but 64. But even if you would pad more bits, it still wouldn't be portable. It would still assume a bit size that isn't always true (64 bits), and also 2s complement is assumed, which isn't always true (though I've never seen a system run that doesn't use 2s complement).

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    @EVOEx - with illustrations, all those assumptions are left to the OP to worry about.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by EVOEx View Post
    @Dino: that makes further assumptions that aren't completely portable.
    Who said anything about portability? He's talking about wanting 24 bit integers.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM