What happens in a conversion from a char to an int?

14

How does it work when you get a variable and make char-48 to transform into an integer, for example in this code I made, I used a date for example "22/05/1994" stored in a char vector and turned it into day, month and year all in integer value. The fact happens in line 5 in the expression num[j]=data[i]-48 . What happens in this operation to happen the conversion?

void transformarDataEmInt(char *data, int *dia, int *mes, int *ano){
    int i,j=0;
    int num[8];
    for (i=0; i<10; i++) {
        num[j]=data[i]-48;
        if (i==2||i==5){
            continue;
        }
        j++;
    }
    *dia = num[0]*10 + num[1];
    *mes = num[2]*10 + num[3];
    *ano = num[4]*1000 + num[5]*100 + num[6]*10 + num[7];
}
    
asked by anonymous 01.02.2014 / 22:03

3 answers

17

What happens is that the type char in languages like C are actually integer types. In the Wikipedia article on C language types is the description of type char :

  

char - > smallest addressable unit of the machine that can contain   basic character set. It is an integer type. Actual type can be either   signed or unsigned, depending on the implementation.

That is, it is the smallest machine-addressable unit capable of containing the character set. This character set was standardized by ANSI being called #

In this way, when you have a char it actually stores the decimal value representing a character in that table. If you refer to the table link I referenced, you will see that the value 48 is used to represent the digit * zero ( '0' ).

The point of using the subtraction arithmetic operation is simply to easily convert the character to the value representing the digit (that is, convert the character '3' , for example, to value 3).

Assuming then:

char c = '3';
int i = c - 48;
printf("O valor de i é [%d]\n", i);

Produce the result:

O valor de i é [3]

Because, as per the ASCII table c = '3' = 51 , we have that c - 48 = 51 - 48 = 3 .

* Note: in the ASCII table of Wikipedia the character is displayed in the "Glyph" column.

    
01.02.2014 / 22:14
3

C characters are encoded in integers according to the ASCII ^ 1 pattern. As Victor pointed out in #, the ascii codes of the numeric characters are consecutive, starting with 48 for zero. So by subtracting 48 we transform the code from '0' into 0 , code from '1' to 1 , etc.

That being said, I would not write this code using the 48 written in the hand like that. First, you can write '0' instead of 48 to let the code intent clearer:

num[j]=data[i] - '0';

Secondly, the standard C library already has functions for converting strings into numbers. Using them, your code becomes simpler and more robust. For example, your current code will read wrong if the day or month has a single digit instead of two, you do not test to see if the digits actually are numbers, etc.

if(3 != sscanf("%d/%d/%d", data, dia, mes, ano)){
  /*tratar erro*/
}

^ 1 - In most implementations. Technically, the standard allows other encodings, such as EBDIC

    
01.02.2014 / 22:20
2

Here's the relevant part of the ASCII table:

Código     Caractere
48         0
49         1
50         2
51         3
52         4
53         5
54         6
55         7
56         8
57         9

Then, when you pick up the character and subtract 48, you are converting from character to number. When you add 48 you do the opposite.

    
01.02.2014 / 22:09