Results 1 to 7 of 7
  1. #1
    ighor10 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default Translate from C to java

    hello guys,

    i am trying to translate a little code from C to java, but i cant..

    please help me..


    the first part is:




    struct tPacket
    {
    WORD size;
    WORD opcode;
    BYTE securityCount;
    BYTE securityCRC;
    BYTE data[8186];
    }; // A genrel Packet

    struct tPacket_5000_E
    {
    WORD size;
    WORD opcode;
    BYTE securityCount;
    BYTE securityCRC;
    BYTE flag;
    BYTE blowfish[8];
    DWORD seedCount;
    DWORD seedCRC;
    DWORD seedSecurity[5];
    };

    struct tPacket_5000_10
    {
    WORD size;
    WORD opcode;
    BYTE securityCount;
    BYTE securityCRC;
    BYTE flag;
    DWORD challenge[2];
    };





    and the second part is :


    int rcv = 0;
    tPacket Packet;

    do
    {
    recv(Socket, (char *)Packet, sizeof(Packet), 0);
    if (rcv > 0)
    {
    if (Packet.opcode == 0x5000 && Packet.size == 0x25)
    std::cout << "First Packet received";
    else if (Packet.opcode == 0x5000 && Packet.size == 0x09)
    std::cout << "Second Packet received";
    else if (Packet.opcode != 0x5000)
    {
    pritnf("%.4X %.4X %.2X %.2X", Packet.size, Packet.opcode, Packet.securityCount, Packet.securityCRC);
    for(int i = 0; i < Packet.size; i++)
    {
    printf("%.2X", Packet.data[i]);
    }
    }
    }
    while(rcv != SOCKET_ERROR);





    please help me guys :S

    thx a lot.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ighor10 View Post
    hello guys,

    i am trying to translate a little code from C to java, but i cant..

    please help me..
    ....
    ....
    please help me guys :S

    thx a lot.
    Hello and welcome to the forum. You may have a misunderstanding about what this forum is all about, and I'd like to clarify this for you. For one, this isn't a dump my work and have someone do it for me type forum (for that try RentACoder.com). Instead we'll be happy to try to help you with any problems you may be having with your code and your problems. So let's see your attempts first and tell us of any problems you may be having with your code.

    Best of luck.

  3. #3
    ighor10 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    ok, im sorry for this.

    my problem is, in this code, that im trying to translate is:

    to do this "struct", i need to do new class,

    and to my new class work, i need to set the constructors,

    and now, i dont know how to do it, because i have 2 variables of type BYTE, and i dont know what is WORD in java.

    if i dont do it, i cant instance a new object from PACKET..

    i realy need help to do it.
    i am pressed to make this working.


    sorry for an inconvenience and for my english :D

    thx

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    The trouble is that the memory layout of a C struct is different than the memory layout of a Java instantiation of a class. Therefore the recv( ... ) call in Java should read all its members (primitives) one by one and it should fill every single member of your Java object one by one, i.e. read ints and bytes and set the Java object members individually. Also the endianess can be a show stopper, depending on the architecture you're running the C code on. Probably a BYTE == byte, WORD == short and DWORD == int.

    kind regards,

    Jos

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Too bad Java doesn't have structs. :/
    I die a little on the inside...
    Every time I get shot.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm not sure that it needs structs, but perhaps it would interop more easily if it had unsigned numbers -- though I'm certainly no pro in this.

  7. #7
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    To program (good code) in Java, you need to think different than when u r programming in C. Both languages use different paradigms, so I think is best, if you tell us what ur code is supposed to do, and there may be a different solution, than then one you implemented in C. More adequate for the situation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •