Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Convert between types in Java

 
Post new topic   Reply to topic Printable version
 View previous topic  When making a bot.. Post :: Post Substats Error..  View next topic  
Author Message
Smong
Guest


Offline

PostPosted: Mon Jan 20, 2003 2:43 am    Post subject: Convert between types in Java Reply to topic Reply with quote

If you want to make a Java bot check this out: http://java.sun.com/docs/books/tutorial/networking/datagrams/clientServer.html

I'm having problems with DatagramPacket, since everything is done in bytes, how can I convert an int to 4 bytes in an array? (Assuming it's an UInt32, which I don't know because Java doc's aren't too helpful). Also conversion of Strings and byte arrays back to ints or Strings. If this was C I would probably be using a struct here, but I don't think Java has any.

Having to do this for every packet is going to get annoying and hard to read:
Code: Show/Hide

    buf = new byte[8];
    buf[0] = 0x00; //Core packet
    buf[1] = 0x01; //Type
    buf[2] = 0x00; //Ticks, System.currentTimeMillis()
    buf[3] = 0x00;
    buf[4] = 0x00;
    buf[5] = 0x01;
    buf[6] = 0x00; //Version
    buf[7] = 0x01;
Back to top
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Mon Jan 20, 2003 8:32 am    Post subject: Re: Convert between types in Java Reply to topic Reply with quote

Smong wrote:
I would probably be using a struct here, but I don't think Java has any.


A struct is just a class with no functions. Java CAN do this, just can't call it a struct.

Code: Show/Hide

public class myStruct
{
    public int myInt;
    public String myString = "Hello!";
}
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Mon Jan 20, 2003 8:45 am    Post subject: Re: Convert between types in Java Reply to topic Reply with quote

Smong wrote:
how can I convert an int to 4 bytes in an array? (Assuming it's an UInt32, which I don't know because Java doc's aren't too helpful).


Code: Show/Hide

int myInt = (0x6 << 24) + (0xB << 16) + (0xD << 8) + (0xF);
byte[] myBytes = new byte[4];
byte[3] = (byte)((myInt >> 24) & 0xF);
byte[2] = (byte)((myInt >> 16) & 0xF);
byte[1] = (byte)((myInt >> 8) & 0xF);
byte[0] = (byte)((myInt) & 0xF);
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Tue Jan 21, 2003 2:31 pm    Post subject: Reply to topic Reply with quote

Thanks. Here is some code that works on bytes (the above code only seemed to work on 4 bits of 8 bit bytes).
Code: Show/Hide

  public final static long getLong(byte[] msg, int pos) {
    return (long)(
      ((msg[pos+3] & 0xFF) << 24) +
      ((msg[pos+2] & 0xFF) << 16) +
      ((msg[pos+1] & 0xFF) <<  8) +
       (msg[pos  ] & 0xFF)          );
  }

  public final static void setLong(byte[] msg, int pos, long val) {
    msg[pos+3] = (byte)(val >> 24);
    msg[pos+2] = (byte)(val >> 16);
    msg[pos+1] = (byte)(val >>  8);
    msg[pos  ] = (byte)(val      );
  }
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Tue Jan 21, 2003 4:12 pm    Post subject: Reply to topic Reply with quote

Right, sorry, didn't test it.
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Bot Questions All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 751 page(s) served in previous 5 minutes.

phpBB Created this page in 0.497540 seconds : 30 queries executed (93.7%): GZIP compression disabled