Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: 8 bit binary to ascii

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 8 bit binary to ascii

    Hi there,

    I have converted a string into binary for example 'hello' =
    01101000 01100101 01101100 01101100 01101111
    h e l l o

    I have stored this binary in a 2 dimentional array of type int.(Each set of 8 bits is its own array)

    I now need to convert this 8 bit binary to ascii (Back to 'hello')

    Any thoughts?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 8 bit binary to ascii

    You can convert each set of 8 back into a number and then put that value directly into a byte data type (some casting required). To convert between bases: See WikiHow: Convert from binary to decimal

    The biggest issue is that the values you have are in ASCII, while the char data type is Unicode. Technically you won't see this crop up if you just want to get those ASCII values back, but it's possible that if you try to display these values you will get the wrong characters displayed. See Java: Converting Non-Unicode Text for information on converting non-unicode characters into unicode.

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: 8 bit binary to ascii

    Hi
    If I were you I would convert that Binary into String by using following code. If you find any other best way let me know.
    int power;
    		double val;
    		int asval;
    		String s2=" ";
    		for(int i=0;i<5;i++)
    		{
    			power=-1;
    			asval=0;
    			for(int j=7;j>=0;j--)
    			{
    				power++;
    				val=0;
     
    				if(arr[i][j]==1)
    				{
    					val=Math.pow(2,power);
    					asval += (int)(val);
     
    				}
     
    			}
    			s2=s2+(char)(asval);
     
    		}
    		s2=s2.substring(1);
    		System.out.println(s2);
    Last edited by murali1253; April 6th, 2010 at 04:30 PM.

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 8 bit binary to ascii

    hi,
    thanks for the post. i have run the code but it didn't work.
    say if i put string as "hi" it will print 0110100001101001.
    and how i'm gonna convert it back?

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: 8 bit binary to ascii

    consider a to z, 0 to 25 and store the bit pattern in the corresponding array (the first index). Suppose a[2][] will contain pattern for c, u will search the array if match found add 97 to its index. A very naive approach, slow....

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: 8 bit binary to ascii

    Integer.parseInt(string, radix);

  7. The Following User Says Thank You to Zymus For This Useful Post:

    Json (May 6th, 2011)

Similar Threads

  1. ASCII Triangle
    By physics in forum Loops & Control Statements
    Replies: 1
    Last Post: March 27th, 2010, 06:39 AM
  2. Problem with binary tree
    By Exoskeletor in forum Object Oriented Programming
    Replies: 2
    Last Post: January 8th, 2010, 01:03 PM
  3. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM
  4. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM
  5. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM