programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Hi, I need Help to convert String to Binary

Greenhorn
Posts: 4
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
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
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
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
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
I understand that Java 1.5 is (finally!) going to have a "printf()" function, where you can do things like 'printf ("%04d", myInt)'...

Wanderer
Sheriff
Posts: 18671
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
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: