5 Replies - 761 Views - Last Post: 28 March 2011 - 06:47 AM Rate Topic: -----

#1 atromen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-February 11

converting binary to string

Posted 27 March 2011 - 07:22 AM

hi everyone,
is there any method to convert binary to string?
i know little about toString method but don't know how to use it..
always give me error.. anyone?
thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: converting binary to string

#2 atromen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-February 11

Re: converting binary to string

Posted 27 March 2011 - 07:28 AM

fyi, i have declared an array as boolean..
i have converted the text into binary and put it in the register,
i want to do the reverse now
so how i'm gonna deal with it?
Was This Post Helpful? 0
  • +
  • -

#3 atromen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-February 11

Re: converting binary to string

Posted 27 March 2011 - 11:21 AM

Hi,
I have made a code to convert text to binary.
the problem is I don't know how to convert it back to text.
so I really need your help. please. anyone?


import java.util.*;
public class textToBinary{

public static void main(String[] args) {


		Scanner input = new Scanner(System.in);
		
		System.out.println("enter text");
		String text = input.nextLine();
		
		byte[] bytes = text.getBytes();
  		StringBuilder binary = new StringBuilder();
  		for (byte b : bytes) {
     			int val = b;
     			for (int i = 0; i < 8; i++) {
        			binary.append((val & 128) == 0 ? 0 : 1);
        			val <<= 1;
		//System.out.println(binary);
		
     			}
     			
  	}System.out.println(binary);
}

}	

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: converting binary to string

Posted 27 March 2011 - 11:56 AM

By the way

binary.append((val & 128) == 0 ? 0 : 1);

and

binary.append(val & 128);

must be kind of equivalent.

Now terminology: in a computer everyting is in binary, even the String you read from the user
It is the display of these binary words that change depending on the variable type used

So if a byte contains x41 or 65d or 1000001b as you wish
If this byte is contained in a variable of type String or char it will be display as A
if it is in a int or a short or a byte and display in decimal it will be displayed as 65
unless you ask to display in binary format or hexadecimal format

By doing

byte[] byte = text.getBytes();

your conversion from text to binary is done !!!

Now if you use a StringBuilder (as the name mentions it is a STRING builder) to build a String representing your data with "0" and "1". This is not converting text to binary.

To built it back
just loop through your 0 and 1 as char
subtract to them - '0'
that will leed int 0 or 1
add it to a byte
shift the byte by 1 bit to the left

start over for each 8 "0"-"1" found in the stream
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: converting binary to string

Posted 27 March 2011 - 12:55 PM

View Postatromen, on 27 March 2011 - 09:22 AM, said:

hi everyone,
is there any method to convert binary to string?
i know little about toString method but don't know how to use it..
always give me error.. anyone?
thanks in advance.

String str = Integer.toBinaryString(123456);
Was This Post Helpful? 1
  • +
  • -

#6 atromen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-February 11

Re: converting binary to string

Posted 28 March 2011 - 06:47 AM

Quote

binary.append((val & 128) == 0 ? 0 : 1);

and

binary.append(val & 128);

must be kind of equivalent.


apparently it's not, without putting "== 0 ? 0 : 1", the result would be any integer,
not just 1 and 0.


Quote

By doing

byte[] byte = text.getBytes();

your conversion from text to binary is done !!!


that's also didn't work bcoz it won't print binaries but symbols and letters


Quote

String str = Integer.toBinaryString(123456);


this won't convert those integers to String but to binaries.


thanks for your help, i would really appreciate it if you could help more.
thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1