Java code - negative byte in byte array in C #

In Java, I have the following line:

new byte[]{59, 55, 79, 1, 0, 64, -32, -3}; 

However, in C #, I cannot use negative bytes in a byte array. I tried to transfer it to byte, and it did not work. What can I do? thanks!

+4
source share
4 answers

Because there is no sign in C # bytes. In Java, bytes are signed.

From Byte Structure

A byte is an immutable value type that represents unsigned integers with values โ€‹โ€‹that range from 0 (which is represented by the Byte.MinValue constant) to 255 (which is represented by the Byte.MaxValue constant)

From Primitive Data Types

The byte data type is an 8-bit two-digit integer. It has a minimum value of -128 and a maximum value of 127

What can I do?

You can use sbyte in C #, which is an 8-bit signed integer.

The value type SByte represents integers with values โ€‹โ€‹from negative 128 to positive 127.

how

 sbyte sb = new sbyte[] {59, 55, 79, 1, 0, 64, -32, -3}; 
+8
source

To stay closer to byte java idea, use sbyte :

 var data = new sbyte[] { 59, 55, 79, 1, 0, 64, -32, -3 }; 

and you probably have to change the other variables to sbyte .

+6
source

Bytes are signed in Java, and unsigned bytes in C #. You can convert from signed to unsigned by adding 256 to the number, so (in your example) -32 becomes 224 and -3 becomes 253.

As Ichabod Clay says (in the comments), you can also use the sbyte type so you don't need to do this conversion. Personally, I donโ€™t like signed bytes, so if it were for literal data, I would just convert it once and do it, but you have a choice. Hooray.: - D

+4
source

In general, you can simply translate the signed to unsigned and vice versa. Most of the time, you are still interested in the bit value of bytes. They remain unchanged in both systems (because numbers with two additions were defined). Now, if you have grown to a value that uses more bits, and you want to convert to an unknown positive number, just use bitwise AND using 1 bit for the number of bits you need.

In c # you can use

 int negInt = (sbyte) b; byte anotherB = (byte) negInt; 

and in Java use:

 int posInt = b & 0xFF; byte anotherB = (byte) posInt; 

You can use this functionality when you need the value of bytes as numbers. Otherwise, you can simply store the bit in an array of bytes - signed or unsigned.

This method should also work for specifying the entire byte array - as you used in the C # example:

 new byte[] {59, 55, 79, 1, 0, 64, (byte)-32, (byte)-3}; 

or even

 new byte[] {(byte)59, (byte)55, (byte)79, (byte)1, (byte)0, (byte)64, (byte)-32, (byte)-3}; 

You may need to add unchecked() if you are using C #.

The same goes for Java:

 new byte[] { (byte) 255, etc. } 

for this kind of conversion, you might want to learn how to use find / replace regular expressions in your favorite editor.

In the end, it takes a little play with casting and bitwise AND to get an idea about it. As soon as you understand, he will never leave you.

+1
source

Source: https://habr.com/ru/post/1498823/


All Articles