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!
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!
char c;
int array[10][10];
while( !plik.eof())
{
getline( plik, text );
int string_l=text.length();
character_controler=false;
for(int i=0; i<string_l; ++i)
{
c=napis.at(i);
if(c==' ') continue;
else if(character_controler==false)
{
array[0][nood]=0;
cout<<"nood: "<<nood<< "next nood "<<c<<endl;
array[1][nood]=atoi(c); // there is a problem
character_controler=true;
}
else if(c==',') character_controler=false;
}
++nood;
}
I have no idea why atoi() doesn't work. The compiler error is:
invalid conversion from `char` to `const char*`
I need to convert c into int.
A char is already implicitly convertible to an int:
array[1][nood] = c;
But if you meant to convert the char '0' to the int 0, you'll have to take advantage of the fact that the C++ standard mandates that the digits are contiguous. From [lex.charset]:
In both the
source and execution basic character sets, the value of each character after 0 in the above list of decimal
digits shall be one greater than the value of the previous.
So you just have to subtract:
array[1][nood] = c - '0';
atoi() expects a const char*, which maps to a c string as an argument, you're passing a simple char. Thus the error, const char* represents a pointer, which is not compatible with a char.
Looks like you need to convert only one character to a numeric value, and in this case you can replace atoi(c) by c-'0', which will give you a number between 0 and 9. However, if your file contains hexadecimals digits, the logic get a little bit more complicated, but not much.
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);
Is it possible to display a single dimensional array of values using SetWindowsText() in a text box on windows api?
for example. SetWindowText(hwndStatic3, sArray);
******************EDIT************
I have a textbox on the windows api where I use GetWindowText() to retrieve the string written in the text box then I convert the string to decimal array. I then convert this decimal array value to hexadecimal value as I am trying to print those values using SetwindowsText within another textbox. However only the last value of the array is printing. How can I print all the values?
******************EDIT************
code:
GetWindowText(hwndtext1, value, 256);
for (i = 15; i >= 0; i--)
{
temp[i] = atoll(value); //converts sting to decimal
ulltoa(temp[i] , sArray, 16); //converts decimal to hexadecimal
buf[i] = temp[i];
}
SetWindowText(hwndStatic3, sArray);
SetWindowText is just a macro with signature:
BOOL SetWindowText(HWND, const TCHAR*);
Depending on your build settings, it will call one of the following:
BOOL SetWindowTextA(HWND, const char*); //ansi version
BOOL SetWindowTextW(HWND, const wchar_t*); //unicode version
where TCHAR is defined as:
#ifdef _UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif
So, an array of strings is not compatible with SetWindowText but an array of characters will work, provided that the array is of type TCHAR *, or of type (char * or wchar_t *) that is compatible with your settings.
First, atoll and ulltoa aren't documented with the Microsoft Visual C/C++ (which is what I use for Windows) so I'm working from documentation I found online. Either your versions do more than those I've found documented, or you've left out some significant code from your example.
Based on the loop control, I'm guessing that you expect to always find 15 values in the string you read from the first control. BUT... the atoll and ulltoa functions only operate on one value at a time and do nothing to advance through the input list. So your loop is converting the first number from string to 64 bit int and then converting that into a string 15 times.
Since you say the last value is the only one you see, your functions must actually be parsing the value string in some way that is not apparent in your example. However, ulltoa seems to always be placing the value into the same place in the same string variable, with each subsequent call in the loop overwriting the previous call. My lazy self would add a bit like this:
int len = 0;
char szOutput[15*20]; // enough space for 15 64 bit hex strings
GetWindowText(hwndtext1, value, 256);
for (i = 15; i >= 0; i--)
{
temp[i] = atoll(value); //converts sting to decimal
ulltoa(temp[i] , sArray, 16); //converts decimal to hexadecimal
buf[i] = temp[i];
len += sprintf( szOutput+len, "%s ", sArray );
}
szOutput[len-1] - '\0'; // remove the final space
SetWindowText(hwndStatic3, szOutput);
Of course, with the sprintf you could also skip the ulltoa call entirely and change the sprintf line to:
len += sprintf( szOutput+len, "%16.16I64X", temp[i] );
(or whatever flavor/form of the hex output you want (see the printf format documentation for details.) If you want your list to be one item per line, then replace the trailing space with a newline. Oh, the I64 in the %16.16I64X is a Microsoft thing that might be different in other compilers/libraries.
FYI, the sprintf technique I used lets the function keep appending to the end of the buffer but incrementing the offset into the buffer (len) by the length of the string just appended, which is the value returned by sprintf. It is a quick and easy way to assembling string lists such as yours.
I don't see this an option in things like sprintf().
How would I convert the letter F to 255? Basically the reverse operation of conversion using the %x format in sprintf?
I am assuming this is something simple I'm missing.
char const* data = "F";
int num = int(strtol(data, 0, 16));
Look up strtol and boost::lexical_cast for more details and options.
Use the %x format in sscanf!
The C++ way of doing it, with streams:
#include <iomanip>
#include <iostream>
#include <sstream>
int main() {
std::string hexvalue = "FF";
int value;
// Construct an input stringstream, initialized with hexvalue
std::istringstream iss(hexvalue);
// Set the stream in hex mode, then read the value, with error handling
if (iss >> std::hex >> value) std::cout << value << std::endl;
else std::cout << "Conversion failed" << std::endl;
}
The program prints 255.
You can't get (s)printf to convert 'F' to 255 without some black magic. Printf will convert a character to other representations, but won't change its value. This might show how character conversion works:
printf("Char %c is decimal %i (0x%X)\n", 'F', 'F', 'F');
printf("The high order bits are ignored: %d: %X -> %hhX -> %c\n",
0xFFFFFF46, 0xFFFFFF46, 0xFFFFFF46, 0xFFFFFF46);
produces
Char F is decimal 70 (0x46)
The high order bits are ignored: -186: FFFFFF46 -> 46 -> F
Yeah, I know you asked about sprintf, but that won't show you anything until you do another print.
The idea is that each generic integer parameter to a printf is put on the stack (or in a register) by promotion. That means it is expanded to it's largest generic size: bytes, characters, and shorts are converted to int by sign-extending or zero padding. This keeps the parameter list on the stack in sensible state. It's a nice convention, but it probably had it's origin in the 16-bit word orientation of the stack on the PDP-11 (where it all started).
In the printf library (on the receiving end of the call), the code uses the format specifier to determine what part of the parameter (or all of it) are processed. So if the format is '%c', only 8 bits are used. Note that there may be some variation between systems on how the hex constants are 'promoted'. But if a value greater thann 255 is passed to a character conversion, the high order bits are ignored.