1

I am using a tool that is sending binary data to a Ponte (Node.js) application that forwards this message (without changes) to an AMQP broker.

Overall it is: Java -> JavaScript -> Java And I do have a strange conversion of the binary data. Here are the HEX values in the order they appear:

When I prepare a binary data set it looks like this:

[4, -30, -30, -9, -115, 0, 1, 0, 1, 0, 96, -32, 46, 0, 0, 0]

When it arrives in JavaScript (Ponte) it looks like this:

[4, 226, 226, 247, 141, 0, 1, 0, 1, 0, 96, 224, 46, 0, 0, 0]

Here the negative decimals turned into positive decimals. If you "substract" those values, then you can see that they do have a value of 256

Now I am sending this data back from JavaScript to Java via an AMQP broker.

In Java my binary data now looks like this:

[4, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, 0, 1, 0, 1, 0, 96, -17, -65, -67, 46, 0, 0, 0]

It is still similar like before, but all the decimals that turned from negative to positive now turned to

-17, -65, -67,

In Java I am working with byte arrays and in JavaScript I am working with a Buffer. Can anyone explain where this strange behaviour does come from?

Let me know if you need more info about my problem.

Thank you a lot!

2
  • 2
    Java has signed bytes; but in order to really understand what is going on; you probably need to provide a minimal reproducible example Commented Feb 28, 2017 at 15:44
  • @GhostCat more generally, signed numbers (=primitives) Commented Feb 28, 2017 at 15:45

2 Answers 2

3

This is clearly a signed/unsigned issue between Ponte and java

[4, -30, -30, -9, -115, 0, 1, 0, 1, 0, 96, -32, 46, 0, 0, 0]

[4, 226, 226, 247, 141, 0, 1, 0, 1, 0, 96, 224, 46, 0, 0, 0]

if yoou look carefully, negative values are turning in its complement equivlaent by adding 256 to its value...

on the Ponte side you can do the math to transform back this array into a signed 8 bits number

[4, 226, 226, 247, 141, 0, 1, 0, 1, 0, 96, 224, 46, 0, 0, 0]

just check that all numbers biggges than 127 were negative in the java side, so you need to do :

if number > 127 then number -=256

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you as well! Will I be able to handle this within Ponte? I cannot change the data that is coming from the first Java application.
HI @MK, sure, convert the values back.... if a number ig bigger than 127 then it was negative, so you need to rest 256 :)
But don't I have the same problem then? This would mean I have a signed array in JavaScript again. Or is it just the conversion at the beginning? And would this affect any strings? Currently I am sending half string, half binary data
As I thought. As soon as I subtract it jumps to the higher number again. Seems like Buffer just don't like signed values. So now I know where the problem is, but the solution unfortunately did not help.
1

It all has to do with you (I mean the programming language) interpret 8 bits. In Java, the first bit is a sign bit, whereas in JavaScript the first bit is treated like the largest bit of the number. Data-wise, the two values are equivalent. If you want to prevent this switching, you should use a larger primitive value like a char or an int

1 Comment

Thank you for your reply! I already thought something similar. The problem is, I cannot change the data that is coming from Java, because right now I am just simulating this binary data. Later it will come from on external source I do not have control over. But I do have control over the JavaScript part and when the Java part the second message is sent to (but there the data already arrives in wrong format). Will I be able to handle it in the JavaScript part? Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.