Convert hex string to int in Crystal - crystal-lang

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)

Related

wcstombs producing unexpected output

I am trying to convert a wchar_t* to char* using wcstombs on Linux(CentOS7 to be specific). The current system encoding is UTF-8 and the return value of wcstombs confirms that all the characters are converted. However, when I print the resulting char* string, I see unexpected values. I am pasting the sample code below. TRACE1 is a custom macro to print values.
int length;
int size = wcstombs(NULL,wideString,0);
TRACE1("number of bytes needed for conversion :-------> %d" , size);
char str[size+1];
length = wcstombs(str, wideString, size+1);
TRACE1("characters converted :-------> %d" , length);
if(length == size)
{
str[size] = '\0';
}
After the conversion I was expecting the char* string to have characters in ascii character set as the wideString has only ascii characters. However, I see non ascii characters in the converted char* string. wideString is something that I receive as return value from .net code and I am receiving it as a 'wchar_t' to keep it uniform across Linux and windows.I am not sure if that can have an impact here?
I have tried setting the locale to the default locale and it didn't help me. Any help is greatly appreciated. I am pasting sample wide string and output string below for reference.
Below is how the original wide character string looks like
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6
And this is how the converted string looks like
ü°<92>§¥¥ý©<90><95>¡¥ý<8b><92>¦¥<8f>ý©<94><93><85><96>ý¨<92>¤<8d><8c>ý©<98>´<9d>¢ý<93><92>¦¥<8f>ü±<92><97>©<95>ý³<92><96>¥<8e>ü±<99>¶¹<89>ü¶<92><94><8d>¤ý<94><9c><86>µ<89>

Arduino C++ sprintf() print "?" [duplicate]

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!

Windows Universal app (XAML): textBlock->Text cannot be called with the given argument list

I'm trying to set the textBlock equal to the result of some calculations, but for some reason i'm getting the following error: "cannot be called with the given argument list" total is an int.
string Result;
ostringstream convert;
convert << total;
Result = convert.str();
textBlock->Text = Result;
The error message means that you are passing a parameter of a wrong type to the textBlock's Text property, which expects a Platform::String, but you pass a std::string. The MSDN page Strings(C++/CX) contains more details on string construction and conversions - also you need to be aware of ANSI and UNICODE when dealing with strings.
Below is the modified code. Noted that I have changed string to wstring (wide string, 16-bit Unicode) so that I can construct a Platform:String with it.
wostringstream convert;
convert << total;
wstring str = convert.str();
String^ Result = ref new String(str.c_str());
tb1->Text = Result;

Double decimal number convert to HEX string

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"

Arduino sprintf float not formatting

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!