2

Forgive if this question is incredibly poorly phrased, but I'll do my best to explain what is (to me) a rather complex situation. I am fully aware that I am in way over my head here, experience wise, but I am hoping one of you can help me.

I come to you brighter minds with this, because I am in a complete rut and there appears to be no light at the end of the tunnel.

I have a file I wish converted with C# to a byte array, because the byte array will be sent as an argument into a Java method.

So I thought:

byte[] a = System.IO.File.ReadAllBytes(filePathHere);

Now, this works, and all is peaceful in the world. But the problem arises when i try to send a into my Java method.

The error it throws me when i try to send this to my Java file by means of JSON is:

org.codehaus.jackson.JsonParseException: Numeric value (181) out of range of Java byte

Now, after some (in vain) Googling, I came to the conclusion that C# thinks its being helpful by converting the file into a byte array that contains items with value between 0 and 255.

While Java is expecting to get as an argument for my method is a byte array with values reaching from -128 to 128.

In an ideal world, I would love it if any of you knew how to convert my file C# into a byte array with values reaching from -128 to 128.

However, if this is in fact impossible, I would also be open to, in Java, converting my byte array with value between 0 and 255 to a more desired -128 to 128 format.

Is what I describe impossible, or does someone know how this can be done?

I deeply appreciate any help anyone can provide. The more detailed and noob-friendly, the better!

4

2 Answers 2

0

Converting types in C# is relatively trivial:

File.ReadAllBytes(filePathHere).Select(b=>(sbyte)b).ToArray();

As a side note, files are supposed to be made of unsigned bytes, so there's no need for sarcasm ("C# thinks its being helpful"). It's just that Java doesn't have proper data types to deal with it.

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

Comments

0

This is because Java works with signed numeric primitives only, while c# bytes are unsigned 8 bit integers.

How to solve in the Java side:

  • You can read it in a integer/short array and then force cast to a byte array, byte by byte.
  • If you need to work with numbers in the interval [0-255], you should use a integer or short array.

How to solve in C# side:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.