# Char to int producing different result

**URL:** <https://discourse.processing.org/t/char-to-int-producing-different-result/15655>\
**Category:** Beginners\
**Created:** [November 20, 2019, 6:59am UTC](https://discourse.processing.org/t/char-to-int-producing-different-result/15655 "2019-11-20T06:59:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruchi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Ruchi](https://discourse.processing.org/u/Ruchi)\
**Post date:** [November 20, 2019, 6:59am UTC](https://discourse.processing.org/t/char-to-int-producing-different-result/15655/1 "2019-11-20T06:59:57Z")

</div>

```auto
String bin ="100";
char bit;
float sum;

void setup(){
  size(500,200);
  noLoop();
  sum=0;
}

void draw(){
  background(0);
  for(int i=0;i<bin.length();i++){   
    bit = bin.charAt(bin.length()-1-i);
    //println(bit);
    sum+= parseInt(bit) * pow(2,i);
    println(sum);
    
  }  
}

```

Console:  
48.0  
144.0  
340.0

Doing binary to decimal conversion but the parseInt is converting characters to their corresponding ascii values & not to the same number.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [November 20, 2019, 10:53am UTC](https://discourse.processing.org/t/char-to-int-producing-different-result/15655/2 "2019-11-20T10:53:40Z")

</div>

Here 4 different ways to fix it:

```
sum += bit == '1' ? pow(2, i) : 0;

sum += parseInt("" + bit, 2) * pow(2, i);

sum += bit == '1' ? (1 << i) : 0;

sum += parseInt("" + bit, 2) << i;

```

`parseInt()` accepts a second argument to specify the base, for binary it’s 2. But the first argument must be a string, not a char.

Also this is the shortest:

```
println(Integer.parseInt("1001", 2));

```

🙂

---

<div class="post-metadata">

**Author:** ![Ruchi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Ruchi](https://discourse.processing.org/u/Ruchi)\
**Post date:** [November 23, 2019, 5:55am UTC](https://discourse.processing.org/t/char-to-int-producing-different-result/15655/3 "2019-11-23T05:55:57Z")

</div>

1st & 3rd method is bit intimidating lol. Could you provide some reference for understanding this sort of expression.Thanks

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 23, 2019, 8:04am UTC](https://discourse.processing.org/t/char-to-int-producing-different-result/15655/4 "2019-11-23T08:04:40Z")

</div>

best: play with it

```auto
// Java Ternary Operator //The ? : operator in Java as a style of IF
float a = 3, b = 7, max;

if (a > b) {
  max = a;
} else {
  max = b;
}

println("if",a, b, max);

max = (a > b) ? a : b;

println("? ",a, b, max);

println("a > b", (a > b) );
println("a > b", (a > b) ? 1 : 0 );

```

it is just a writing style for the IF

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [November 23, 2019, 9:21am UTC](https://discourse.processing.org/t/char-to-int-producing-different-result/15655/5 "2019-11-23T09:21:59Z")

</div>

[https://processing.org/reference/conditional.html](https://processing.org/reference/conditional.html)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 23, 2019, 9:34am UTC](https://discourse.processing.org/t/char-to-int-producing-different-result/15655/6 "2019-11-23T09:34:34Z")

</div>

Hello,

> **[?: (conditional) / Reference](https://processing.org/reference/conditional.html)**
>
> A shortcut for writing an if and else structure. The conditional operator, ?: is sometimes called the ternary operator, an operator that takes three arguments. If the test …

> **[\<\< (left shift) / Reference](https://processing.org/reference/leftshift.html)**
>
> Shifts bits to the left. The number to the left of the operator is shifted the number of places specified by the number to the right. Each shift to the left doubles the number, therefore each left shi…

> **[charAt() / Reference](https://processing.org/reference/String_charAt_.html)**
>
> Returns the character at the specified index. An index ranges from 0 to the length of the string minus 1. The first character of the sequence is at index 0, the next at index 1, etc.

> **[pow() / Reference](https://processing.org/reference/pow_.html)**
>
> Facilitates exponential expressions. The pow() function is an efficient way of multiplying numbers by themselves (or their reciprocal) in large quantities. For example, pow(3, 5) is equi…

```auto
String bin ="10001010";
char bit = 0;
float sum = 0;

println(bin.length());

for(int i=0;i<bin.length();i++)
  {   
  bit = bin.charAt(bin.length()-1-i);
  sum += bit == '1' ? pow(2, i) : 0;  
  //sum += parseInt("" + bit, 2) * pow(2, i); 
  //sum += bit == '1' ? (1 << i) : 0;  
  //sum += parseInt("" + bit, 2) << i;
  println(i, bit, sum);
  }

```

Content of loop courtesy of:

> [@hamoid](#):
>
> 4 different ways to fix it

🙂
