1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5StampS3、ESP32の16進ヘキサ変換をためす。

Last updated at Posted at 2025-02-02

x 過去ログを見ょ!!!
x 3.1.1
x 「シリアルモニタ」は、なんどか開き直す(windows系)

結果

o_coq788.jpg

プログラム




//HEXBuilder_esp32_s3_2

#include <HEXBuilder.h>

void setup() {

  //シリアルの初期化 esp32-s3 usb-serial
  Serial.begin(9600);
  Serial.println();
  //シリアルの待ちが0.5*9
  for (int i = 0; i < 9; i++) {
    delay(500);  //接続待ち
    Serial.print('.');
  }  //for
  Serial.println();

  Serial.println("\n\n\nStart.");

  const unsigned char helloBytes[] = "Hello World"; //入力

  char buff1[256]; //一時領域
  size_t len1 = HEXBuilder::bytes2hex(buff1, sizeof(buff1), helloBytes, sizeof(helloBytes));

  Serial.printf("IN: <%s\\0>\nOUT <%s>\n", helloBytes, buff1); //表示

  Serial.println();

  // Convert a HEX string like 6c6c6f20576f726c64 to a binary buffer
  const char *hexin = "48656c6c6f20576f726c6400";  // As the string above is \0 terminated too  //入力

  unsigned char buff2[256]; //一時領域
  size_t len2 = HEXBuilder::hex2bytes(buff2, sizeof(buff2), hexin);

  // Safe to print this binary buffer -- as we've included a \0 in the hex sequence.
  Serial.printf("IN: <%s>\nOUT <%s\\0>\n", hexin, buff2);  //表示

  Serial.println("Done.");

}

void loop() {}



おまけ


#include <HEXBuilder.h>

void setup() {

  /*
    Serial.begin(115200);
  */

  //シリアルの初期化 esp32-s3 usb-serial
  Serial.begin(9600);
  Serial.println();
  //シリアルの待ちが0.5*9
  for (int i = 0; i < 9; i++) {
    delay(500);  //接続待ち
    Serial.print('.');
  }  //for
  Serial.println();

  Serial.println("\n\n\nStart.");

  // Convert a HEX string like 6c6c6f20576f726c64 to a binary buffer
  {
    const char *out = "Hello World";
    const char *hexin = "48656c6c6f20576f726c6400";  // As the string above is \0 terminated too

    unsigned char buff[256];
    size_t len = HEXBuilder::hex2bytes(buff, sizeof(buff), hexin);

    if (len != 1 + strlen(out)) {
      Serial.println("Odd - length 1 is wrong");
    }

    if (memcmp(buff, out, len) != 0) {
      Serial.println("Odd - decode 1 went wrong");
    }

    // Safe to print this binary buffer -- as we've included a \0 in the hex sequence.
    Serial.printf("IN: <%s>\nOUT <%s\\0>\n", hexin, buff);
  };

  {
    String helloHEX = "48656c6c6f20576f726c64";
    const char hello[] = "Hello World";

    unsigned char buff[256];
    size_t len = HEXBuilder::hex2bytes(buff, sizeof(buff), helloHEX);

    if (len != strlen(hello)) {
      Serial.println("Odd - length 2 is wrong");
    }

    if (strcmp((char *)buff, hello) != 0) {
      Serial.println("Odd - decode 2 went wrong");
    }
  }

  {
    const unsigned char helloBytes[] = {0x48, 0x56, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};
    String helloHEX = "48566c6c6f20576f726c64";

    String out = HEXBuilder::bytes2hex(helloBytes, sizeof(helloBytes));
    if (out.length() != 2 * sizeof(helloBytes)) {
      Serial.println("Odd - length 3 is wrong");
    }

    // we need to ignore case - as a hex string can be spelled in uppercase and lowercase
    if (!out.equalsIgnoreCase(helloHEX)) {
      Serial.println("Odd - decode 3 went wrong");
    }
  }

  {
    const unsigned char helloBytes[] = {0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};
    const char helloHex[] = "6c6c6f20576f726c64";

    char buff[256];
    size_t len = HEXBuilder::bytes2hex(buff, sizeof(buff), helloBytes, sizeof(helloBytes));
    if (len != 1 + 2 * sizeof(helloBytes)) {
      Serial.println("Odd - length 4 is wrong");
    }

    // we need to ignore case - as a hex string can be spelled in uppercase and lowercase
    if (strcasecmp(buff, helloHex)) {
      Serial.println("Odd - decode 4 went wrong");
    }
  }
  Serial.println("Done.");
}

void loop() {}

o_coq789.jpg

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?