Thread: sscanf and 32 bit two's complement

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    sscanf and 32 bit two's complement

    I will have a string that will contain a 32 bit integer in two's complement notation. The integer can be in decimal(e.g 2123) or in hexadecimal format(e.g. 0xf0000000). The goal is to convert it into int. If an overflow or underflow occurs (e.g. 2147483648) the program must throw an error.

    Note: since it's in 32 bit two's complement, the value of 0xfffffffd will be -3 not 268435473. For normal decimal values a minus(-) sign will indicate that the value is negative(duh!).

    I have tried strtol with errno, but it returns positive values for all hexadecimal values. And sscanf() cannot check overflow. For the moment I have implemented hex and dec separately in my main program. I used strtol with errno for decimal values. For hex I have checked if the string size() returns <= 10(including 0x) or not, and if it is I have used sscanf() to turn it into int. It works, but I was looking for a more elegant solution that combines both dec and hex.
    Code:
    #include<iostream>
    #include<cerrno>
    #include<string>
    #include<cstdlib>
    
    using namespace std;
    
    //0000 0000 0000 0000 0000 0000 0000 0011 = 3
    //1111 1111 1111 1111 1111 1111 1111 1101 = -3
    //f    f    f    f    f    f    f    d
    int main()
    {
    	string s = "0xfffffffd";
    	
    	int value = strtol(s.c_str(), NULL, 0);
    	
    	if(errno == ERANGE)
    	{
    		cout<<"Overflow"<<endl;	
    		//exit(1);
    	}
    	
    	cout<<"using strtol: "<<value<<endl;
    	sscanf(s.c_str(), "%x", &value);
    	cout<<"using sscanf: "<<value<<endl;
    	
    	return 0 ;
    }
    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Just use strtol() for both decimal and hex.

    If you want the unsigned 0xfffffffd to be signed, then just do that as a simple calculation, by checking the most significant bit.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    Is the checking of the most significant bit more straightforward than the use of sscanf with size()?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Is this doable?
    Code:
       unsigned int temp = strtoul(s.c_str(), NULL, 0);
       int value = temp;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    Quote Originally Posted by Dave_Sinkula
    Is this doable?
    Code:
       unsigned int temp = strtoul(s.c_str(), NULL, 0);
       int value = temp;
    Yes, it is doable, and it works exactly the way i wanted.

    Thank you very much.

Popular pages Recent additions subscribe to a feed