This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

chat to int

Hi a question about converting a char to an int

As a character takes values ​​in Table I can convert ascii int = char-48; example 0 = '0 '-48;

while (i <4) {
if (IFG2 & UCA0RXIFG) {
data [i] = UCA0RXBUF;
UCA0TXBUF = data [3];
i + +;
}
}
The data value [3] = 0 but as character and I need it as int.

My question is do as I am thinking or is there a library to convert from char to int  thanks.

  • I think you are asking "how to best convert ASCII representation of a number digit into a raw binary number".

    my_int = 0;
    
    if ((data >= '0') && (data <= '9'))
    {
      my_int = (int)(data - '0');
    }

  • hi thank 

    my_int = (int)(data - '0');

    with that line you do so the char is converted to an integer?
  • Yes. In the example, data is a char, '0' is a char, so the math is done as char (a byte). Then the result is cast to type int and assigned to my_int which is of type int.

    The assignment my_int = 0 is to handle the out-of-bounds condition that the if statement is testing for.

  • Brian, you correctly identified (and answered) the real question.
    However, there still seems to be some misunderstanding, so I’ll answer the original one:

    A “char” is a value from 0 to 255 or from -128 to +127. It has an 8 bit size.

    A “character” is a letter or symbol, that is represented by a number. Usually, it is interpreted by the ASCII character set, where 32 means a space, 65 stands for an ‘a’ etc. For the processor, these are only numbers.

    Turning a char into an int is automatically done by the compiler. An unsigned char will seamlessly turn into a signed or unsigned int or long int or float. A signed char will also be silently converted into a signed int or long int, or  afloat. Trying to turn a signed char into an unsigned int should give a compiler warning.

    Turning a character into an integer value requires interpreting/decoding the value that represents the character by the used character table. For numbers this is easy, as a zero digit character is represented by the value 48, the one by 49 and so on. The compiler automatically turns a character constant into a char when enclosing the character in single quotes:
    ‘0’ is equal to 48 or 0x30.
    So Brian’s suggestion to subtract ‘0’ from data means subtracting the value 48 from the value of the character you received. Which leaves you with 0 for ‘0’, 1 for ‘1’ etc.
    However, this conversion is done digit by digit. It won’t turn the character sequence “123” into the value 123 but rather into three separate values 1,2 and 3.


    To transform character sequences into values, the C standard library offers the function scanf and atoi.
    Both functions require a ‘string’, a sequence of characters stored in a char array, terminated with a zero value.
    However, this is plain C and not related to the MSP430.

     Besides this, the code posted will always return data[3], even f there is nothing received into data[3] yet.

**Attention** This is a public forum