The online racing simulator
data type word
(12 posts, started )
#1 - abz1
data type word
does anyone know how to convert the data type "word" into a java "int".
Well, word size depends on the underlying CPU, but I guess it's most likely the same as the java int (32bit, afaik). I'm no Java coder so I have no idea if what I say makes sense, but a simple cast to int should do the trick.
#3 - abz1
well its not for a supercomputer hehe

regarding this

word 2-byte unsigned integer
So it's a UInt16, should be a short in Java.

I just realized you probably have a byte array, so here it goes
byte[] b; //Contains whatever data, one short consists of two bytes
short s = (short) (b[0] << 8 | b[1]);

Instead of 0 and 1 use the address wherever the short is located in your byte array.
#5 - abz1
thanks AndroidXP

[edit]
can someone confirm the verifyID is correct.

Id: VER
Version: 0.5U
Product: S2
VerifyId: 512

thanks
#6 - Stuff
Actually a Java short is 2 bytes signed, -32,768 to 32,767. Whereas a word is 2 bytes unsigned 0 to 65,536. A Java character is also 2 bytes unsigned, so that would be a better choice


public static int wordBytesToInt(byte[] bytes, int offset) {
return (char)(bytes[offset] << 8 | bytes[offset + 1]);
}

And yeah, 512 is correct for the InSimVer part of the version packet (no verify ID )
#7 - abz1
Im trying to convert a int into a 4-byte array for InSimPack. DOes anyone know how it can be done. My method below dosent seem to work.


char VerifyID = (char)(decodePacket[19] << 8 | decodePacket[20]);
int i = (int)VerifyID;

message[4]=(byte)((i & 0xff000000)>>>24);
message[5]=(byte)((i & 0x00ff0000)>>>16);
message[6]=(byte)((i & 0x0000ff00)>>>8);
message[7]=(byte)((i & 0x000000ff));

How does it fail? The bitwise logic looks correct.

--
rheiser
#9 - abz1
I get the error "InSim : ACK for unknown packet"

I am trying to send back the verifyID.

heres the full code if anyone can spot a mistake.


public REN(byte [] decodePacket)
{
/*********************REN**********************/
String id = new String(decodePacket, 0, 3).trim();
System.out.println("Id: "+id);

/*********************Sp0**********************/
int Sp0 = Integer.parseInt("" +decodePacket[4]);
System.out.println("Sp0: "+Sp0);

/*********************Sp1**********************/
int Sp1 = Integer.parseInt("" +decodePacket[5]);
System.out.println("Sp1: "+Sp1);

/*****************InSimVer********************/
char Ver = (char)(decodePacket[6] << 8 | decodePacket[7]);
System.out.println("VerifyId: "+(int)Ver);

//send verification
[B]new InSimPack((byte)'A',(byte)'C',(byte)'K',(int)Ver);[/B]
}

public InSimPack(byte a, byte b, byte c, int i)
{
byte [] message = new byte [8];
message[0] = (byte)a;
message[1] = (byte)b;
message[2] = (byte)c;
message[3] = 0x00;

//value
message[4]=(byte)((i & 0xff000000)>>>24);
message[5]=(byte)((i & 0x00ff0000)>>>16);
message[6]=(byte)((i & 0x0000ff00)>>>8);
message[7]=(byte)((i & 0x000000ff));

LFSInit.Com.sendPacket(message, message.length);
}

I haven't worked with InSim (yet ), but in the absense of any other replies...

I did notice that the "Verification ID" that you received is being decoded as a two byte value, and the one you're sending back is a four byte value. Unfortunately I can't find the InSim docs online, so I can't verify that's correct behavior. The only other thing I can imagine is the byte order being incorrect, but you're sending it in network order, so that's almost certainly not the case.

--
rheiser
Yeah, everything seems correct, but I'm definitely no expert when it comes to the exact bitwise difference between C and Java. But, on the other hand I do have that pesky verifyID working in my Java InSim app.. Here is what I have:

To get the two word bytes from a packet, this is what I use. It's not the code I posted earlier, that returns the true value. This one returns a value that will work in my InSimPack, not the correct value unfortunately.. Its very similar. I think the only thing needed to get the correct value is change the 3 to (length - 1), like the 2nd one. Code from: code snippets

public static int bytesToInt(byte[] bytes, int offset, int length) {
int temp = 0;

for (int i = 0; i < length; i++) {
int shift = (3 - i) * 8;
temp += (bytes[i + offset] & 0xFF) << shift;
}

return temp;
}

And here is my convert function used in my InSimPack to send that int back. Code from: experts exchange googley match

public static byte[] intToBytes(int value) {
byte[] bytes = new byte[4];

for(int i = 0; i < bytes.length; i++) {
int offset = (bytes.length - 1 - i) * 8;
bytes[i] = (byte)(value & (0xFF << offset) >>> offset);
}

return bytes;
}

While not perfect, it works for me for now until I have the time to poke around with the bits for a better solution. Oh, and I will also post my working Java package later (week I guess, after finals) to show the rest

edit: Well, here is the whole thing as a jar. Feel free to extract it, browse the incomplete docs and stuff and use the package. This one is working but not every event from InSim is handled properly yet. Most of them are. Like I said, give me a week or so and I will post a better version
(take out the .zip to run it, or the .jar to open it easily.. 1.5 required because of the enums and few "for each" loops)

#12 - abz1
Thanks guys, ill try the code later on today and let you know if I got it working.

Its quite intresting to see how people approach the problem and implement a solution. I have seen 2 java projects now, they are both completly different and even mine.. Ill be sure to post my set of java code aswell, so you can see how I done my implementation.

data type word
(12 posts, started )
FGED GREDG RDFGDR GSFDG