Dear Forum,
I am looking for a way to convert a string with hex characters inside into readable ASCII.
For example the hex string would be 6167653d41726b68616d and the expected output is age=Arkham
I tried some suggestions i found onlie but struggle with the dynamic size of the string as well as the conversion into one string with the message in the end.
Does anyone know a awy?
Thanks so much and have a great weekend
Is the original data in a String or a C style string ?
Show us the code you have so far.
(in code tags)
make a minimal sketch which shows which variable types you are using and what you plan with the new value after having it as ASCII.
edit:
by the way, quite interesting to read what the forum comes up with:
In og. String format
"6167653d41726b68616d"
I am on the go atm
But can show some code but the code itself is hug
gcjr
August 17, 2024, 11:36am
6
isn't a hex string already ascii?
th18231a23:
6167 653d 4172 6b68 616d
there are 10 bytes. what data type is this? array of bytes?
look this over
output
x = 0x1a2b4
z = 6167653d41726b68616d
long x = 0x01a2b4;
byte z [] = { 0x61, 0x67, 0x65, 0x3d, 0x41, 0x72, 0x6b, 0x68, 0x61, 0x6d };
const int Zlen = sizeof(z);
char s [90];
// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin (9600);
sprintf (s, "x = 0x%lx", x);
Serial.println (s);
int j = 0;
for (int i = 0; i < Zlen; i++, j += 2)
sprintf (& s[j], "%02x", z [i]);
s [j] = '\0';
Serial.print ("z = ");
Serial.println (s);
}
void loop (void) { }
like this you mean?
void setup() {
char hex_str[]="6167653d41726b68616d";
char char_str[(strlen(hex_str)+2)/2]={0};
int i=0,j=0;
Serial.begin(115200);
for(i=0; i < strlen(hex_str); i+=2){
char temp[3] ={0};
memcpy(temp,&hex_str[i],2);
char_str[j++]=strtol(temp, NULL, 16);
}
Serial.print("Hex string: ");
Serial.println(hex_str);
Serial.print("\nCoverted Hex string: ");
Serial.println(char_str);
}
void loop() {
// put your main code here, to run repeatedly:
}
Output:
Hex string: 6167653d41726b68616d
Coverted Hex string: age=Arkham
hope that helps....
thank you for the suggestion.
The issue is I cant get my string (got it in a variable of type String) to a char [] as it can't know the size:
Compilation error: initializer fails to determine size of 'hex_str'
i tried putting the length of the String in to it but get the error
Compilation error: array must be initialized with a brace-enclosed initializer
so I put {} around it and get error
Compilation error: cannot convert 'String' to 'char' in initialization
this is the last one char hex_str[strURI.length()]= {strURI};
yes I did:
char convertStringURI = strURI.c_str();
char hex_str[]= convertStringURI;
Compilation error: invalid conversion from 'const char*' to 'char' [-fpermissive]
also tried
char* convertStringURI = strURI.c_str();
char hex_str[]= convertStringURI;
Compilation error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
That's the point.
If YOU had posted a short demo sketch as suggested, you might have gotten an example with a String object not strings...
here's an example using c_str()
void setup() {
char *str_p;
String Str = "this is a string";
str_p = &Str.c_str()[0];
Serial.begin(115200);
Serial.println(str_p);
Serial.println(strlen(str_p));
}
void loop() {
// put your main code here, to run repeatedly:
}
I let you combine this with code in post #7 !
hope that helps...
I can do a shortened version:
String strURI ="";
strURI = "6167653d41726b68616d"; //example value as value comes from a nfc card and is written to the strURI pair by pair
really don't know what more I should add as code.
The whole code is a mess and huge as I need to clean-up the nfc card functions (reader)
do you want me to post all the code?
no.
I just wanted you to do the ground work
String hexToAscii( String hex )
{
uint16_t len = hex.length();
String ascii = "";
for ( uint16_t i = 0; i < len; i += 2 )
ascii += (char)strtol( hex.substring( i, i+2 ).c_str(), NULL, 16 );
return ascii;
}
void setup() {
Serial.begin(115200);
String strURI ="";
strURI = "6167653d41726b68616d";
String out= hexToAscii(strURI);
Serial.println(out);
}
void loop() {
// put your main code here, to run repeatedly:
}
will output
age=Arkham
by the way, the function comes from another thread, you could have found if you followed the forum search.
Forget it, he is convinced that he really need to use Strings, so I give up trying to dissuade him and wrote a function in another topic he made (as he is french like me). I think this is what he is looking for:
String hexToAscii( String hex )
{
uint16_t len = hex.length();
String ascii = "";
for ( uint16_t i = 0; i < len; i += 2 )
ascii += (char)strtol( hex.substring( i, i+2 ).c_str(), NULL, 16 );
return ascii;
}
1 Like
Hi sherzaad,
I posted the code one to one in a new sketch and same error:
Compilation error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
I use Wokwi and did not get any errors for the share example I did....
did you try MY example code from post 12 ?
Maybe its related to the esp32 I use? but shouldnt make a diffrence?
I tried wokwi with your code:
ifi-scan.ino: In function 'void setup()':
wifi-scan.ino:5:11: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
5 | str_p = &Str.c_str()[0];
| ^~~~~~~~~~~~~~~
| |
| const char*
Error during build: exit status 1
void setup()
{
Serial.begin(9600);
byte z [] = { 0x61, 0x67, 0x65, 0x3d, 0x41, 0x72, 0x6b, 0x68, 0x61, 0x6d };
for (int i = 0; i < sizeof z; i++)
{
Serial.print((char)z[i]);
}
}
void loop() {}
Output:
age=Arkham
ah... so ur using an ESP32....
I tested with a UNO...
fixed it for esp32!
void setup() {
String hex_S = "6167653d41726b68616d";
const char *hex_str;
hex_str = &hex_S.c_str()[0];
char char_str[(strlen(hex_str)+2)/2]={0};
int i=0,j=0;
Serial.begin(115200);
for(i=0; i < strlen(hex_str); i+=2){
char temp[3] ={0};
memcpy(temp,&hex_str[i],2);
char_str[j++]=strtol(temp, NULL, 16);
}
Serial.print("Hex string: ");
Serial.println(hex_str);
Serial.print("\nCoverted Hex string: ");
Serial.println(char_str);
}
void loop() {
// put your main code here, to run repeatedly:
}
https://wokwi.com/projects/406480084365072385
1 Like