0

In the following piece of code i am trying to enter a 8 digit binary number then manipulate each digit seperatly.

import java.util.Scanner;
public class binCoverstion2{

   private static Scanner kbd = new Scanner(System.in);

   public static void main (String[] args){
      int bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7, decNumber;
      System.out.println("Enter a 8 digit binary number then hit enter.");
      String binaryNum = kbd.nextLine();
      bit0 = binaryNum.charAt(0);
      System.out.println(bit0);

The output of bit0 is just to see what the value was set too. the input i gave it was 10011001, but bit0 was set to 49. Why?

2
Contributors
1
Reply
6
Views
3 Years
Discussion Span
Last Post by JamesCherrill
0

Java chars are really numeric values that represent Unicode (ASCII) characters, and as it happens the ASCII value for the character '1' is decimal 49 (see http://web.cs.mun.ca/~michael/c/ascii-table.html).
Your bit0 variable is an int, so Java displays it as a numeric value, not the character that it represents. If you declare bit0 as a char it will print more like you expect.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.