Convert char to int in C and C++

前端 未结 12 1975
梦毁少年i
梦毁少年i 2020-11-22 02:41

How do I convert a char to an int in C and C++?

相关标签:
12条回答
  • 2020-11-22 02:59

    Use static_cast<int>:

    int num = static_cast<int>(letter); // if letter='a', num=97
    

    Edit: You probably should try to avoid to use (int)

    int num = (int) letter;

    check out Why use static_cast<int>(x) instead of (int)x? for more info.

    0 讨论(0)
  • 2020-11-22 03:01

    C and C++ always promote types to at least int. Furthermore character literals are of type int in C and char in C++.

    You can convert a char type simply by assigning to an int.

    char c = 'a'; // narrowing on C
    int a = c;
    
    0 讨论(0)
  • 2020-11-22 03:07

    Presumably you want this conversion for using functions from the C standard library.

    In that case, do (C++ syntax)

    typedef unsigned char UChar;
    
    char myCppFunc( char c )
    {
        return char( someCFunc( UChar( c ) ) );
    }
    

    The expression UChar( c ) converts to unsigned char in order to get rid of negative values, which, except for EOF, are not supported by the C functions.

    Then the result of that expression is used as actual argument for an int formal argument. Where you get automatic promotion to int. You can alternatively write that last step explicitly, like int( UChar( c ) ), but personally I find that too verbose.

    Cheers & hth.,

    0 讨论(0)
  • 2020-11-22 03:08

    char is just a 1 byte integer. There is nothing magic with the char type! Just as you can assign a short to an int, or an int to a long, you can assign a char to an int.

    Yes, the name of the primitive data type happens to be "char", which insinuates that it should only contain characters. But in reality, "char" is just a poor name choise to confuse everyone who tries to learn the language. A better name for it is int8_t, and you can use that name instead, if your compiler follows the latest C standard.

    Though of course you should use the char type when doing string handling, because the index of the classic ASCII table fits in 1 byte. You could however do string handling with regular ints as well, although there is no practical reason in the real world why you would ever want to do that. For example, the following code will work perfectly:

      int str[] = {'h', 'e', 'l', 'l', 'o', '\0' };
    
      for(i=0; i<6; i++)
      {
        printf("%c", str[i]);
      }
    

    You have to realize that characters and strings are just numbers, like everything else in the computer. When you write 'a' in the source code, it is pre-processed into the number 97, which is an integer constant.

    So if you write an expression like

    char ch = '5';
    ch = ch - '0';
    

    this is actually equivalent to

    char ch = (int)53;
    ch = ch - (int)48;
    

    which is then going through the C language integer promotions

    ch = (int)ch - (int)48;
    

    and then truncated to a char to fit the result type

    ch = (char)( (int)ch - (int)48 );
    

    There's a lot of subtle things like this going on between the lines, where char is implicitly treated as an int.

    0 讨论(0)
  • 2020-11-22 03:12

    It sort of depends on what you mean by "convert".

    If you have a series of characters that represents an integer, like "123456", then there are two typical ways to do that in C: Use a special-purpose conversion like atoi() or strtol(), or the general-purpose sscanf(). C++ (which is really a different language masquerading as an upgrade) adds a third, stringstreams.

    If you mean you want the exact bit pattern in one of your int variables to be treated as a char, that's easier. In C the different integer types are really more of a state of mind than actual separate "types". Just start using it where chars are asked for, and you should be OK. You might need an explicit conversion to make the compiler quit whining on occasion, but all that should do is drop any extra bits past 256.

    0 讨论(0)
  • 2020-11-22 03:12

    For char or short to int, you just need to assign the value.

    char ch = 16;
    int in = ch;
    

    Same to int64.

    long long lo = ch;
    

    All values will be 16.

    0 讨论(0)
提交回复
热议问题