I want to convert string "6867" into string "hg" and output. 68 is the hexadecimal h and 67 is the hexadecimal g. How should this be achieved?
For example with sscanf().
Convert the numbers to integers, assuming they are hexadecimal numbers with "%2x".
See the format here: https://www.cplusplus.com/reference/cstdio/scanf/.
Then convert the integers to ASCII characters.
It is always 4 hexadecimal characters long ? Is it always 2 hexadecimal characters per character ?
Your string is not like a hexadecimal string, it is a string of decimal digits. You should clarify the format as advised above.
Maybe it should be a string like "68,67" or "68 67", otherwise itâs hard to figure out how youâll do the parsing of the string.
char inputString[] = "6867";
int HexDigit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return 0;
}
char HexByte(char *p)
{
char value = 0;
value += HexDigit(*p++);
if (*p != '\0')
{
value = value * 16 + HexDigit(*p);
}
return value;
}
void setup()
{
Serial.begin(115200);
delay(200);
for (unsigned i = 0; i < strlen(inputString); i += 2)
{
Serial.print(HexByte(&inputString[i]));
}
}
void loop() {}
Seems to be an unusual requirement. I can't help feeling that the original input was bytes that have been now 'printed' as decimal digits instead of being printed as HEX
Where does the inputString come from?
My SafeString library lets you print( ,HEX) to a char[ ] by wrapping the char [ ] in a SafeString.
The hex numeric format shown isnât unusual at all⌠look at âIntelâ HEX files that have been around since the 70s.
As long as the data is being served in a âknownâ, expected representation, you can handle it any way that works and is reliable / repeatable.
First of all thank you for your help, what I use in my code is
String a = "686700af", it is not of type char inputString[], what should I do if it is converted to type char inputString[]?
ok
it can be â68 67âďźso how to transform it?
If itâs 6867âŚ., you need to extract each pair of two characters.
If itâs arriving as 68 67 .. .., you could use strtok() to separate the fields (using any delimiters), or if youâre sure itâs always two digits with a comma, you could play with fields of three characters and ignore the comma, but careful if the input if isnât tagged with a comma at the end !
const char *inputString = a.c_str();
BUT. that gives you access to the char [ ] inside the inputString object so you should not change it, but @johnwasser code just accesses the chars without changing them so that would be OK
Is it like this?
IDE reports conflicting declaration'const char* inputString'
#include<stdio.h>
#include<string.h>
String a = "680f00af";
char inputString[9];
const char *inputString = a.c_str();
int HexDigit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return 0;
}
char HexByte(char *p)
{
char value = 0;
value += HexDigit(*p++);
if (*p != '\0')
{
value = value * 16 + HexDigit(*p);
}
return value;
}
void setup()
{
Serial.begin(9600);
}
void loop() {
delay(1000);
for (unsigned i = 0; i < strlen(inputString); i += 2)
{
Serial.print(HexByte(&inputString[i]));
}
}
No you need to do the
const char *inputString = a.c_str();
inside the loop() / setup() code.
and delete the char inputString[9];
String a = "680f00af"; has already created a char[9] for you, automagically, and saved it inside the String object a.
a.c_str() just gives you access to it. Actually gives you access to a const char * to the first element.
Also be aware if you add chars to String a, the char* inputString may not longer point to anything valid so if you going to use this approach, get the c_str() pointer and process it immediately before doing anything else.
Ohhh, I deleted char inputString[9]; it works, thank you very much
Not a good idea to have inputString as a global. If you change the String a, inputString can become invalid so keep it as a local like
void loop() {
// .. other code here
{ // start a local block
const char *inputString = a.c_str();
// do stuff here with inputString but do not change it
} // inputString disappears here at the end of this code block
// rest of loop here
}
Ok thanks, i will do this later
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.