c++ convert string to hex [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ convert hex string to signed integer
I allready searched on google but didn't find any help.
So heres my problem:
I have strings that allready contains hex code e.g.: string s1 = "5f0066"
and i want to convert this string to hex.
I need to compare the string with hex code i've read in a binary file.
(Just need the basic idea how to convert the string)
5f
Best regards and thanks.
Edit: Got the binary file info in the format of unsigned char. SO its 0x5f...
WOuld like to have teh string in the same format

Use std:stoi as (in C++11 only):
std::string s = "5f0066";
int num = std::stoi(s, 0, 16);
Online demo

Use stringstream and std::hex
std::stringstream str;
std::string s1 = "5f0066";
str << s1;
int value;
str >> std::hex >> value;

strtol(str, NULL, 16) or strtoul(str, NULL, 16) should do what you need.
strtol
strtoul

Related

Arduino: Get Hex value from two decimal strings

I would like to do a conversion but don't know how.
Originally I get a Hex value like 03 EE which represents 1006.
Now those data is represent in a String array and as decimals:
[0] = "3"
[1] = "238"
Whats the easiest way to get back to a decimal 1006 from this situation?
I do this on an Arduino with C++
Something like this should do it:
const char* s[] = {"3", "238"};
int result = (std::stoi(std::string(s[0])) << 8)
+ std::stoi(std::string(s[1]));
std::cout << result;
Please note that I use std::stoi, if you don't like it, please see for more conversion possibilities at: Convert string to int C++
live example
As Arduino's String cannot process hex representations, you should do it manually or with another libraries (e.g. using sprintf from standard C library, if it is possible). Here is a manual way:
int decBytesToInt(const String &msByte, const String &lsByte)
{
int res = 0;
res |= (msByte.toInt() & 0xFF) << 8;
res |= (lsByte.toInt() & 0xFF);
}
msByte and lsByte are most significant and least significant bytes decimal representations correspondingly.
Supposed that int is 16-bit, of course.

C++: Convert std::string to UINT64

I need to convert a (decimal, if it matters) string representation of a number input from a text file to a UINT64 to pass to my data object.
size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input;
//convert num to input required here
Is there any way to convert an std::string to a UINT64 in a similar way to atoi()?
Thanks!
Edit:
Working code below.
size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input; //= std::strtoull(num.cstr(), NULL, 0);
std::istringstream stream (num);
stream >> input;
Use strtoull or _strtoui64().
Example:
std::string s = "1123.45";
__int64 n = std::strtoull(s.c_str(),NULL,0);
There are at least two ways to do this:
Construct a std::istringstream, and use our old friend, the >> operator.
Just convert it yourself. Parse all the digits, one at a time, converting them to a single integer. This is a very good exercise. And since this is an unsigned value, there isn't even a negative number to worry about. I would think that this would be a standard homework assignment in any introductory computer science class. At least it was, back in my days.
you can use stoull:
char s[25] = "12345678901234567890"; // or: string s = "12345678901234567890";
uint64_t a = stoull(s);

Need help on padding hex and some conversions

I will briefly explain what I want to do and help appreciated.
I have a hex number which is formatted as 16 byte number like this:
1: std::string myhex = "00000000000000000000000000000FFD";
Then I want to convert it to int. Which I think I successfully do using this:
// convert hex to int
unsigned int x = strtoul(myhex.c_str(), NULL, 16);
printf("x = %d\n", x); // prints 4093 as needed
Now, I want to convert this integer back to hex. Which I think I also successfully do using this:
// Convert int back to hex
char buff[50];
string hexval;
sprintf(buff,"%x",x);
hexval = buff;
cout << hexval.c_str(); // prints "ffd".
But my problem is that now, I want to convert the "ffd" string as above back to the format it was before, e.g., 16 byte number padded with zeros like this:
00000000000000000000000000000FFD
I want to convert the string not only print it.
Any help how to do this?
Also any corrections if anything I was achieving above is wrong or not OK are welcome.
Preferably I would like this to compile on Linux also.
Use the 0 flag (prefix) for zero-padding and field width specification in a printf:
printf("%032X", x);
Use snprintf to store it in your string:
snprintf(buff, sizeof(buff), "%032X", x);
Or use asprintf to store it in a newly-allocated string, to be certain that the memory available for the string is sufficient (since it's allocated by asprintf):
char *as_string = NULL;
asprintf(&as_string, "%032X", x);

Converting a string into an integer parameter [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to convert a number to string and vice versa in C++
c++ - convert pointer string to integer
Is there a way to convert a string into an integer parameter without any big algorithms?
string = "100";
integerFunction(int string);
I've tried atoi functions and tried to manually convert each number over with the string[count] - 48 way but it needs to be in a way where the number of digits don't become a problem with this. Any suggestions or algorithms out there that can help? I really appreciate it.
Like this:
int StringToInt( const std::string & str )
{
std::stringstream ss(str);
int res = 0;
ss >> res;
return res
}

base 64 string to hexa string

How can i conver from base64 string to hexa string (i'm working in ubuntu - c++ code). My hexa string I would like to be like 0x0c....and so on. Need help. Can someone please give me an exaple?Thx!
A quick solution that uses common (though not standard) functions:
std::string input = MY_ENCODED_STRING;
unsigned long decoded_value = strtol(input.c_str(), NULL, 64);
char buffer[100] = {0};
std::string output = itoa(decoded_value, buffer, 16);
boost::lexical_cast may be able to provide a more elegant solution (not sure on that one, though).