Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting hex string to unsigned int issue C++ [closed]

Tags:

c++

I am currently designing a code and one of the instructions given is to get the first two digits of a hex string and do a desired operation. Everything that is less than A is taken into account. Anything greater than that is not, it just takes the next number. Which messes up the code.

Here is the line which converts it:

int x = atoi(hex.c_str);

What am I doing wrong?

like image 540
Julio Garcia Avatar asked Mar 01 '13 00:03

Julio Garcia


People also ask

How do I convert a string to an unsigned int?

In the C Programming Language, the strtoul function converts a string to an unsigned long integer. The strtoul function skips all white-space characters at the beginning of the string, converts the subsequent characters as part of the number, and then stops when it encounters the first character that isn't a number.

Are hexadecimal signed or unsigned?

A hexadecimal value is int as long as the value fits into int and for larger values it is unsigned , then long , then unsigned long etc. See Section 6.4. 4.1 of the C standard. Just as the accepted answer states.

Can you cast int to unsigned?

You can convert an int to an unsigned int . The conversion is valid and well-defined. Since the value is negative, UINT_MAX + 1 is added to it so that the value is a valid unsigned quantity. (Technically, 2N is added to it, where N is the number of bits used to represent the unsigned type.)

What is hex string in C?

Keep in mind that a string in C is an array of char values. And a char is an unsigned 8-bit quantity. (Just as an int is a signed 16-bit quantity.) The maximum value that can be placed in an unsigned 8-bit field is 255 decimal which is 0xFF hex or 0377 octal or 11111111 binary.


3 Answers

I'm assuming this is what you meant - you'd like to convert only the first two hexadecimal digits of a string to an unsigned integer. I'm also assuming that the string contains only valid hexadecimal digits. If you want to validate the string, you need to write some extra code for that.

Use strtoul to convert a hex string to unsigned integer instead. Also use substr() method of the string class to extract only the initial 2 digits of the hexadecimal string.

#include <string>
#include <cstdio>
#include <cstdlib>

int main()
{
    std::string myhex = "abcdef";
    unsigned int x = strtoul(myhex.substr(0, 2).c_str(), NULL, 16);
    printf("x = %d\n", x);
}

This would be your output (i.e. 0xab = 171):

x = 171
like image 163
Tuxdude Avatar answered Sep 30 '22 10:09

Tuxdude


the atoi function requires base 10

instead use e.g. strtol

it is generally a good idea to read the documentation. just write the function's name into AltaVista (or, for that matter, google). and it will find some documentation for you

like image 45
Cheers and hth. - Alf Avatar answered Sep 30 '22 10:09

Cheers and hth. - Alf


int x = strtol(hex.c_str(), NULL, 16);

You can also use sscanf if you want to read a partial string. You mentioned you might want to just take the first two characters:

int x;
if( 1 == sscanf(hex.c_str(), "%2x", &x) ) {
    printf( "Value is: %d\n", x );
} else {
    printf( "Conversion failed\n" );
}

Note the above isn't really C++. You can use the std::hex stream modifier for that (note that I called your string mystr this time to avoid confusion (particularly if you've imported the namespace std):

int x;
std::istringstream iss(mystr);
bool ok = (iss >> std::hex >> x);

[edit] I notice in your comment you ask about converting to an unsigned integer. In that case, why are you not explicitly declaring your integer as unsigned int?

For unsigned, you should use strtoul.

like image 40
paddy Avatar answered Sep 30 '22 10:09

paddy