Need help to create byte array

Discussion in 'Java' started by ami, Nov 2, 2005.

  1. ami

    ami Guest

    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
    ami, Nov 2, 2005
    #1
    1. Advertising

  2. On Wed, 02 Nov 2005 09:30:14 -0800, ami wrote:

    > 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.

    --
    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"
    Stefan Schulz, Nov 2, 2005
    #2
    1. Advertising

  3. ami

    ami Guest

    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?
    ami, Nov 2, 2005
    #3
  4. ami wrote:
    > 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 ... };

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Nov 2, 2005
    #4
  5. "ami" <> wrote in message
    news:...
    >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

    :)
    Luc The Perverse, Nov 2, 2005
    #5
  6. ami

    zero Guest

    "ami" <> 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.
    zero, Nov 2, 2005
    #6
  7. ami

    ami Guest

    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.
    ami, Nov 2, 2005
    #7
  8. ami

    ami Guest

    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.
    ami, Nov 2, 2005
    #8
  9. ami

    zero Guest

    "ami" <> 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.
    zero, Nov 2, 2005
    #9
  10. ami

    zero Guest

    "ami" <> 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(" ");
    }
    }
    zero, Nov 2, 2005
    #10
  11. ami

    ami Guest

    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?
    ami, Nov 2, 2005
    #11
  12. ami

    zero Guest

    "ami" <> 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.
    zero, Nov 2, 2005
    #12
  13. ami

    Roedy Green Guest

    On 2 Nov 2005 09:30:14 -0800, "ami" <> wrote, quoted
    or indirectly quoted someone who said :

    >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..



    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #13
  14. ami

    Roedy Green Guest

    On Wed, 02 Nov 2005 19:15:56 +0100, Stefan Schulz <>
    wrote, quoted or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #14
  15. ami

    Roedy Green Guest

    On 2 Nov 2005 12:00:59 -0800, "ami" <> wrote, quoted
    or indirectly quoted someone who said :

    >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 = (byte) 0x56;

    See http://mindprod.com/jgloss/unsigned.html

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #15
  16. ami

    Roedy Green Guest

    On Wed, 02 Nov 2005 22:52:28 GMT, zero <> wrote, quoted or
    indirectly quoted someone who said :

    >> 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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #16
  17. ami

    Roedy Green Guest

    On 2 Nov 2005 15:10:38 -0800, "ami" <> wrote, quoted
    or indirectly quoted someone who said :

    >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.


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #17
  18. ami

    Roedy Green Guest

    On Wed, 02 Nov 2005 23:26:07 GMT, zero <> wrote, quoted or
    indirectly quoted someone who said :

    >> 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #18
  19. ami

    Roedy Green Guest

    On 2 Nov 2005 13:15:52 -0800, "ami" <> wrote, quoted
    or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #19
  20. ami

    Jack Guest

    On Thu, 03 Nov 2005 01:53:32 GMT, Roedy Green
    <> wrote:

    >>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.
    Jack, Nov 3, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,243
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    640
    Kirby
    Oct 8, 2004
  3. Replies:
    20
    Views:
    9,765
    licebmi
    Sep 7, 2009
  4. Tom McGlynn
    Replies:
    4
    Views:
    854
    Mark Space
    Apr 19, 2008
  5. Patricia Shanahan
    Replies:
    0
    Views:
    385
    Patricia Shanahan
    Apr 17, 2008
Loading...

Share This Page