Java signed and VB6 unsigned

Hello everyone

I know I made 2 posts in the past few days about this problem, but at that time, I wasn't sure this IS the problem or some other issues I might not know about. So the posts were very confusing. I thought I might make this new one to simplify things.

A quick overview, I need to convert a VB6 client to Java. The server is in VB6 and I can't modify its code. So no access. I do have the source code and the working version of the VB6 client however.

The problem I have is how to pass byte value more then 127 to the VB server. Java use signed byte (-127 to 127) while VB uses unsigned. Using packet sniffing program (Ethereal), I know the VB6 client is passing bytes values 0x85, 0x9E to the server, while my Java client (in doing exact same thing) can only pass value 0x3F 0x3F instead. Anything below 127 (i.e alphabetical letters) is the same between the 2 of program.

So my question is how one can pass signed Java bytes into VB6 unsigned bytes. I did some search on google and found this

http://www.javaworld.com/javaforums/showflat.php?Cat=&Board=javabeginner&Number=9208&page=15&view=collapsed&sb=7&o=&fpart=1

but still didn't help me. I still only can pass bytes below 127.

Any help is appreciate. Thank you in advance!

[1321 byte] By [BuggyVBa] at [2007-11-27 6:25:23]
# 1
Well, how about using a Java short or int? Or maybe use Java's nio package, java.nio.ByteBuffer might be helpful.
thomas.behra at 2007-7-12 17:45:09 > top of Java-index,Java Essentials,Java Programming...
# 2
When you write out a byte, it loses it's sense of signedness.-1 is 255, Java just treats it as -1 because of the way it was designed.This means that when working with bytes in Java, you need to & 0xFF it (it gets promoted to int then).How are you sending the
-Kayaman-a at 2007-7-12 17:45:09 > top of Java-index,Java Essentials,Java Programming...
# 3

To understand how things work you have to distinguish between the "bits in the byte", which can vary between 0x00 and 0xff in Java as well as in VB, and the interpretation of these bits, which is different.

While 0xff might be interpreted as 255 in VB, it is a -1 in Java.

So if you want to send the byte value 0x85, just write e.g.

byte by = (byte)0x85;

and the byte will contain the correct bits. You can also use casting from an int or short and it will give correct results:

int i = 255;

byte by = (byte)i;

will leave by with 0xff.

In the other direction, you might consider converting your bytes to short or int. In this case you have to handle the negative values explicitly, e.g.

byte by = (byte)0x85;// you might have received this value from VB

int i = by & 0xff;

leaves the variable i with the value of 133.

martin@worka at 2007-7-12 17:45:09 > top of Java-index,Java Essentials,Java Programming...
# 4
The data is sent as strings. The strings are somewhat encoded by some functions (so characters can be of high values, i.e more then 178). And the strings are sent through socket class.
BuggyVBa at 2007-7-12 17:45:09 > top of Java-index,Java Essentials,Java Programming...
# 5

So if you want to send the byte value 0x85, just write e.g.

byte by = (byte)0x85;

You are **** right... If I do something like

byte[] b = new byte[2];

b[1] = ((byte)0x85);

b[2] = ((byte)0x90);

send(new String(b)); // send is my send function

Then VB can understand ... Wow, I got the password right for the first time in 2 days.

Thanks so much martin@work, as well as everyone who tried to help me (big thanks to jsalonen as well). You guys are wonderful!

BuggyVBa at 2007-7-12 17:45:09 > top of Java-index,Java Essentials,Java Programming...