• 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

problem converting 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 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.
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This discussion from two weeks ago might help. Or then again it might not help. It makes interesting reading, anyway.
 
tera tellence
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to you all
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic