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

Asked

Viewed 8,488 times

15

How it works when you take a variable and do char-48 To transform in integer, as for example in this code I made, I used a date for example "22/05/1994" stored in a char vector and transformed in day, month and year all in integer value. The fact happens on 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];
}

3 answers

19


What happens is that the type char in languages like C are actually integer types. No Wikipedia article on language types C this is the description of the 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 unit addressable by the machine capable of containing the character set. This character set was standardized by ANSI being called ASCII table (acronym for American Standard Code for Information Interchange).

That way, when you have one char it actually stores the decimal value that represents 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 (i.e., to convert the character '3', for example, at 3).

Supposing then:

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

Produces the result:

O valor de i é [3]

For, as according to the ASCII table c = '3' = 51, one has to c - 48 = 51 - 48 = 3.

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

  • 1

    Very good explanation, thank you :D

  • 5

    Just to complement: C does not necessarily use the ASCII table. Thus 48 is not always '0', but '0' is always '0'. So you’d rather write data[i] - '0'.

  • Another point to note is that this works because whoever created the ASCII table had the sensitivity to leave the numeric characters in order. Otherwise the conversion would have to be at the base of the switch-case. Also, it is always good to arrive if the character is between '0' and '9' before making the conversion, just to be sure.

  • 2

    @C.E.Gesser In C the characters '0' to '9' are always in order, regardless of whether ASCII is used or not. By standard (session 5.2.1/3): "In Both the source and Execution basic Character sets, the value of each Character after 0 in the above list of decimal digits Shall be one Greater than the value of the Previous."

  • That’s what I meant. I spoke of the ASCII table just for being the most basic and known.

3

C characters are encoded in integers according to ASCII 1 standard. As Victor pointed out in responsible for him, the ascii codes of the numeric characters are consecutive, starting with 48 to zero. Therefore, subtracting 48 we transform the code of the '0' in 0, the code of '1' in 1, etc..

That said, I wouldn’t write that code using the 48 written in hand like this. First, you can write '0' instead of 48 to make the intention of the code clearer:

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

Second, 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 one digit only instead of two, you do not test to see if the digits really 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

  • +1 Nice suggestion to make clearer with the use of '0'. :)

2

Here is 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

So, by taking the character and subtracting 48, you are converting from character to number. By adding 48 you do the opposite.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.