Hi guys i'm trying to use two int values and make a long Hex value i have sensorValue0 = 255 & sensorValue1 = 50 what i need is to add them both together in hex like this ( FF32 )
with the following code i get
// = 63ce this one is just 25550 in hex
// = 25550 this one is on the right track but not in HEX ( FF32 )
How can i get this to work then store the value ( FF32 ) for later use?
String string1;
#include <string.h>
char res[5];
int sensorValue0 = 255; // = FF HEX
int sensorValue1 = 50; // = 32 HEX
void setup() {
Serial.begin(19200);
String string1 = String ();
Serial.println("on");
}
void loop() {
sprintf(&res[0], "%04x", sensorValue0*100 + sensorValue1);
Serial.println(res); // = 63ce
string1 = String(sensorValue0*100 + sensorValue1);
Serial.println(string1); // = 25550
while(true);
}
:o :o :o :o :o :o :o :o :o :o :o :o
system
August 1, 2015, 8:25pm
2
sprintf(&res[0], "%04x", sensorValue0*100 + sensorValue1);
They are HEX values. The multiplier is 256, not 100.
thanks Paul it now gives me ff32 now how can i use ff32 convert into 65330 DEC
void loop() {
sprintf(&res[0], "%04x", sensorValue0*256 + sensorValue1);
Serial.println(res); // = ff32 HEX
tt = res ; // ?
Serial.println(tt); // ff32 HEX = 65330 DEC
while(true);
}
I tryed this but the output is
ff32
0
not
ff32
65330
char res[5];
int sensorValue0 = 255; // = FF HEX
int sensorValue1 = 50; // = 32 HEX
int tt = 0;
void setup() {
Serial.begin(19200);
String string1 = String ();
Serial.println("on");
}
void loop() {
sprintf(&res[0], "%04x", sensorValue0*256 + sensorValue1);
Serial.println(res); // = ff32 HEX
int tt = atoi(&res[5]);
Serial.println(tt); // ff32 HEX = 65330 DEC
while(true);
}
system
August 1, 2015, 9:01pm
5
String string1 = String ();
Create a useless instance of the String class that immediately goes out of scope. Why?
If 0xFF32 is really 65330, how realistic is your expectation that that will fit in an int?
how can i convert ff32 and store the value 65330, ? if int cant handle that length of numbers ?
You don't want atoi, but rather strtol. Convert string (base 16) to long integer
http://www.cplusplus.com/reference/cstdlib/strtol/
You don't need to go through a hex string conversion when you can just bit shift and combine the two integers into a long.
void setup()
{
Serial.begin(9600);
int sensorValue0 = 255; // = FF HEX
int sensorValue1 = 50; // = 32 HEX
char hex_string[5];
sprintf(hex_string, "%04x", sensorValue0 * 256 + sensorValue1);
Serial.println(hex_string);
long val = strtol(hex_string, NULL, 16);
Serial.println(val);
Serial.println("bitCombineValue");
long bitCombineValue = ((long)sensorValue0 << 8) | (long)sensorValue1;
Serial.println(bitCombineValue);
}
void loop()
{
}
system
August 1, 2015, 9:46pm
9
You don't want atoi, but rather strtol.
It's really pointless to convert two bytes to a string, and then convert the string to a long.
long val = sensorValue0 * 256 + sensorValue1;
long val = sensorValue0 * 256 + sensorValue1;
sensorValue0 *256 goes out of range
long val = (long)sensorValue0 * 256 + sensorValue1;