File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes problem converting to binary Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem converting to binary" Watch "problem converting to binary" New topic
Author

problem converting to binary

tera tellence
Greenhorn

Joined: Sep 01, 2010
Posts: 4
Hi all,

I am new to Java although not new to programming
I do not know how to convert an ASCII(some string value) to binary using some existing useful methods.

So here is an eg.


With Arrays.toString, I get the decimal values of the ASCII but in an array of "[....]" format.

And I had no success converting it into binary using the Integer.toBinaryString I know something is wrong with the data type conversion but dunno what.


Well, my actual problem is this. I have a byte array and I want to see the actual bits in this array.

If I do



I only see the bytes of the OBJECT and not the bytes of the value stored in the byte object.. Am I making sense??

Please help.
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Check this post : http://stackoverflow.com/questions/917163/convert-a-string-like-testing123-to-binary-in-java

There are some samples on there that may help you with what you require


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
String#getBytes() or much better String#getBytes(Charset charset)/String#getBytes(String charsetname) returns a "binary" representation as a byte[]. You want a specific representation of the bytes when printed to the console? Arrays.toString delivers one, but obviously not the one you want.

You need to iterate over the array of byte and print every byte as a binary representation. You want something like "10010110", I guess? Integer.toBinaryString(int) would do.

But keep in mind that this will not show you the bytes in the string but the bytes of a specific representation. If you want ASCII you need to supply the Charset to the getBytes method. If you don't than your platforms default charset will be used which will in most cases not be ASCII - but if it is a superset like ISO-8859 this may go unnoticed.
tera tellence
Greenhorn

Joined: Sep 01, 2010
Posts: 4
Thanks to you two.

Yes Hauke Ingmer,

You need to iterate over the array of byte and print every byte as a binary representation. You want something like "10010110", I guess? Integer.toBinaryString(int) would do.

I do want the "10010110" representation.

My problem is also that, the bytes I pass to convert into Binary needn't always be an ASCII or string that is passed as bytes.

The string I pass into the byte array is just to keep it simple.

I want later read files into byte array and pass the binary(file binary) as byte array.


Do you think Integer.toBinaryString would be of use?
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Sure, if you get the file as byte[] then you just don't need the detour with String#getBytes(String). If at one point in your data flow you get the file contents as string you have to carefully look at every place where you convert the data. Never forget an explicit charset then.

I do want the "10010110" representation.

Just to be sure - we are talking about the string representation here? "1"s and "0"s taken from a charset, the result being a string. I ask just because I can hardly think of a situation where I would need a string representation of the bits of a file content...
tera tellence
Greenhorn

Joined: Sep 01, 2010
Posts: 4
Oops no,

I dont want a string represntation!

I just need to pass the byte array which is the way the binary is obtained. I could pass this as a byte array itself or as string.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Well, then you don't need any conversion at all. ASCII is a binary representation of string contents (but not the one used for Java strings internally).

Hm, before passing around the contents of a file as a byte array you should probably look at Java's stream classes (like java.io.InputStream and java.io.OutputStream), especially the chaining of streams (and readers/writers, if characters are involved). Maybe the java.io.ByteArrayInputStream and java.io.ByteArrayOutputStream can help you achieve what you want to do.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
This discussion from two weeks ago might help. Or then again it might not help. It makes interesting reading, anyway.
tera tellence
Greenhorn

Joined: Sep 01, 2010
Posts: 4
thanks to you all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem converting to binary