Re: [porting code from c++ to java]

Discussion in 'Java' started by Enis, Apr 1, 2004.

  1. Enis

    Enis Guest

    ------------------

    that's what i done :
    DatagramPacket dp;

    i have no more utility to use ByteArrayInputStream as dp provide me an
    array of bytes :

    byte[] buffer = new byte[10000];
    buffer =dp.getData();

    after i have problems to transform this array in natif java
    variables...as the datagram contains long,int,short,byte... variables

    i have problems to cast from an array to a short etc...:
    byte[] tt = new byte[2];
    System.arraycopy(buffer,1,tt,0,1);
    short x =(Short) tt;
    this does not work

    i tried also to deal with bytes even if i think that i'm mistake but
    the idea is :
    short x;
    (byte)(X >>> 8 )= buffer[3] ;
    (byte) X=buffer[2];

    but this does not work no more...
    so if someone see how i must deal with it thanks....
     
    Enis, Apr 1, 2004
    #1
    1. Advertising

  2. Enis

    Chris Smith Guest

    Enis,

    I'm sorry, but I can't help you. I simply have no idea what you're
    saying. Maybe someone else can parse your grammar better and tell you
    what you need to know. Hope what I've said so far is at least a little
    useful.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 2, 2004
    #2
    1. Advertising

  3. I have rewritten some of what you said based on my (limited)
    understanding of what you were trying to say. My edits appear
    in [square brackets] below.

    Enis wrote:

    > DatagramPacket dp;
    >
    > I have no more [need] to use ByteArrayInputStream as dp provides me
    > an array of bytes:
    >
    > byte[] buffer = new byte[10000];
    > buffer = dp.getData();


    You just allocated a 10 thousand byte array, and then discarded it.
    The garbage collector has enough work to do without you making more.

    byte[] buffer = dp.getData();

    > [I have problems converting this array to primitive Java variables]
    > as the datagram contains long, int, short, byte... [values]


    You haven't mentioned whether the values in the datagram are in big-
    or little-endian form. I will assume they are in network byte order
    (big-endian), which is also what Java uses.

    > i have problems to cast from an array to a short etc...:
    > byte[] tt = new byte[2];
    > System.arraycopy(buffer,1,tt,0,1);
    > short x =(Short) tt;
    > this does not work


    No kidding. Java does not permit lying to the compiler about the
    types of variables.

    > I tried also to deal with bytes even if I think that I'm [mistaken]
    > but the idea is:


    > short x;
    > (byte)(X >>> 8) = buffer[3];
    > (byte) X = buffer[2];


    x = (buffer[2] << 8) | (buffer[3] & 0xff);

    > but this does not work [...]


    I don't think you understand the purpose of ByteArrayInputStream. It
    lets you read from an array of bytes *as if* it were an input stream.
    Combined with DataInputStream, you have all the tools you need to
    extract the longs, ints, shorts, and bytes from the datagram without
    worrying about the bit-shuffling yourself. (And based on the code
    above, that's a *good* thing.)

    ByteArrayInputStream bais = new ByteArrayInputStream(dp.getData());
    DataInputStream dis = new DataInputStream(bais);
    short x = dis.readShort(); // get the first two bytes as a short

    And so forth.

    --
    No, his mind is not for rent to any god or government.
    Please direct replies to drc -at- asus -dot- net.
     
    David R. Conrad, Apr 3, 2004
    #3
    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. DevBoy
    Replies:
    0
    Views:
    346
    DevBoy
    May 6, 2004
  2. =?Utf-8?B?QmlsbA==?=

    Problem with porting 1.1 code to 2.0

    =?Utf-8?B?QmlsbA==?=, Jan 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,020
    Steven Cheng[MSFT]
    Jan 6, 2006
  3. Harald Hein
    Replies:
    0
    Views:
    531
    Harald Hein
    Jul 22, 2003
  4. Roedy Green
    Replies:
    0
    Views:
    419
    Roedy Green
    Jul 22, 2003
  5. Arash Nikkar

    porting RXTX linux code to windows

    Arash Nikkar, Apr 12, 2006, in forum: Java
    Replies:
    5
    Views:
    1,714
    Martin Gregorie
    Apr 15, 2006
Loading...

Share This Page