R
R
Rodge2017-11-24 19:32:40
css
Rodge, 2017-11-24 19:32:40

How to convert char array to int array?

There is an array char K that "takes out" numbers from the file (considering that the file may contain less than 100 numbers), but I have another array that consists of numbers from 0 to 9. The task is to compare these arrays and find out what number is not in the K array. For now, I just replace the same numbers with 10. And then, when outputting through if, I check that if 10 is not output and vice versa. (everything is clear here) But since char are characters , and int numbers do not add up as we would like. I tried to do this, but everything is even:
char K[99];

for(int x = 0; x < 10; x++)
    {
        for (int i = 0; i < 100; i++)
        {
            if(L[x] == int(K[i]))
            {
                L[x] = 10;
            }
        }
    }

How can I solve this kind of problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sergio_nsk, 2017-11-24
@sergio_nsk

1. char K[100];
2. if(L[x] + '0' == int(K[i]))

R
res2001, 2017-11-24
@res2001

1. If char is an integer type, you can directly, without explicit type casting, compare with int.
In your case, it does not store a character, but the ASCII code of the character, i.e. just an integer. But with the same success it is possible to write there any other number included in the range of possible values:
char c = 10;
2. Type casting in C style looks like this: (int)K[i]
3. To convert an ASCII code to a number, use the option suggested by sergio_nsk
or K[i] - '0'
or you can assign character codes directly to the L array, and not numbers from 0-9 (and it doesn't matter that the array is int, not char)
L[0] = '0';
L[1] = '1';
Then you can compare without transformations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question