Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Print/display the raw bits (the 1s and 0s)

 
Timothy Toe
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I print/display the raw bits (the 1s and 0s) that Java writes to and read from a socket stream ?

By the way, I am using DataInputStream and DataOutputStream´┐Żand my Java program is communicating with a C++ program at the other endpoint´┐Żvia a TCP/IP socket connection.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if Integer.toBinaryString() will help. I expect you'll have to convert each byte one at a time.
 
Timothy Toe
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

But I dun understand the following :

I send character H into the stream. If Java deals with Unicode, which is 16-bits, then it should send 0000 0000 0100 1000. 'H' is 0048 in Unicode.

But Integer.toBinaryString(...) gave me :
1001000

Did 0000 0000 0100 1000
or just
1001000
went into the stream and into the network ?

I need to send fixed length 16-bit Unicode characters.

Thanks again.
 
Timothy Toe
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I know for sure what went into the network ? I mean the 1s and 0s...
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation for OutputStream.write() says:
Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

So if you write 'H', a byte value of 48 (0100 1000) gets written.
The easy way to check would be to write a server and client pair to read/write values into the streams and check them.
If you need to write 16-bit Unicode, you will probably have to wrap the socket's OutputStream with a
OutputStreamWriter and specify a Unicode charset that supports 16-bit characters.
 
Timothy Toe
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

If you need to write 16-bit Unicode, you will probably have to wrap the socket's OutputStream with a
OutputStreamWriter and specify a Unicode charset that supports 16-bit characters.


Class Charset which OutputStreamWriter uses has UTF-16 charsets. But from what I understand, UTF-16 is a type of Unicode compression (for storage of characters in files and for transmission over the network)...and thus, it is a variable-width charset. Some characters in UTF-16 are coded in 16-bits and some 32-bits (surrogates). I need fixed-width.

Thanks.
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read the documentation for Charset, it states that:
Every implementation of the Java platform is required to support the following standard charsets.

Several of which are variations on UTF-16. It goes on to state:

Consult the release documentation for your implementation to see if any other charsets are supported.

So UTF-16 is not the only choice via the Charset class. The alternatives will, of course, vary depending on your platform. Charset has an availableCharsets() method which enumerates the alternatives.
An important question to ask is: what encoding does your other program expect.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had Raw Bits for breakfast this morning. (That's oat hulls and wheat chaff for those who aren't long time PHC fans)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic