1 Replies - 1155 Views - Last Post: 18 February 2009 - 08:04 AM Rate Topic: -----

#1 mrfikri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 12-October 08

Problem With Binary Number for SPACE

Posted 18 February 2009 - 06:48 AM

	String x;
	int z;
	
	System.out.println("Your Plaintext?:");
	x = stdin.readLine();
	
	byte[] w = x.getBytes();
	
	for(z = 0; z < w.length; z++)
	{
	  String p = Byte.toString(w[z]);
	  int i = Integer.parseInt(p);
	  String by = Integer.toBinaryString(i);
	  if(by == "100000")
	  {
		by = "0100000";
	  }
	  System.out.print(by + " ");
	}


I've a problem with this... i don't know why?... characters will be showed in 7 bits binary number but 'space' will be shown in only 6 bits. why? is it going to change the value if i add 0 in front the binary number of SPACE? if no, how to add it since i couldn't make it in my source code. :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: Problem With Binary Number for SPACE

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem With Binary Number for SPACE

Posted 18 February 2009 - 08:04 AM

Change
 if(by == "100000")
to
if(by.equals( "100000"))


== will be true if the lhs and rhs identifiers are the same object, but false if they are 2 separate objects holding equivalent values..


Quote

i don't know why?... characters will be showed in 7 bits binary number but 'space' will be shown in only 6 bits. why?

The ascii code for SPACE is 32 so it only needs 6 binary digits. The ordinary printable characters (on an English keyboard) have ascii codes 33 - 126 so they require 7 binary digits. Integer.toBinaryString() doesn't append any leading zeros.
Was This Post Helpful? 1

Page 1 of 1