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).
Related
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);
I'm working with a ATmega328p and Arduino System in a Project, I'm trying to get the saved data in 2 spaces of a eeprom memory and concat it. I always ask to google and check examples but this time I give up.
byte dataEE = readEEPROM(disk1,space);
I normally use strcat, but in this case I cand find the way to convert Byte type to char*
char * strcat ( char * destination, const char * source );
My system is working right now with String to solve this. but a I'd like to know a more efficient way, I always read that we must avoid the String.
String TimeData = String(readEEPROM(disk1,space1)) + String(readEEPROM(disk1,space2));
Maybe something like this:
char TimeData[3];
TimeData[0] = (char) readEEPROM(disk1,space1);
TimeData[1] = (char) readEEPROM(disk1,space2);
TimeData[2] = 0;
Now you can use TimeData wherever you would normally use a null-terminated char* string.
I need to write 16-bit integers to a file. fstream only writes characters. Thus I need to convert the integers to char - the actual integer, not the character representing the integer (i.e. 0 should be 0x00, not 0x30) I tried the following:
char * chararray = (char*)(&the_int);
However this creates a backwards array of two characters. The individual characters are not flipped, but the order of the characters is. Thus I created this function:
char * inttochar(uint16_t input)
{
int input_size = sizeof(input);
char * chararray = (char*)(&input);
char * output;
output[0]='\0';
for (int i=0; i<input_size; i++)
{
output[i]=chararray[input_size-(i+1)];
}
return output;
}
This seems slow. Surely there is a more efficient, less hacky way to convert it?
It's a bit hard to understand what you're asking here (perhaps it's just me, although I gather the commentators thought so too).
You write
fstream only writes characters
That's true, but doesn't necessarily mean you need to create a character array explicitly.
E.g., if you have an fstream object f (opened in binary mode), you can use the write method:
uint16_t s;
...
f.write(static_cast<const char *>(&s), sizeof(uint16_t));
As others have noted, when you serialize numbers, it often pays to use a commonly-accepted ordering. Hence, use htons (refer to the documentation for your OS's library):
uint16_t s;
...
const uint16_t ns = htons(s);
f.write(static_cast<const char *>(&ns), sizeof(uint16_t));
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);
I'm writing the websocket server on C++ and having a problem with calculating the Sec-WebSocket-Accept key. I've tested sha1 and it generates the right value (I've taken the string from wikipedia example and got the same hash) and I also found the online base64 converter to test the second part, and it seems working right. But as I can see from other issues, my string is too long.
std::string secKey = readHeader(buffer,n,"Sec-WebSocket-Key");
std::string magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
secKey.append(magic);
unsigned char hash[20];
char hexstring[41];
sha1::calc(secKey.c_str(),secKey.length(),hash);
sha1::toHexString(hash,hexstring);
std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(hexstring) ,strlen(hexstring));
For secKey = "4aRdFZG5uYrEUw8dsNLW6g==" I get encoded value "YTYwZWRlMjQ4NWFhNzJiYmRjZTQ5ODI4NjUwMWNjNjE1YTM0MzZkNg=="
I think you can skip the toHexString line and base64 encode the 20 byte hash instead.
sha1::calc(secKey.c_str(),secKey.length(),hash);
std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(hash), 20);
That's what my C++ server does and handshakes complete successfully.