Converting a String to HEX array

Hello! I am trying to convert a String to an array of HEX. For example I have a String s = "Hello World", I want a function where I can pass the string and returns an array of HEX equivalent as such, array = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64}.

I tried using this code but it doesn't convert with the "0x" at the begining, plus the array is inverted.

Any idea?

What does that mean?

Please post your code, on code tags.

You could print the "0x" very simply.

Please take a moment and read the first 4 posts here:

They're pinned for your convenience, and following their guidance will help you get a lot more help from us.

But I don't want to print it. I wanted to be in the array.
That is the code:

#include <Arduino.h>


void stringToHex(){

  String s1 = "TP-Link_DB0";
  int hex_dec;
  String output;

  Serial.println();
  Serial.print("string: ");
  Serial.println(s1);
  Serial.println("hexval: ");

  for (const auto &item : s1) {

    hex_dec = int(item);
    char hexaDeciNum[100];
    int i = 0;

    while (hex_dec != 0) {
      int temp = 0;
      temp = hex_dec % 16;
      if (temp < 10) {
        hexaDeciNum[i] = temp + 48;
        i++;
      }
      else {
        hexaDeciNum[i] = temp + 55;
        i++;
      }
      hex_dec = hex_dec / 16;
    }//54 50 2D 4C 69 6E 6B 5F 44 42 30
    for (int j = i - 1; j >= 0; j--){
      // hexaDeciNum[j] = "0x" + hexaDeciNum[j];
      output += hexaDeciNum[j];
    }
  }
  Serial.print(output);

}


void setup(void)
{
  Serial.begin(9600);
  stringToHex();
}

void loop(void){

}

Umm, do you mean reversed? That's obvious in the code, so you can fix that pretty easy, no?

Not sure what you mean here.
All in all, your not describing your problem, you're picking nits with someone else's code. Try telling us again what it is you want - I think it's "an array containing the hexadecimal equivalent of the characters in a string", but that's so simple it can't be, can it?
C

That is what I want in simple words.

@camsysca I wanna also be able to retunr the array

Trick is, your "characters" are "hexadecimal numbers". So what "conversion" needs doing? Or are you asking to transfer each character in the string into a value in the array?

Exactly! For example "Hello world" will be passed to the function and the function returns an array as such array = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64} where 0x48 is the "H", 0x65 is the "e" and so on...

That is a little bit tricky in C. You have to return a pointer to the array, and the array has to be in some persistently allocated memory, because memory allocated by a function is released when it returns.

What do you need this for? There is probably an easier way to do the unspecified task that the code that you have requested, does.

Is this (simply an image on which no arithmetic is possible) what you want to see for the string "Hello World" or any other string?

 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64

In the following sketch, I have done simple manipulation:

char inArray[] = "Hello World";
//char outArray[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64}.
char outArray[80] = {0};
int x = 0;

void setup()
{
  Serial.begin(9600);
  for (int j = 0; j < sizeof inArray - 1; j++)
  {
    store(inArray[j], x);  // 0x48,
    x = x + 6;
  }
  outArray[x-1] = ' ';
  Serial.println(outArray);
}

void loop()
{

}

void store(char y, int x)
{
  outArray[x] = ' ';
  outArray[x + 1] = '0';
  outArray[x + 2] = 'x';
  if ((y >> 4) <= 9)
  {
    outArray[x + 3] = (y >> 4) + '0';
  }
  else
  {
    outArray[x + 3] = (y >> 4) + '7';
  }
  if ((y & 0x0F) <= 9)
  {
    outArray[x + 4] = (y & 0x0F) + '0';
  }
  else
  {
    outArray[x + 4] = (y & 0x0F) + '7';
  }
  outArray[x + 5] = ',';
}

I want to encrypt the data using AES algorithm which requires an array of HEX. I could avoid the function but it will make the code pretty long.

You can do integer to hex string conversion with strtoul(). It won't add the '0x' but you can concatenated that yourself. Same thing with the ', '.

it seems you do not understand something... Aes algorithm does not require "array of HEX"

in C++ "array of HEX" or "array of chars" or "array of decimals" not more than different representations of the same data and not need any transformation at all

all data is binary. HEX is simply one way of displaying it (e.g. binary, octal decimal, ascii, pixels)

 68 65 6C 6C 6F 20 77 6F 72 6C 64 0
 0 64 6C 72 6F 77 20 6F 6C 6C 65 68
const char s [] = "hello world";

void setup ()
{
    Serial.begin (9600);

    for (unsigned i = 0; i < sizeof(s); i++)  {
        Serial.print (" ");
        Serial.print (s[i], HEX);
    }
    Serial.println ();

    for (int i = (int)sizeof(s)-1; i >= 0; i--)  {
        Serial.print (" ");
        Serial.print (s[i], HEX);
    }
    Serial.println ();

}

void loop ()
{
}

Let's get down to the nitty gritty. What data type does the AES software expect as input? Can we please see some detail about that? Are you calling a function in some AES support module? Details...

Yeah but I don't want to print it I want an array holding these values.

your array already contains those values

1 Like

all values are stored as binary bits. binary, octal, hex, decimal, ascii describe how to display those bits, not how they are stored.

see How Computers Store Data

1 Like

answer post #16, please. You do yourself no favors by ignoring requests for details that shed light on your real needs.