• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hi, I need Help to convert String to Binary

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every one,
I am new student in java programming, so I need your help

I want to takes a function as its input a string of decimal integers, each character in the string can be thought of as a decimal digit. The digit should be converted to 3-bit binary string an packed into an int.

I did the following code, but there is problem in convert 5 and 6 also I want the output like this

1234567 = 001 010 011 100 101 110 111

class Bin
{
public static void main(String []args)
{
String N=(args[0]);
for(int i=0;i<N.length();i++)
{
String n=N.substring(i,i+1);
int a=Integer.parseInt(n);
int y=1,b;
String z="";

for(a=a;a!=0 ; )
{
z=z+y;
y=a&1;
a=a>>1;

}
b=Integer.parseInt(z);
System.out.print(" "+b+" ");

}
System.out.println();
}
}


Thanks,
[ September 30, 2004: Message edited by: Sultan A. A ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you are extracting the bits of a from right to left but concatenating them to z from left to right.
You should have written z=y+z; as the last statement in your inner loop. Then you don't need to give y an initial value.
By initializing y as 1 you got a lot of the digits right, but your algorithm is still backwards.
[ September 30, 2004: Message edited by: Mike Gershman ]
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should change your code like this :



Change code in line of z = z + y; to z = y + z;

And

Move code in changed code to last line in loop.

And

change for loop to while loop. (It's readability).
 
Sultan A. A
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for help, but the problem now is the output like this
1 10 11 100 101 110 111
I want it like
001 010 011 100 101 110 111

and
thaks for help me again,
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For that you need a way to pad the output, cattleDrive provide a method there, change " " to "0".

Originally posted by Sultan A. A:
Thanks for help, but the problem now is the output like this
1 10 11 100 101 110 111
I want it like
001 010 011 100 101 110 111

and
thaks for help me again,


[ October 01, 2004: Message edited by: chi Lin ]
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that Java 1.5 is (finally!) going to have a "printf()" function, where you can do things like 'printf ("%04d", myInt)'...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that Java 1.5 is (finally!) going to have a "printf()" function

In fact, 1.5 is available now. (Well it's called 5.0 for some silly reason, but aside from that...) And yes, it does have a printf() function. Check out PrintWriter interface. Enjoy...
[ October 02, 2004: Message edited by: Jim Yingst ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't want to use System.out.printf(), which probably won't be on your exams or your grader's system, try this:
 
reply
    Bookmark Topic Watch Topic
  • New Topic