ex
buffer , this return FF but now i wanne have the dec value of FF = 255 how can i do that i tried long Ro = strtol(buffer*, NULL, 16);* but get error can someone bring me on the right direction
We PRINT values in decimal, hex, octal etc but the valuse never change they are values ( numbers) and there not in hex or dec thats just the way we write them
This seems like a simple question once things are understood. You might also want to ask in the language specific forums below to get help in your native language.
On an Arduino 'strtol' is an incredibly long and inefficient function. You can do this though:
inline byte hexToByte(char* hex){
byte high = hex[0];
byte low = hex[1];
if (high > '9'){
high -= (('A'-'0')-0xA); //even if hex is lower case (e.g. 'a'), the lower nibble will have the correct value as (('a'-'A')&0x0F) = 0.
}
if (low > '9'){
low -= (('A'-'0')-0xA); //even if hex is lower case (e.g. 'a'), the lower nibble will have the correct value as (('a'-'A')&0x0F) = 0.
}
return (((high & 0xF)<<4) | (low & 0xF));
}
inline unsigned int hexToInt(char* hex){
return ((hexToByte(hex) << 8) | hexToByte(hex+2));
}
inline unsigned long hexToLong(char* hex){
return ((((unsigned long)hexToInt(hex)) << 16) | hexToInt(hex+4));
}
Which can be called for example like this;
char buf[] = "FF";
byte number = hexToByte(buf);
//or
char bigbuf[] = "DEAD";
unsigned int largeNumber = hexToInt(bigbuf);
//or
char hugebuf[] = "DEADBEEF";
unsigned long hugeNumber = hexToLong(hugebuf);
receive this error
standv31:71: error: invalid conversion from 'char' to 'char*'
standv31:71: error: initializing argument 1 of 'unsigned int hexToInt(char*)'
this is the code where it happens
Serial.print("-");
Serial.print(buffer[i]);
Serial.print("-");
unsigned int largeNumber = hexToInt(buffer[i]);
Serial.print(largeNumber);
Serial.println("-");
standv31.ino: In function 'void play()':
standv31:77: error: invalid conversion from 'char' to 'char*'
standv31:77: error: initializing argument 1 of 'byte hexToByte(char*)'