// convert a hex string such as "A489B1" into an array like [0xA4, 0x89, 0xB1].
const byte MaxByteArraySize = 5;
const char aa[] = "\xE5";
const char ae[] = "\xE6";
const char oe[] = "\xF8";
const char a[] = "\x41";
int vals[] = {
230, // æ
248, // ø
229, // å
198, // Æ
216, // Ø
197, // Å
};
int val = 'a';
const char txt[] = "my string";
char *symbols[64] = {nullptr};
void setup() {
Serial.begin(115200);
for (int x : vals) {
char c = (char)x;
const char str[] = {(char)x, '\0'};
Serial.print(x); Serial.print(" / "); Serial.print(x, HEX);
Serial.print(" -> "); Serial.print(str);
Serial.print(" / "); Serial.println(c);
}
delay(2000);
int index = 0;
symbols[index++] = aa;
symbols[index++] = aa;
symbols[index++] = ae;
symbols[index++] = oe;
symbols[index++] = a;
symbols[index++] = oe;
for (int i = 0; i<index; i++)
Serial.print(i),
Serial.print(" "),
Serial.print(symbols[i]),
Serial.print(" "),
Serial.println(symbols[i], HEX);
/*
char *index, *ptr;
ptr = txt; Serial.print("ptr: '"); Serial.print(ptr); Serial.println("'"); delay(2000);
int len = strlen(ptr);
index = (ptr + (--len)); *index = '\0';
Serial.print("ptr: '"); Serial.print(ptr); Serial.println("'"); delay(2000);
*/
}
void loop() {
}
/*
void setup()
{
Serial.begin(115200);
byte byteArray[MaxByteArraySize] = {0};
hexCharacterStringToBytes(byteArray, "C3A5");
dumpByteArray(byteArray, MaxByteArraySize);
hexCharacterStringToBytes(byteArray, "A489B10");
dumpByteArray(byteArray, MaxByteArraySize);
}
void hexCharacterStringToBytes(byte *byteArray, const char *hexString) {
bool oddLength = strlen(hexString) & 1;
byte currentByte = 0;
byte byteIndex = 0;
for (byte charIndex = 0; charIndex < strlen(hexString); charIndex++) {
bool oddCharIndex = charIndex & 1;
if (oddLength) {
// If the length is odd
if (oddCharIndex) {
// odd characters go in high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else {
// Even characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
else {
// If the length is even
if (!oddCharIndex) {
// Odd characters go into the high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else {
// Odd characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
}
}
void dumpByteArray(const byte * byteArray, const byte arraySize) {
for (int i = 0; i < arraySize; i++) {
Serial.print("0x");
if (byteArray[i] < 0x10)
Serial.print("0");
Serial.print(byteArray[i], HEX);
Serial.print(", ");
}
Serial.println();
}
byte nibble(char c) {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 0; // Not a valid hexadecimal character
}
void loop() {
if( Serial.available()) {
char buf[8];
int n = Serial.readBytesUntil('\n', buf, sizeof(buf)-1); buf[n] = '\0';
unsigned long hex = strtol(buf, 0, 10);
Serial.print("buf: "); Serial.println(buf);
Serial.print("hex: "); Serial.println(hex);
}
//byte byteArray[MaxByteArraySize] = {0};
//hexCharacterStringToBytes(byteArray, "C3A5");
//dumpByteArray(byteArray, MaxByteArraySize);
}
*/
/*
unsigned char rxBuf[2]= { 0xC3, 0xA5 };
char ReceString[32];
void setup() {
Serial.begin( 9600);
sprintf(ReceString, "\\\\x%02X%02X", (unsigned int)rxBuf[0], (unsigned int)rxBuf[1]);
Serial.println(ReceString);
}
void loop() {
if( Serial.available()) {
Serial.print( "You have entered: ");
delay(100); // allow the rest of the line to be received.
while( Serial.available()) {
byte c = Serial.read();
if( c != '\r' && c != '\n') {
if( c <= 0x0F)
Serial.print( "0");
Serial.print( c, HEX);
Serial.print( ", ");
}
}
Serial.println();
}
delay(1);
}
*/
/*
unsigned char rxBuf[8]= { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22 };
unsigned long rxId = 0x103;
char ReceString[32];
void setup()
{
Serial.begin( 9600);
}
void loop()
{
sprintf(ReceString, "%03lX %02X%02X%02X%02X%02X%02X%02X%02XX",
rxId,
(unsigned int) rxBuf[0],
(unsigned int) rxBuf[1],
(unsigned int) rxBuf[2],
(unsigned int) rxBuf[3],
(unsigned int) rxBuf[4],
(unsigned int) rxBuf[5],
(unsigned int) rxBuf[6],
(unsigned int) rxBuf[7]);
ReceString[3]=7+'0';
Serial.println(ReceString);
Serial.println(rxBuf[0]);
delay(1000);
}
*/