Take an AlphaNumeric string and output a Hex String array

I know this is a bit clunky and there are probably far better ways to do this out there but I made this simple version just to get people up and running without much programming knowledge under their belt.

This is basically a library I created to take an AlphaNumeric string and output a Hex String array.

#include <Wire.h>

void setup()
{
Wire.begin();

Serial.begin(9600);
char c[5] = “65AB”;
String outString0 = ASCII_Library(c[0]);
String outString1 = ASCII_Library(c[1]);
String outString2 = ASCII_Library(c[2]);
String outString3 = ASCII_Library(c[3]);

Serial.println(outString0);
Serial.println(outString1);
Serial.println(outString2);
Serial.println(outString3);

}

void loop()
{

}

String ASCII_Library(char c){
// INPUT 0-9 OR A-Z (CAPITALIZED)
// OUTPUT HEX
switch (c){
case 48:
return “0x30”; // CHR = 0
break;
case 49:
return “0x31”; // CHR = 1
break;
case 50:
return “0x32”; // CHR = 2
break;
case 51:
return “0x33”; // CHR = 3
break;
case 52:
return “0x34”; // CHR = 4
break;
case 53:
return “0x35”; // CHR = 5
break;
case 54:
return “0x36”; // CHR = 6
break;
case 55:
return “0x37”; // CHR = 7
break;
case 56:
return “0x38”; // CHR = 8
break;
case 57:
return “0x39”; // CHR = 9
break;
case 65:
return “0x41”; // CHR = A
break;
case 66:
return “0x42”; // CHR = B
break;
case 67:
return “0x43”; // CHR = C
break;
case 68:
return “0x44”; // CHR = D
break;
case 69:
return “0x45”; // CHR = E
break;
case 70:
return “0x46”; // CHR = F
break;
case 71:
return “0x47”; // CHR = G
break;
case 72:
return “0x48”; // CHR = H
break;
case 73:
return “0x49”; // CHR = I
break;
case 74:
return “0x4A”; // CHR = J
break;
case 75:
return “0x4B”; // CHR = K
break;
case 76:
return “0x4C”; // CHR = L
break;
case 77:
return “0x4D”; // CHR = M
break;
case 78:
return “0x4E”; // CHR = N
break;
case 79:
return “0x4F”; // CHR = O
break;
case 80:
return “0x50”; // CHR = P
break;
case 81:
return “0x51”; // CHR = Q
break;
case 82:
return “0x52”; // CHR = R
break;
case 83:
return “0x53”; // CHR = S
break;
case 84:
return “0x54”; // CHR = T
break;
case 85:
return “0x55”; // CHR = U
break;
case 86:
return “0x56”; // CHR = V
break;
case 87:
return “0x57”; // CHR = W
break;
case 88:
return “0x58”; // CHR = X
break;
case 89:
return “0x59”; // CHR = Y
break;
case 90:
return “0x5A”; // CHR = Z
break;
}

}

That is useless junk. Did you even test it? Did you ever stop to think why the last number is printed as zero?
It does not do what you claim it does:

  • it does not output hex
  • it does not output a string.
    And it's ugly beyond belief.
    You clearly don't know what your ASCII_Library() function actually does - which is absolutely nothing useful. You're also obviously not familiar with arrays and 'for' loops.
    Anyone "without much programming knowledge under their belt" would do well to use this only as an example of how not to do such a thing.

Pete

Thanks, a simple "that is incorrect" or something would have done. No need to be angry. :grin:

No need to be angry

I wasn’t.

a simple “that is incorrect” or something would have done

What would you have learned from that? It is not just incorrect. It is incorrect in many ways, several of which I pointed out. It also wouldn’t have helped any newbie who might think your code was something useful.
If you don’t like your mistakes being pointed out, don’t post mistakes.

Pete

P.S. I forgot to mention that you need to read how to post code properly and then post your code in [code] [/code] tags.
Note that you can edit one of your existing messages, and add a [code] tag at the front and a [/code] tag at the end of the code. It will make it much more readable and easy to copy and paste into the IDE for testing, and then people might be more inclined to help you in your other thread.

Pete

void printHex (uint8_t x)
{
  Serial.print (F("0x"));
  if (x < 16)
  {
    Serial.print (F("0"));
  }
  Serial.print ((int)x, HEX));
}