Help For Convert String To Hex Byte

Hi everyone,
i have an a string i want to like on this sample whatever i tried i cant get it done please someone can help me?
String Numbers="123456"
i want to convert it like this
byte n1=0x01
byte n2=0x02
byte n3=0x03
byte n4=0x04
byte n5=0x05
byte n6=0x06
or like this also help me
byte numArray[]={0x01,0x02,0x03,0x04,0x05,0x06};

whatever i tried i cant get it like this
Because i want to write this string to serial like this
Serial.write(0x01);
Serial.write(0x02);
Serial.write(0x03);
Serial.write(0x04);
Serial.write(0x05);
Serial.write(0x06);

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

look this over
see ASCII chart

output:

    0    1    2    3    4    5    6
   48   49   50   51   52   53   54
 0x30 0x31 0x32 0x33 0x34 0x35 0x36
 0x00 0x01 0x02 0x03 0x04 0x05 0x06
void
dispBytes (
    const char *s )
{
    char t [5];
    int len = strlen (s);

    for (int n = 0; n < len; n++)  {
        sprintf (t, "    %c", s [n]);
        Serial.print (t);
    }
    Serial.println ();

    for (int n = 0; n < len; n++)  {
        sprintf (t, " %4d", s [n]);
        Serial.print (t);
    }
    Serial.println ();

    for (int n = 0; n < len; n++)  {
        sprintf (t, " 0x%02x", s [n]);
        Serial.print (t);
    }
    Serial.println ();

    for (int n = 0; n < len; n++)  {
        sprintf (t, " 0x%02x", s [n] - '0');
        Serial.print (t);
    }
    Serial.println ();
}


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

    String numbers  = "0123456";
    dispBytes (numbers.c_str());
}

void loop ()
{
}

Based on @gcjr and assuming the submitted string only contains valid Hex characters, I'd suggest this code :

void dispBytes(const char *s)
{
  int len = strlen(s);

  for(int n = 0; n < len; n++)  {
    if(s[n] <= '9') {
      Serial.write(s[n] - '0');
    }
    else if(s[n] >= 'a') {
      Serial.write(s[n] - 'a' + 10);
    }
    else {
      Serial.write(s[n] - 'A' + 10);
    }
  }
  Serial.println();
}


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

  String numbers  = "0123456";
  dispBytes(numbers.c_str());

  Serial.write(0x00);
  Serial.write(0x01);
  Serial.write(0x02);
  Serial.write(0x03);
  Serial.write(0x04);
  Serial.write(0x05);
  Serial.write(0x06);
  Serial.println();

  numbers  = "abcdef";
  dispBytes(numbers.c_str());
  Serial.write(0x0a);
  Serial.write(0x0b);
  Serial.write(0x0c);
  Serial.write(0x0d);
  Serial.write(0x0e);
  Serial.write(0x0f);
  Serial.println();

  numbers  = "ABCDEF";
  dispBytes(numbers.c_str());
  Serial.write(0x0A);
  Serial.write(0x0B);
  Serial.write(0x0C);
  Serial.write(0x0D);
  Serial.write(0x0E);
  Serial.write(0x0F);
}

void loop()
{
}

Of course, the result on an ASCII terminal is as it should : weird
Here on Wokwi : StringToHex V1 - Wokwi ESP32, STM32, Arduino Simulator

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.