aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Print/display the raw bits (the 1s and 0s) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Print/display the raw bits (the 1s and 0s)" Watch "Print/display the raw bits (the 1s and 0s)" New topic
Author

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

Timothy Toe
Ranch Hand

Joined: Oct 19, 2002
Posts: 156
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

Joined: Jan 29, 2003
Posts: 8791
See if Integer.toBinaryString() will help. I expect you'll have to convert each byte one at a time.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Timothy Toe
Ranch Hand

Joined: Oct 19, 2002
Posts: 156
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

Joined: Oct 19, 2002
Posts: 156
How do I know for sure what went into the network ? I mean the 1s and 0s...
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8713
    
    6

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Timothy Toe
Ranch Hand

Joined: Oct 19, 2002
Posts: 156
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

Joined: Oct 29, 2001
Posts: 8713
    
    6

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

Joined: Jan 29, 2003
Posts: 8791
I had Raw Bits for breakfast this morning. (That's oat hulls and wheat chaff for those who aren't long time PHC fans)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Print/display the raw bits (the 1s and 0s)
 
Similar Threads
about Math.abs()
Left shift operators
~ and !
How to represent bitstring in java
does java codes are converted into assembly by JVM ?