Need help to create byte array

A

ami

Hello,

I am a new bee in java, I am simulating ethernet frame for which I
need to create byte array of 0'and 1's. Ethernet frame has 56 bit
preamble of alternating 1's and 0's. I need to send this field and
other with similar format over the socket. Can any one help and explain
me how to create byte array for 1 and 0?

amruta
 
S

Stefan Schulz

Hello,

I am a new bee in java, I am simulating ethernet frame for which I
need to create byte array of 0'and 1's. Ethernet frame has 56 bit
preamble of alternating 1's and 0's. I need to send this field and
other with similar format over the socket. Can any one help and explain
me how to create byte array for 1 and 0?

Clearly java.lang.reflect.Array is the way to go. Look up the
newInstance method (using byte.class as first argument)

Or just use any very basic textbook.
 
A

ami

I understand how to declare array. I am famalier with int,long double
array but I am confused for byte array. How do I insert 10101010 for
first byte in byte array? If possible can you give some code?
 
K

Knute Johnson

ami said:
Hello,

I am a new bee in java, I am simulating ethernet frame for which I
need to create byte array of 0'and 1's. Ethernet frame has 56 bit
preamble of alternating 1's and 0's. I need to send this field and
other with similar format over the socket. Can any one help and explain
me how to create byte array for 1 and 0?

amruta

10101010 binary is -86 decimal when represented in a Java byte.

So you could do:

byte[] buffer = { -86,-86 ... };
 
L

Luc The Perverse

ami said:
I understand how to declare array. I am famalier with int,long double
array but I am confused for byte array. How do I insert 10101010 for
first byte in byte array? If possible can you give some code?

Use hex.

Make sure you understand how to convert between different bases and the
concepts behind it. Could could store any discrete binary number (Within
range) in an int just as easily
 
Z

zero

I understand how to declare array. I am famalier with int,long double
array but I am confused for byte array. How do I insert 10101010 for
first byte in byte array? If possible can you give some code?

If you want an actual array of bytes, why not use byte[]?

If you want to manipulate individual bits, have a look at the BitSet class.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/BitSet.html

I think you should post some code, and ask a more specific question. That
way we can give the answer that is best suited for your needs. Otherwise
we're just working on a best guess.
 
A

ami

Sorry If I am asking silly question but if I put -86 How do I convert
it into 10101010 while reading it back. While printing I have to print
it in binary form.
 
A

ami

I do not have any code because I can not figure out how to insert
binary no. in the array. I need to create frame in which certain bytes
are used for certain fields. frame has preamble, destination and source
address, CRC I have to convert everything in binary data to send from
server to client socket.
 
Z

zero

I do not have any code because I can not figure out how to insert
binary no. in the array.

That depends on what you mean by array. You have to decide what kind of
data structure you want before you can figure out how to insert things into
it.
I need to create frame

What do you mean by frame? In Java, a frame is usually a window - ie
something visual on the screen containing user interface components.
in which certain bytes are used for certain fields. frame has preamble,
destination and source address, CRC I have to convert everything in
binary data to send from server to client socket.

I think you're jumping the gun here. You first have to think about how
you're going to design your program. Are you creating both the server and
client? If so, you can just send complete objects instead of byte streams
(of course under the hood it's byte streams anyway, but you don't have to
worry about that).
If you really need bytes, why were you talking about 1s and 0s? That's
bits, not bytes. If you want to send a byte stream, you don't need to know
about the individual bits.

If you simply want to convert an int to a byte, it's as simple as:

int i = 5;
byte b = (byte)i;

At least, as long as the int is within the range of a byte.

You really need to give us some more information to get a better answer.
What exactly do you want to do? You may be making it harder than it should
be.
 
Z

zero

Sorry If I am asking silly question but if I put -86 How do I convert
it into 10101010 while reading it back. While printing I have to print
it in binary form.

If you just want to print it on screen, try this:

public static void printIntAsBits(int value)
{
int mask = 1 << 31;

for(int i = 0; i < 32; i++)
{
System.out.print((value & mask) == 0 ? "0" : "1");
value <<= 1;
if(i%8 == 0)
System.out.print(" ");
}
}
 
A

ami

I think you are right I need to send bits over the socket. I can not
use objects I need to send bits. but I thought you can not have bits in
java that is why I am saying byte.
Please help regarding how to send bits?How to convert string or int
type in bits?
 
Z

zero

I think you are right I need to send bits over the socket. I can not
use objects I need to send bits.

Why? An individual bit has no meaning, it must be part of some data, so
why would you need to send the individual bits?
but I thought you can not have bits in
java that is why I am saying byte.
Please help regarding how to send bits?How to convert string or int
type in bits?

Strings and ints *are* bits, just grouped together in a meaningful way.
Anyway, this may help:

Socket aSocket;
....
OutputStream output = aSocket.getOutputStream();
String sData = "hello world";
output.write(sData.bytes());
....

Don't forget to close your streams and sockets.
 
R

Roedy Green

Can any one help and explain
me how to create byte array for 1 and 0?

You could use a java.util.BitSet
or
byte[] bits = new byte[56];

There is no way you are going to get that preamble sent over the wire
in a way that fools other hardware into thinking it was hardware-
generated. All you can do is cart your bits around as data inside the
ethernet packets.

You are born a little to late. Back in the early 80s I lead a
Saturday morning Apple ][ club. We bought Hitachi X25 chips and build
ourselves a little LAN from the ground up. But even back then that
framing was all hardware-generated..
 
R

Roedy Green

I think you are right I need to send bits over the socket. I can not
use objects I need to send bits. but I thought you can not have bits in
java that is why I am saying byte.
Please help regarding how to send bits?How to convert string or int
type in bits?

I keep jumping about in my understanding what you want to accomplish.

1. If you want to do a software simulation of a small ethernet LAN all
running in one machine, that sounds like a doable project.

2. If you somehow think you are going to create ethernet frames and
shoot them out over your LAN forget it.

3. If you are trying to figure out how to make remote machines
communicate simulating an Ethernet LAN, then the preamble bits are
irrelevant. Further, there is nothing like the broadcast ability for
remotes and even if there were, it would be an inefficient way to
communicate.
 
R

Roedy Green

Sorry If I am asking silly question but if I put -86 How do I convert
it into 10101010 while reading it back.

Inside the machine everything is binary. We convert it to strings so
binary-challenged humans can make sense of it. You can also convert a
binary int to String of the letters '0' and '1' just as easily as the
String of the letters '0' '1' ... '9'.

See http://mindprod.com/jgloss/binary.html
for how.
 
J

Jack

geepers, I think all the guy wanted was to know how to specify to the
compiler that he is using binary notation :)

once upon a time, it was what? %100 meant 4, and not 100 decimal? You
didn't need to specify all eight bits, either. Am I remembering that
correctly?

So he wanted: byte[] array = new byte[%10101010, %10101010, ....]

But I believe that we can't do that in Java, as Roedy's glossary says.
So therefore, the next answer is that you can use hexadecimal
notation, 0xAA (which is somewhat more obvious than using decimal).

Or maybe it's 0x55 that he wants...

You could use a java.util.BitSet
or
byte[] bits = new byte[56];

not to nitpick, but it'd be new byte[0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA] for 56 bits.

BUT, as Roedy says below, it's all moot - since you can't do what he
wants. You can oniy send TCP or UDP in Java, and maybe lately a bit of
ICMP.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top