Converting single character string to integer

void setup() {
  Serial.begin(9600);
  char string[] = "A\r\n"; // 'A' with carriage return and newline
  int num = string[0];
  Serial.print(string);
  Serial.println(num);
}

void loop() {
}

No one has suggested switch/case.

How are all the answers you have received so far not working for you? So many answers... pick one.

Followup to this? How do I send any byte as a single ascii character?

can you post an example of your input?
use single quotes to represent ASCII characers; dbl quotes for strings

do you mean

    char c = Serail.read();
    if (c == '3')
        doSomething();

1.
toInt() works on String-type character string. For example:

  String myMsg2 = "12";
  int value = myMsg2.toInt();
  Serial.println(value);  //shows 12

2.
To get the ASCII value of a character (for example: A), we can execute --

  String s1 = "A123c";
  char ch1 = s1.charAt(0);  //A
  Serial.print(ch1, HEX);  //shows: 41 = ASCII value of A

I've opted for if - else, finding that the switch/case needs integers

Does not.

char something = 'b';
void setup() {
  Serial.begin(115200);
  switch (something) {
    case 97: Serial.println("val a"); break;
    case 'b': Serial.println("char b"); break;
    default: break;
  }
}
void loop() {}

Why will you not show what you are trying to do?

actually needs something that can be promoted to an int. So a char, or an enum is fine for example

no, cases can be ASCII chars as i showed before. They can also be the integer value of an ASCII char

static int  val = 0;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0'...'9':
            val = c - '0' + (10 * val);
            break;

        case 'A':
            analogPin = val;
            Serial.print   ("analogPin = ");
            Serial.println (val);
            val = 0;
            break;

        case 'D':
            debug ^= 1;
            break;

The spec states that the controlling expression of a switch statement must be of integral type or enumeration type. This includes char , bool , all signed and unsigned integer types, and enum / enum class values. (so Floating-point types, pointers, and standard library strings are not permitted as the switch expression for example)

Yes. This can be shown here, by the compiler error showing how it views characters:

char something = 'a';
void setup() {
  Serial.begin(115200);
  switch (something) {
    case 97: Serial.println("val a"); break;
    case 'a': Serial.println("char a"); break;
    default: break;
  }
}
void loop() {}

Cheers

Yes an enum I think would be an alternative also

you're overthinking this

Okay, but I think this is the last time I ask, then I'll give up trying to help you: can you show us what values ​​we're talking about with some examples, and maybe even show us your code (even just portions of code, as long as you tell us exactly what this request is for)?

char a = 'a';
int ascii = (int) a;

This is unnecessary, as others have said. You can do things like:

char cleartext = 'a';
char cyphertab[] = "mnopqrstuvwxyzabcdefghijkl";

encoded = cyphertab[cleartext];
// or
alsoEncoded = (cleartext + 13);
if (alsoEncoded > 'z')
  alsoEncoded = alsoEncoded - 26;

In that case your topic title is misleading as it implies that you want the numeric value of the single character rather than the ASCII value

what's the difference? 33 vs '!" '!'

image

   32 0x20     48 0x30 0   64 0x40 @   80 0x50 P   96 0x60 `  112 0x70 p
   33 0x21 !   49 0x31 1   65 0x41 A   81 0x51 Q   97 0x61 a  113 0x71 q
   34 0x22 "   50 0x32 2   66 0x42 B   82 0x52 R   98 0x62 b  114 0x72 r
   35 0x23 #   51 0x33 3   67 0x43 C   83 0x53 S   99 0x63 c  115 0x73 s
   36 0x24 $   52 0x34 4   68 0x44 D   84 0x54 T  100 0x64 d  116 0x74 t
   37 0x25 %   53 0x35 5   69 0x45 E   85 0x55 U  101 0x65 e  117 0x75 u
   38 0x26 &   54 0x36 6   70 0x46 F   86 0x56 V  102 0x66 f  118 0x76 v
   39 0x27 '   55 0x37 7   71 0x47 G   87 0x57 W  103 0x67 g  119 0x77 w
   40 0x28 (   56 0x38 8   72 0x48 H   88 0x58 X  104 0x68 h  120 0x78 x
   41 0x29 )   57 0x39 9   73 0x49 I   89 0x59 Y  105 0x69 i  121 0x79 y
   42 0x2a *   58 0x3a :   74 0x4a J   90 0x5a Z  106 0x6a j  122 0x7a z
   43 0x2b +   59 0x3b ;   75 0x4b K   91 0x5b [  107 0x6b k  123 0x7b {
   44 0x2c ,   60 0x3c <   76 0x4c L   92 0x5c \  108 0x6c l  124 0x7c |
   45 0x2d -   61 0x3d =   77 0x4d M   93 0x5d ]  109 0x6d m  125 0x7d }
   46 0x2e .   62 0x3e >   78 0x4e N   94 0x5e ^  110 0x6e n  126 0x7e ~
   47 0x2f /   63 0x3f ?   79 0x4f O   95 0x5f _  111 0x6f o  127 0x7f 
char s [90];

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

    for (int i = ' '; i < (' ' +16); i++)  {
        for (int j = 0; i+j < 128; j += 16)  {
            char c = i+j;
            sprintf(s, "  %3d 0x%02x %c", c, c, c);
            Serial.print (s);
        }
        Serial.println ();
    }
}

void loop () {}

Some might say

the numeric value of the character '1' is 1.

and then ask about getting the numeric value of other characters not '0' … '9'.

They would be in need of some extra learning around the entire matter.

Until you know this like you know your nose, it can be a bit of a mystery.

Of course any character has a value, '!' is 33 and '*' is 42.

This same little kind of thing happens when noobs ask about converting hex to decimal. What are they talking about? The number in a byte, for example, can be expressed in any base, typically decimal and hexadecimal, no conversion is necessary.

Oh, BCD, whyantcha say so?

a7

The difference is that 33 is an integer and the other is a String in the context of the OP's original question and those are certainly not equal

If we were talking about the difference between 33 and '!' then they would be equal, at least for some functions

As is often the case there is confusion in this topic as to what is really required and references to both strings and Strings don't help

you said single character