Hi! I'm working on a project that requires a string of letters to be converted into a string of hex characters, transmitted, and then converted back into a string of letters. For example the string "text" turns into "74657874" and then back into "text". How would I go about doing this? Thanks!
for each c in "text"
sprintf(tmpBuf, "%d", (int)c);
strcat(buf, tmpBuf);
done
To reverse:
for each 'c', 'd' in "74657874"
memcpy(tmpBuf, address_of("74657874"), 2); //copy each pair
tempBuf[2] = '\0'; //terminated this string
int v = atoi(tmpBuf); //convert "74" to 74
sprintf(tmpBuf, "%c", (char)v); //put "t"
strcat(buf, tmpBuf);
done
@OP
If I have understood --
1. You have the following text message what you see on Serial Monitor-1 (SM-1) of Fig-1 (below):
text
Figure-1: Software UART Port based connection between two UNOs
2. You want the message (text) of Step-1 to take over the form like 74657874, and you want to see it on SM-1.
3. You want that the form (74657874) what you are seeing on SM-1 should be transferred to UNO-2 via hardware/software UART Port.
4. After the receiption of the codes of Step-3 by the UNO-2, you want to see the same form of code (74657874) on the Serial Monitor-2 (SM-2) of Fig-1.
5. You want to see this message: text on SM-2 corresponding to the code of Step-4.
arduino_new:
for each c in "text"
sprintf(tmpBuf, "%d", (int)c);
strcat(buf, tmpBuf);
done
To reverse:
for each 'c', 'd' in "74657874"
memcpy(tmpBuf, address_of("74657874"), 2); //copy each pair
tempBuf[2] = '\0'; //terminated this string
int v = atoi(tmpBuf); //convert "74" to 74
sprintf(tmpBuf, "%c", (char)v); //put "t"
strcat(buf, tmpBuf);
done
That crappy code should not be posted as a solution.
You convert each byte into one to three digits but assume that each byte is represented as two digits.
And you use decimal digits, not as requested hexadecimal digits.
If I don't misunderstand you want to pass from a string of digits, thar rapresent an hex based number to the number itself. Is it correct?
If the previuos thing is correct you can move like this:
First of all you have a number and a string (or String, but I prefer first one)
You have a function
At the biginning the number became 0
For (all the string)
If (the string [for] element is a digit (like isDigit (variable) or it's complete form))
Number =number16+variable-'0'
Else
Number=number16+variable-'A'
Can it be a solution?
1. OP has the text message -- text.
char myStr[5] = "text";
Serial.print(myStr); //Serial Monitor (SM-1) shows: text
2. OP wants to see 74657874 (the ASCII Codes for the charcaters of the text message) on SM-1..
(a) char myStr[5] = {0x74, 0x65, 0x78, 0x74, 0x00}; //already done in Step-1
(b) All the codes of the members of myStr[] must be brought together to get 74657874. So, apply atol() as suggested by Post#7.
unsigned long x = atol(myStr); // x = 74657874
Serial.println(x, HEX); //SM-1 shows: 74657874
3. OP wants to send the value of x (74657874) to another UART-based device (assume UNO-2).
(a) Convert the digits of x into their respective ASCII codes using ltoa() function.
char myBuffer[20]="";
ltoa(x, muBuffer, 16); //myBuffer[0:7] = {0x37, 0x34, 0x36, 0x35, 0x37, 0x38, 0x37, 0x34};
(b) Transmit the content of char myBuffer[]; using print() method.
mySUART.print(myBuffer); //data has gone to UNO-2 via software serial port (mySUART)
4. Op want to see 74657874 and the message text on Serial Monitor (SM-2) of the Receiver.
void loop()
{
if (mySUART.available() > 0)
{
char ch = mySUART.read();
Serial.print(ch); //SM-2 shows: 74657874
myBuffer[i] = ch;
i = i + 1;
if (i == 8)
{
Serial.println();
Serial.println(":Equivalent text for the received code is:");
for (int j = 0; j < 8; j = j + 2)
{
myBuffer[j] = (myBuffer[j] << 4) | (myBuffer[j + 1] & 0x0F);
}
myBuffer[1] = (myBuffer[2]);
myBuffer[2] = (myBuffer[4]);
myBuffer[3] = (myBuffer[6]);
myBuffer[4] = 0x00; //null-byte
Serial.print(myBuffer); //SM-2 shows: text
}
}
}
char input[] = "just a string";
char generated[sizeof(input) * 2 - 1];
char output[sizeof(input)];
char toNibble(char from) {
from &= 0xF;
return from < 10 ? from + '0' : from - 10 + 'A';
}
void toHex(const char* in, char* buff, uint8_t count) {
for (; count--; in++) {
*buff++ = toNibble(*in >> 4);
*buff++ = toNibble(*in & 0xF);
}
*buff = 0;
}
char fromNibble(char hex) {
return hex < 'A' ? hex - '0' : hex - 'A' + 10;
}
void fromHex(char* out, const char* buff, uint8_t count) {
while (count--) {
byte val = fromNibble(*buff++) << 4;
val |= fromNibble(*buff++);
*out++ = val;
}
}
void showVals() {
Serial.print(F("From: '"));
Serial.print(input);
Serial.print(F("' via '"));
Serial.print(generated);
Serial.print(F("' to '"));
Serial.print(output);
Serial.println(F("'"));
}
void setup() {
Serial.begin(250000);
toHex(input, generated, sizeof(input) - 1);
fromHex(output, generated, sizeof(input) - 1);
showVals();
}
void loop() {}
From: 'just a string' via '6A757374206120737472696E67' to 'just a string'
Delta_G:
Why are you converting it to ascii yourself first? The print function DOES THAT FOR YOU!!!!I don't understand why everyone is coming with these long convoluted solutions when all the OP needs if he wants to send a number as ascii is to call Serial.print with that number. print will send it as ascii. THERE'S NO REASON TO CONVERT IT FIRST!!!!
Yea! the command mySUART.print(74657874); could do the job; but, I have converted to ASCII to demonstrate the application of the opposite function of atol() -- the ltoa().
Delta_G:
mySUART.print(74657874);
No that would print the ascii representation of that number. OP wanted that which is ascii for "text". So he needs:
The command mySUART.print(74657874) does transmit the respective ASCII codes for the digits of the number 74657874. The outcomes of the said command are received and processed by the codes of 4. of Post#8 to retrieve 74657844 and the message text.
Delta_G:
But all he wants to do is send the word "text" from one Arduino to another.
You forgot the restriction that only characters that make up hexadecimal numbers (0-9A-F) are allowed in the transmission.
Delta_G:
Where was that restriction made? You made that up. OP never said that.
I read this in the very first post (and it seems most of the readers did).
garret3g:
I'm working on a project that requires a string of letters to be converted into a string of hex characters, transmitted, and then converted back into a string of letters.
There are special communications that impose some restrictions on the message content for one emails,
where binary content is passed via base64 encoding, hex encoding would be another less compact way.
It could be used as a simple hiding mechanism of otherwise directly readable text,
it could be used as suppression of blanks or other special chars in urls or the like,
or some for other obscure reason to send some char data as hex dump.