how to print a string in binary

D

djbitchpimp

I want to print out the contents of a string in binary. For example:

String s = "0123" ;

I want my System.out.println to print:

0000 0001 0010 0011

Any ideas?
 
J

Joan

I want to print out the contents of a string in binary. For
example:

String s = "0123" ;

I want my System.out.println to print:

0000 0001 0010 0011

Any ideas?
In java version 5.0 they have "System.out.format()"
you should check it out.
 
A

Alan Krueger

Roedy said:
this is dreadful code. I suggest avoiding it.

int check = (int)Math.pow(2,pos - 1);
return ( check == (num & check) );

Yes, not only does Math.pow(2,<something>) take an order of magnitude
more time to compute than the left-shift operator[1], but it fails to
compute the check correctly for the most significant bit.

[1] Repeated micro-benchmarks with v1.5.0_03 JRE, so take with a grain
of salt.
 
M

Malte

Roedy said:
this is dreadful code. I suggest avoiding it.

int check = (int)Math.pow(2,pos - 1);
return ( check == (num & check) );

My point was that Google more often than not gets faster replies than a
NG :)
 
J

Jeff Schwab

I want to print out the contents of a string in binary. For example:

String s = "0123" ;

I want my System.out.println to print:

0000 0001 0010 0011

Any ideas?

// Would this do?


import java.io.PrintWriter;

public class ToBinary {

private static PrintWriter out =
new PrintWriter(System.out, true);

private static PrintWriter err =
new PrintWriter(System.err, true);

public static String toBinary(String s)
throws BadDigitException {

String result = "";

for (int i = 0; i < s.length(); ++i) {
switch (s.charAt(i)) {
case '0': result += "0000 "; break;
case '1': result += "0001 "; break;
case '2': result += "0010 "; break;
case '3': result += "0011 "; break;
case '4': result += "0100 "; break;
case '5': result += "0101 "; break;
case '6': result += "0110 "; break;
case '7': result += "0111 "; break;
case '8': result += "1000 "; break;
case '9': result += "1001 "; break;
case 'a':
case 'A': result += "1010 "; break;
case 'b':
case 'B': result += "1011 "; break;
case 'c':
case 'C': result += "1100 "; break;
case 'd':
case 'D': result += "1101 "; break;
case 'e':
case 'E': result += "1110 "; break;
case 'f':
case 'F': result += "1111 "; break;
default: throw new
BadDigitException(s.charAt(i));
}
}
return result;
}

public static void main(String[] args) {
try {
out.println(toBinary("0123"));
} catch(BadDigitException x) {
err.println(x);
}
}
}

class BadDigitException extends Exception {
BadDigitException(char c) {
super("Bad digit: " + c);
}
}
 
D

djbitchpimp

Ok thanks for all of the replies! What about if I wanted to take the
string and put the binary value into an array of 1's and 0's? For
instance if I used the string "password", the binary value would be
01110000 01100001 01110011 01110011 01110111 01101111 01110010
01100100. I want this in an array, say

int bytes [] = new int [56] ;
bytes [0] = 0;
bytes [1[ = 1;
....
 
S

steve

I want to print out the contents of a string in binary. For example:

String s = "0123" ;

I want my System.out.println to print:

0000 0001 0010 0011

Any ideas?

// Would this do?


import java.io.PrintWriter;

public class ToBinary {

private static PrintWriter out =
new PrintWriter(System.out, true);

private static PrintWriter err =
new PrintWriter(System.err, true);

public static String toBinary(String s)
throws BadDigitException {

String result = "";

for (int i = 0; i < s.length(); ++i) {
switch (s.charAt(i)) {
case '0': result += "0000 "; break;
case '1': result += "0001 "; break;
case '2': result += "0010 "; break;
case '3': result += "0011 "; break;
case '4': result += "0100 "; break;
case '5': result += "0101 "; break;
case '6': result += "0110 "; break;
case '7': result += "0111 "; break;
case '8': result += "1000 "; break;
case '9': result += "1001 "; break;
case 'a':
case 'A': result += "1010 "; break;
case 'b':
case 'B': result += "1011 "; break;
case 'c':
case 'C': result += "1100 "; break;
case 'd':
case 'D': result += "1101 "; break;
case 'e':
case 'E': result += "1110 "; break;
case 'f':
case 'F': result += "1111 "; break;
default: throw new
BadDigitException(s.charAt(i));
}
}
return result;
}

public static void main(String[] args) {
try {
out.println(toBinary("0123"));
} catch(BadDigitException x) {
err.println(x);
}
}
}

class BadDigitException extends Exception {
BadDigitException(char c) {
super("Bad digit: " + c);
}
}

at a pinch yes , but it is seriously bloated, you can "loose" all the lower
case "abcdef" by adding a single line to convert to uppercase at the start
of the routine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top