String of hex values to hex integer

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!

To send text as ascii codes just use Serial.print(). To read it on the other end use Serial.read(). Anything that derives from Print will print text as ascii like that. That's the only way text ever gets sent anywhere.

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.

And it's not even compilable either. Looks like he got some python mixed in there.

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=number
16+variable-'A'

Can it be a solution?

Silente:
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=number
16+variable-'A'

Can it be a solution?

It could be, but why write out all that code when :

Serial.print(theNumber);

will do it for you?

Read the OP, he just wants to send it as ascii and reassemble. On the other end there is again a one line function already built in for this. Why reinvent the wheel? Use atol or atoi depending on the size of the number.

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
    }
  }
}

Divide et impera.

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'
  1. 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.

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!!!!

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().

mySUART.print(74657874);

No that would print the ascii representation of that number. OP wanted that which is ascii for "text". So he needs:

Serial.print("text");

and exactly what he said he wanted to go out over the line will go.

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.

GolamMostafa:
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.

That isn't what the OP asked for. Why are you inventing new problems to solve? Why not stick to what the OP wanted? He wanted to transmit the ascii for "text" from one arduino to another and receive it. He just needs Serial.print and Serial.read. He doesn't realize that the ascii conversion is automatic.

On neither end does he want that long 8 digit number. That was what he imagines would go out over the wire between the two. And if he uses print then that's what he will get.

Seriously guys, the guy asked to send "text" from one Arduino to another. You guys are getting on this red herring where he mentioned ascii in the OP. But all he wants to do is send the word "text" from one Arduino to another.

Do any of you really believe that all this stuff is needed to send the word "text" from one arduino to another? Why are you guys all converting numbers to ascii? What does that have to do with any of the OP's question?

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.

Whandall:
You forgot the restriction that only characters that make up hexadecimal numbers (0-9A-F) are allowed in the transmission.

Where was that restriction made? You made that up. OP never said that.

You can't send those digits anyway. You can only send binary. He chose to write it out in HEX. That's a pretty common way to write it.

You mean you think he wanted to turn "test" into an ascii representation and then get the ascii representation of the ascii representation and send that by serial? That would be pretty redundant wouldn't it?

When you type Serial.print("test") then 0x74657874 goes out over the serial line and on the other side it gets read as ascii and put back together to be "text". That is EXACTLY what the OP asked for.

Why in the world would anyone want to turn it into ascii and then make ascii of the ascii only to have to do a double conversion on the other end? What possible purpose could that serve?

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.