I want to convert a decimal number(17592186044416) to hex string in MFC. AndI have tried to use
code 1:
double ftw = 0;
CString str;
str.Format(_T("%X"), ftw); //this will always be 0 in HEX
code 2:
char t1[100];
_itoa_s(ftw, t1, 16);// this will give me 80000000 in HEX
It seems like the str.Format(_T("%X"), ftw); and _itoa_s(ftw, t1, 16); function has the limit. Is there any other command that I can use to get the 17592186044416 to hex string?
Thank you.
You can cast it to a 64-bit integer and then format using the I64 qualifier in your format string:
double ftw = 17592186044416.0;
CString str;
str.Format(_T("%I64X"), (__int64)ftw);
For the example above, str will have the string "100000000000"
Related
I have this arduino sketch,
char temperature[10];
float temp = 10.55;
sprintf(temperature,"%f F", temp);
Serial.println(temperature);
temperature prints out as
? F
Any thoughts on how to format this float? I need it to be a char string.
Due to some performance reasons %f is not included in the Arduino's implementation of sprintf(). A better option would be to use dtostrf() - you convert the floating point value to a C-style string, Method signature looks like:
char *dtostrf(double val, signed char width, unsigned char prec, char *s)
Use this method to convert it to a C-Style string and then use sprintf, eg:
char str_temp[6];
/* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
dtostrf(temp, 4, 2, str_temp);
sprintf(temperature,"%s F", str_temp);
You can change the minimum width and precision to match the float you are converting.
As has been stated before Float support is not included in sprintf on Arduino.
String class
Arduino has its own String class.
String value = String(3.14);
then,
char *result = value.c_str();
String class reference, link above
Constructs an instance of the String class. There are multiple versions that construct Strings from different data types (i.e. format them as sequences of characters), including:
a constant string of characters, in double quotes (i.e. a char array)
a single constant character, in single quotes
another instance of the String object
a constant integer or long integer
a constant integer or long integer, using a specified base
an integer or long integer variable
an integer or long integer variable, using a specified base
a float or double, using a specified decimal places
I've struggled for a few hours on getting this right, but I did finally. And this uses modern Espressif C++ provided by Platformio, and my target MCU is an ESP32.
I wanted to display a prefix label, the float/int value, then the unit, all inline.
I can't relay on seperate Serial.print() statements, as I am using an OLED display.
Here's my code example:
int strLenLight = sizeof("Light ADC: 0000");
int strLenTemp = sizeof("Temp: 000.0 °C");
int strLenHumd = sizeof("Humd: 00.0 %");
char displayLight[strLenLight] = "Light ADC: ";
char displayTemp[strLenTemp] = "Temp: ";
char displayHumd[strLenHumd] = "Humd: ";
snprintf(strchr(displayLight, '\0'), sizeof(displayLight), "%d", light_value);
snprintf(strchr(displayTemp, '\0'), sizeof(displayTemp), "%.1f °C", temperature);
snprintf(strchr(displayHumd, '\0'), sizeof(displayHumd), "%.1f %%", humidity);
Serial.println(displayLight);
Serial.println(displayTemp);
Serial.println(displayHumd);
Which displays:
Light ADC: 1777
Temp: 25.4 °C
Humd: 55.0 %
dtostrf() is deprecated, and it doesn't exist on every board core platforms.
On the other hand, sprintf() doesn't format floats on AVR platforms!
I want to convert an hex string value in a variable to int.
This works for a literal: "#{0xFF}".to_i
But with a var...?
Not working test (interpolation error):
_myvar = "FF"
"#{0x_myvar}".to_i
Why not check the API documentation for String
"FF".to_i(16)
I have this arduino sketch,
char temperature[10];
float temp = 10.55;
sprintf(temperature,"%f F", temp);
Serial.println(temperature);
temperature prints out as
? F
Any thoughts on how to format this float? I need it to be a char string.
Due to some performance reasons %f is not included in the Arduino's implementation of sprintf(). A better option would be to use dtostrf() - you convert the floating point value to a C-style string, Method signature looks like:
char *dtostrf(double val, signed char width, unsigned char prec, char *s)
Use this method to convert it to a C-Style string and then use sprintf, eg:
char str_temp[6];
/* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
dtostrf(temp, 4, 2, str_temp);
sprintf(temperature,"%s F", str_temp);
You can change the minimum width and precision to match the float you are converting.
As has been stated before Float support is not included in sprintf on Arduino.
String class
Arduino has its own String class.
String value = String(3.14);
then,
char *result = value.c_str();
String class reference, link above
Constructs an instance of the String class. There are multiple versions that construct Strings from different data types (i.e. format them as sequences of characters), including:
a constant string of characters, in double quotes (i.e. a char array)
a single constant character, in single quotes
another instance of the String object
a constant integer or long integer
a constant integer or long integer, using a specified base
an integer or long integer variable
an integer or long integer variable, using a specified base
a float or double, using a specified decimal places
I've struggled for a few hours on getting this right, but I did finally. And this uses modern Espressif C++ provided by Platformio, and my target MCU is an ESP32.
I wanted to display a prefix label, the float/int value, then the unit, all inline.
I can't relay on seperate Serial.print() statements, as I am using an OLED display.
Here's my code example:
int strLenLight = sizeof("Light ADC: 0000");
int strLenTemp = sizeof("Temp: 000.0 °C");
int strLenHumd = sizeof("Humd: 00.0 %");
char displayLight[strLenLight] = "Light ADC: ";
char displayTemp[strLenTemp] = "Temp: ";
char displayHumd[strLenHumd] = "Humd: ";
snprintf(strchr(displayLight, '\0'), sizeof(displayLight), "%d", light_value);
snprintf(strchr(displayTemp, '\0'), sizeof(displayTemp), "%.1f °C", temperature);
snprintf(strchr(displayHumd, '\0'), sizeof(displayHumd), "%.1f %%", humidity);
Serial.println(displayLight);
Serial.println(displayTemp);
Serial.println(displayHumd);
Which displays:
Light ADC: 1777
Temp: 25.4 °C
Humd: 55.0 %
dtostrf() is deprecated, and it doesn't exist on every board core platforms.
On the other hand, sprintf() doesn't format floats on AVR platforms!
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);
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