Mapping Passing Byte Values from .NET to Java and from Java to .NET |
|
Contents
- SYMPTOMS
- CAUSE
- RESOLUTION
SYMPTOMS
Byte values are incorrect when calling a Java method from a .NET client and calling a .NET method from a Java client.
CAUSE
The type byte in .NET and Java have different ranges. Java only has the signed type, byte, which ranges from -127..128. In .NET, there are two different types - signed and unsigned. The .NET unsigned type is byte and has a range of 0..255. The .NET signed type is sbyte and has a range of -127..128.
J-Integra for .NET currently maps the Java signed type byte (-127..128) onto the .NET unsigned type byte (0..255), which means that the user will need to perform conversions on the byte values received using J-Integra for .NET.
RESOLUTION
When receiving bytes from a Java client, a possible conversion routine in C# looks like this:
sbyte toSignedByte(byte b) { return b < 128 ? (sbyte)b :(sbyte)(b - 256);}
When receiving bytes from .NET client, a possible conversion routine in Java looks like this:
int toUnsignedInt(byte b) { return b > =0 ? b : (256 + b);}
Status
The above conversion routines may be incorporated into a future release of J-Integra for .NET so that they are no longer required to be coded by the developer.