A char array (32 bytes) contains the following comma-separated data as an example: f9,43.65,27.25,11.0 0,33,37,C.
The first two characters are hex data. Hence the array contains a hex value, three floats, two integers and a character with a dot.
How can these be extracted from this array (ie as in "removed"), and stored in a variable as hex values without the comma?
strtol()
And if you really want to mess with floats strtof()
Use strtok() to first break up the string at the comma delimiters.
septillion:
strtol()And if you really want to mess with floats strtof()
No, it is just the first two hex characters that are needed, no need for float in the extract; I do some experiments with strtol()
gfvalvo:
Use strtok() to first break up the string at the comma delimiters.
The initial array needs to be preserved for further use but without the first two hex characters and without the first comma.
How certain are you of the structure of the string?
Will there always be two hex digits at the start? 01? 0B? 6A?, FF?
If so, just grab them manually:
.
.
.
bh = ConvertCharToHex( inString[0] );
bl = ConvertCharToHex( inString[1] );
hexval = (bh << 4) + bl;
Serial.print( "Hex = 0x" ); Serial.println( hexval, HEX );
.
.
.
byte ConvertCharToHex( char value )
{
value = toupper(value);
if( value >= 'A' )
value = value - 'A' + 10;
else
value = value - '0';
return value;
}
strtol() does not remove characters from the char array. In this case the char array needs its first 2 characters including the first comma to be removed and stored as 2 hex values without the comma.
brice3010:
The initial array needs to be preserved for further use but without the first two hex characters and without the first comma.
brice3010:
How can these be extracted from this array (ie as in "removed"), and stored in a variable as hex values without the comma?
What you meant by "these" was ambiguous. So, as @septillion said, strtol() will do everything you need:
void setup() {
char string[] = "f9,43.65,27.25,11.0 0,33,37,C.";
char *remainder;
uint16_t firstValue;
Serial.begin(115200);
delay(1000);
firstValue = strtol(string, &remainder, 16);
Serial.println(firstValue, HEX);
remainder++;
Serial.print(remainder);
}
void loop() {
}
Output:
F9
43.65,27.25,11.0 0,33,37,C.
gfvalvo:
What you meant by "these" was ambiguous. So, as @septillion said, strtol() will do everything you need:void setup() {
char string[] = "f9,43.65,27.25,11.0 0,33,37,C.";
char *remainder;
uint16_t firstValue;
Serial.begin(115200);
delay(1000);
firstValue = strtol(string, &remainder, 16);
Serial.println(firstValue, HEX);
remainder++;
Serial.print(remainder);
}
void loop() {
}
Output:
F9
43.65,27.25,11.0 0,33,37,C.
Thanks gfvalvo and I am sorry for the ambiguity I caused. I will experiment with your code tomorrow morning: this is exactly what I am looking for.
The purpose here is to use these 2 hex values to compare with the calculated XOR value from the remainder of the char array. Since the initial hex values are the result of the same calculation on the initial values of the char array, an integrity check is performed.
Blackfin:
How certain are you of the structure of the string?Will there always be two hex digits at the start? 01? 0B? 6A?, FF?
If so, just grab them manually:
.
.
.
bh = ConvertCharToHex( inString[0] );
bl = ConvertCharToHex( inString[1] );
hexval = (bh << 4) + bl;
Serial.print( "Hex = 0x" ); Serial.println( hexval, HEX );
.
.
.
byte ConvertCharToHex( char value )
{
value = toupper(value);
if( value >= 'A' )
value = value - 'A' + 10;
else
value = value - '0';
return value;
}
Yes there always will be 2 hex characters followed by a comma at the start of the char array.
gfvalvo:
What you meant by "these" was ambiguous. So, as @septillion said, strtol() will do everything you need:void setup() {
char string[] = "f9,43.65,27.25,11.0 0,33,37,C.";
char *remainder;
uint16_t firstValue;
Serial.begin(115200);
delay(1000);
firstValue = strtol(string, &remainder, 16);
Serial.println(firstValue, HEX);
remainder++;
Serial.print(remainder);
}
void loop() {
}
Output:
F9
43.65,27.25,11.0 0,33,37,C.
There may be an issue: the total length of the char array may vary. Sorry for my reply to Blackfin when I wrote that the structure will always be the same. In fact the size of some characters will vary. For example there may be a 6 between two commas but just as well a 15 or 40 in the case of the characters representing an integer
There may be an issue: the total length of the char array may vary.
That doesn't matter as long as the first two characters and comma are present.