Thread: java to c.

1. java to c.

hey everybody, ive just started studying the language, im familiar with java so people told me its relatively easy... well, i have couple of questions if someone is willing to help.
in this example:
for(n=0,.......)
n=10*n+(s[i]-'0');
why is it written s[i]-'0'? what is the meaning behind it?
and another question is there any way to transfer an int array in base 10 to base 2? and if there is what is the process?
tnx for any help.

2. why is it written s[i]-'0'? what is the meaning behind it?
s is an array, and s[i] gives us the the value of the element at index i in array.
'0' is a character representation of the number zero.
This character is represented as an ascii code(which is really just a number) which is why we can subtract the character '0'.
The ascii-code of '0' is the decimal number 48, so they might just as well have written:
s[i] - 48.

3. Originally Posted by radxxx
hey everybody, ive just started studying the language, im familiar with java so people told me its relatively easy... well, i have couple of questions if someone is willing to help.
in this example:
for(n=0,.......)
n=10*n+(s[i]-'0');
why is it written s[i]-'0'? what is the meaning behind it?
and another question is there any way to transfer an int array in base 10 to base 2? and if there is what is the process?
tnx for any help.
s[i] - '0' is the standard idiom for converting a char in the 0 to 9 range, to a digit of a number. The char '9' becomes the number 9 by subtracting '0' from it.

10n + s[i] - '0' could be the conversion from a 2D array, (10 X ??) to a 1D array. Same as 10*row + column.

One way is to take the highest power of 2 that is less than your base 10 number, and divide into it. Store this intermediate result. Repeat that, storing all the intermediate results, until you have either a 1 or 0 left over. Now add all the intermediate results together, using base 2 rules:

So if the base 10 number was 11:

11 == it's 2^3 (in a loop, 2 * 2 * 2, get it?) or 100, with 3 remainder

100 == 2^3rd
+011 (3 remainder) (you can repeat this same process with the remainder, to get the 011.
====
111 in base 2 == 11 in base 10.

4. So if the base 10 number was 11:

11 == it's 2^3 (in a loop, 2 * 2 * 2, get it?) or 100, with 3 remainder

100 == 2^3rd
+011 (3 remainder) (you can repeat this same process with the remainder, to get the 011.
====
111 in base 2 == 11 in base 10.
The principle is right, but you got a little mistake.
The highest power of 2 that "fits into" 11 is indeed 2 * 2 *2.

But:
100 are NOT equal to 2^3.
This is because we start at 2^0, not 2^1.
So the correct are:
1000 = 2^3 = 8.

13-8 = 3.
And repeat again: What's the highest base of 2 that fits into 3?
That's 1, so it's
2*1, which is equal to:
10 = 2(in base 10).
3 -2 = 1.

What's the highest power of 2 that fits into 1? The highest power is 0, so it's simply:
1.

We add everything and get:
1000
0010
0001
------
1011

and 1011(in base 2) happens to be 11(in base 10)

Popular pages Recent additions