Java Forum / General / November 2005
Need help to create byte array
ami - 02 Nov 2005 18:30 GMT 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
Stefan Schulz - 02 Nov 2005 19:15 GMT > Hello, > [quoted text clipped - 3 lines] > 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.
 Signature You can't run away forever, But there's nothing wrong with getting a good head start. --- Jim Steinman, "Rock and Roll Dreams Come Through"
ami - 02 Nov 2005 21:00 GMT 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?
Luc The Perverse - 02 Nov 2005 21:48 GMT >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
-- LTP
:) zero - 02 Nov 2005 22:03 GMT "ami" <amruta@hotmail.com> wrote in news:1130961659.395450.188070 @g47g2000cwa.googlegroups.com:
> 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.
ami - 02 Nov 2005 22:21 GMT 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.
zero - 02 Nov 2005 23:52 GMT "ami" <amruta@hotmail.com> wrote in news:1130966488.608269.247110 @g49g2000cwa.googlegroups.com:
> 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.
ami - 03 Nov 2005 00:10 GMT 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?
zero - 03 Nov 2005 00:26 GMT "ami" <amruta@hotmail.com> wrote in news:1130973038.505110.107370 @f14g2000cwb.googlegroups.com:
> 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.
Roedy Green - 03 Nov 2005 03:13 GMT >> 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? what he might mean is he wants to ability to control bits individually in his messages. He needs a way of doing bit addressibility to get at any bit.
See http://mindprod.com/jgloss/masking.html http://mindprod.com/jgloss/binary.html
for tips on how to do bit twiddling.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 03 Nov 2005 03:02 GMT >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.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 03 Nov 2005 02:58 GMT >> I need to create frame > >What do you mean by frame? He is talking about an Ethernet frame, a hardware packet with some preamble sync bits, addressing header.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 03 Nov 2005 02:57 GMT >How do I insert 10101010 for >first byte in byte array? If possible can you give some code? You then are putting 8 bits per byte.
so if you want to put
01010110 that is 0x56
b[i] = (byte) 0x56;
See http://mindprod.com/jgloss/unsigned.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 03 Nov 2005 02:54 GMT >Clearly java.lang.reflect.Array is the way to go. Look up the >newInstance method (using byte.class as first argument) he is just teasing.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Knute Johnson - 02 Nov 2005 21:39 GMT > Hello, > [quoted text clipped - 5 lines] > > amruta 10101010 binary is -86 decimal when represented in a Java byte.
So you could do:
byte[] buffer = { -86,-86 ... };
 Signature Knute Johnson email s/nospam/knute/
ami - 02 Nov 2005 22:15 GMT 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.
zero - 02 Nov 2005 23:57 GMT "ami" <amruta@hotmail.com> wrote in news:1130966152.737040.278030 @g44g2000cwa.googlegroups.com:
> 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(" "); } }
Roedy Green - 03 Nov 2005 03:15 GMT >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.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 03 Nov 2005 02:53 GMT >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..
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Jack - 03 Nov 2005 03:55 GMT >>Can any one help and explain >>me how to create byte array for 1 and 0? 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.
>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. Roedy Green - 03 Nov 2005 07:29 GMT >>byte[] bits = new byte[56]; > >not to nitpick, but it'd be new byte[0xAA, 0xAA, 0xAA, 0xAA, 0xAA, >0xAA, 0xAA] for 56 bits. It looks originally as if he wanted addressibility so bad he was willing to store only one bit per byte.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
ami - 03 Nov 2005 19:22 GMT So should I do the following:
1.Take parameters as string or int 2. Convert it into bytes and put it into array. 3. send that array to the client 4. read byte convert it to back to print in readable format.
Tex - 03 Nov 2005 19:24 GMT > 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 [quoted text clipped - 5 lines] > 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? Assuming you understand binary, decimal & hex encoding.
1. Don't need array to send 10101010101010101010110... for 56 bits (7 bytes): Problem is that Java compiler won't let you set byte b = 0xAA. You can have such a byte, but can't initialize it with Hex. It would be nice, but tuff luck. I'm sure Java compiler guys thought they were helping me.
So, set byte = -86 which compiler will allow and byte = -86 dec = 10101010 binary = 0xAA hex. All same thing in memory, thus:
byte b = -86; for (int i = 0; i < 7; i++) Send(b);
2. Suppose you need to send arbitraty bit patterns in bytes. For all bit patterns less than 128 you can just use hex, e.g. byte b = 0x3C; For all bit patterns with msb = 1, you need to find equivalent decimal negative representation. Or you could set up a byte array containing all the 256 bit patterns and use an int index which you can set with hex.
a. byte[] b = { 0, // 0. Dec value for byte 0x00 = 0000 0000 . . . . . . 126, // 126. Dec Value for byte 0x7F = 0111 1111 -128, // 127. Dec Value for byte 0x80 = 1000 0000 -127, // 128. Dec Value for byte 0x81 = 1000 0001 . . . . . . -1}; // 255. Dec Value for byte 0xFF = 1111 1111
b. Same array with less typing and less bytecode: byte[] b = new byte[256]; byte bPos = 0; byte bNeg = -128; for ( int i = 0; i <= 0x7F; i++) b[i] = bPos++; for ( int i = 0x80; i <= 0xFF; i++) b[i] = bNeg++;
Now, with our 256-byte byte array set we can send an abritray bit pattern and use Hex. Suppose we want to send 16 bits: 1000 1111 0001 0000 (i.e. 0x8F, 0x10). Create an int array for the 2-byte field/frame or whatever you wanna call it as:
int[] iField = {0x8F, 0x10}; for (int i = 0; i < iField.length; i++) Send(b[iField[i]]);
where integers that the compiler will let us set the way we want are used as indexes into the 256-byte array.
Wanna print out a byte? Working with bits, a hex printout works well, so:
int iHex = b[...some index we want...] & 0x00FF; //0x00FF clears any sign extension String s = Integer.toHexString(iHex);
--tex
Tex - 03 Nov 2005 19:41 GMT >> 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 [quoted text clipped - 5 lines] >> 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? Casting also works and is simple & straight forward. Suppose we want to send 16 bits: 1000 1111 0001 0000 (i.e. 0x8F, 0x10). Create an int array for the 2-byte field as:
int[] iField = {0x8F, 0x10}; //Java compiler allows this for (int i = 0; i < iField.length; i++) Send((byte) iField[i]);
--tex
ami - 04 Nov 2005 18:47 GMT Thank you all . I have success fully created byte array. but Now my proble is I can not read that array? can any one tell me how to read array over sockets? I have tried following code with DataInput/output Streams without any luck :(
byte[] ethFrame; out.write(ethFrame, 0, ethFrame.length);
And tried to read on the other end with in.read(bytes,offset, length); or in.readFully(bytes,offset, length);
but I could not print it out?
Can any one help me to read byte array ?
Please help me!!
Roedy Green - 05 Nov 2005 11:25 GMT >can any one tell me how to read array over sockets? see http://mindprod.com/applets/fileio.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|