display 1's and 0's of byte array

D

dan.ference

I am reading binary messages off of a port via UDP packets ->
socket.receive(packet)

I store the received packet into a byte array -> byte[] received =
packet.getData();

I want to output in binary format the variable received. How do I get
the 1's and 0's to display instead of the asii characters? Any help is
greatly appreciated!
 
C

Chris Smith

I am reading binary messages off of a port via UDP packets ->
socket.receive(packet)

I store the received packet into a byte array -> byte[] received =
packet.getData();

I want to output in binary format the variable received. How do I get
the 1's and 0's to display instead of the asii characters? Any help is
greatly appreciated!

Integer.toString(data, 2), for anything that fits in an int. Not that
if you're treating the byte as unsigned, you should instead do
Integer.toString(data & 0xff, 2) to correct for sign extension.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
F

fastman42

The solution Integer.toString(int, int) does not work in my case. The
binary messages are being stored in the variable 'received' which is of
type byte[]. I want to be able to display the 1's and 0's of
'received' to the screen for human readability (not the ascii
characters). Sorry if i was not clear earlier.
 
R

Roedy Green

The solution Integer.toString(int, int) does not work in my case. The
binary messages are being stored in the variable 'received' which is of
type byte[]. I want to be able to display the 1's and 0's of
'received' to the screen for human readability (not the ascii
characters). Sorry if i was not clear earlier.

use it once on each byte.
 
C

Chris Uppal

fastman42 said:
The solution Integer.toString(int, int) does not work in my case. The
binary messages are being stored in the variable 'received' which is of
type byte[].

So use a loop ?

-- chris
 

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

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top