Converting int to char in C

后端 未结 7 535
不知归路
不知归路 2021-01-18 03:55

Right now I am trying to convert an int to a char in C programming. After doing research, I found that I should be able to do it like this:

int value = 10;
c         


        
7条回答
  •  滥情空心
    2021-01-18 04:18

    Characters use an encoding (typically ASCII) to map numbers to a particular character. The codes for the characters '0' to '9' are consecutive, so for values less than 10 you add the value to the character constant '0'. For values 10 or more, you add the value minus 10 to the character constant 'A':

    char result;
    if (value >= 10) {
        result = 'A' + value - 10;
    } else {
        result = '0' + value;
    }
    

提交回复
热议问题