How to convert byte[] c# to java byte[] - java

I have this code in C#
byte[] t = {6, 250, 215}.
But in Java is
byte[] t = {6, -6, -41}.
How to solve this problem?

How to solve this problem
the 1st is read about how java represents data types..:
byte: The byte data type is an 8-bit signed two's complement integer.
It has a minimum value of -128 and a maximum value of 127 (inclusive).
The byte data type can be useful for saving memory in large arrays,
where the memory savings actually matters. They can also be used in
place of int where their limits help to clarify your code; the fact
that a variable's range is limited can serve as a form of
documentation.
as #bradimus statements, java's byte is an 8-bits 2 complement signed int
but in C# you will never see a negative byte value
Hint:
if you consider to make a conversion...
-6 in java can be 256+(-6) = 250 in C#
and carefully consider the max and min in a java-byte if you need to convert from C# to java....

Related

NodeJS equivalent to bytes in Java

Im looking at some code in java, and I see lots of code using byte[], what is the equivalent to all of this in NodeJS / Javascript?
Here is the piece of code im looking at, Input is any base64 string
byte[] byteArray = Base64.decode(input, 0);
byte[] ivBytes = new byte[16];
System.arraycopy(byteArray, 0, ivBytes, 0, 16);
byte[] encryptedTextBytes = new byte[(byteArray.length - ivBytes.length)];
System.arraycopy(byteArray, 16, encryptedTextBytes, 0, encryptedTextBytes.length);
Thanks for helping me out.
byte is one of several primitive number data types in Java. Primitive data types specify the size and type of variable values. For instance, a byte takes the size of 1 byte and can store whole numbers from -128 to 127. Similarly short takes 2 bytes, int takes 4 bytes... so on and so forth. Refer this page for a complete list of data types with their respective sizes.
Unlike Java, Javascript has only one type of number and it stores numbers as double precision floating point values using 64 bits. So technically, there is no equivalent number type in JavaScript to the Java number types byte, short, int, long and float. We use var, let and const to declare variables in JavaScript and all those variables will be equivalent to double data type in Java.

Store signed value in Byte

I was trying to store byte value in a variable and trying to perform some logic based upon this calculation.
byte mByteValue = -129; // Holding byte value
Problem is I am always getting value 127, due to which my logic fails everytime.
Any specific reason behind this, why its behaving strange in my case?
A byte in java is a signed 8-bit value. 8 bits gives you 256 possible values, but since a byte is signed and can represent positive and negative values, those 256 values must be roughly split in half across the possible positive and negative values. So you can't store negative values past -128; in particular don't expect to be able to store -256.
What you're actually observing when your byte has the value 127 is known as overflow (see this wiki article)
If you need to manipulate values outside this range, as in your example code, or e.g. an unsigned byte, at some point you'll need to make use of a wider integer type, like short.
The standard libraries provide these limits as Byte.MIN_VALUE and Byte.MAX_VALUE (docs here and here).
The range of byte is from -128 to 127. You can not store any value beyond these range.
This is because byte is 8 bits. So the maximum positive number stored at byte is -
2^7 -1 = 127. // since the first bit is sing bit; 0 for positive
And minimum negative number stored at byte is-
2^7 = -128 //since the first bit is sign bit; 1 for negative.
And if you use unsigned byte the it would be 255.
To correctly convert a byte to an int use mByteValue & 0xFF. You can read more about the Two's complement here: https://en.wikipedia.org/wiki/Two%27s_complement.

Byte primitive in Java

I have been working on a project with images and I read on internet that working in byte for images solves alot of problems and improve efficiency. I tried to google it but didn't find any definition for it. So I came here to ask
What is byte in Java? What is it use?
You can read the Java tutorials.
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
byte is one of the primitive types of java. Is an abstraction of a byte (8 bit integer with sign).

How does a byte[] in java actually store data

If I have the following:
byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
I know that the size of each element would be one byte. But what I don't seem to understand is how would the integer 87 be stored in one byte? Or, how does the byte[] store data?
EDIT: I see that you can store -128 to 127 in a byte here in java. So, does that mean there is no way to store anything greater than or lesser than those numbers in a byte[]? If so, doesn't that limit the use of this? Or am not understanding the exact places to use a byte[].
A byte is 8 bits. 2^8 is 256, meaning that 8 bits can store 256 distinct values. In Java, those values are the numbers in the range -128 to 127, so 87 is a valid byte, as it is in that range.
Similarly, try doing something like byte x = 200, and you will see that you get an error, as 200 is not a valid byte.
A byte is just an 8-bit integer value. Which means it can hold any value from -2^7 to 2^7-1, which includes all of the number in {87, 79, 87, 46, 46, 46}.
An integer in java, is just a 4-byte integer, allowing it to hold -2^31 to 2^31 - 1
A Java byte is a primitive with a minimum value of -128 and a maximum value of 127 (inclusive). 87 is within the allowed range. The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
A byte[] is an Object which stores a number of these primitives.
I think the short answer is that byte[] stores bytes. The number 87 in your array above it a byte, not an int. If you were to change it to 700 (or anything higher than 127) you'd get a compile error. Try it.
You can use byte to store values of 8 bit in it which have a (signed) range from from -128 to 127.
With byte[] you can do some special operations like building Strings from a given bytestream and decode them with a desired Charset, and some functions will give you byte[] as their return value.
I don't know enough about the internals of the JVM but it might save memory though.
this is because, the computer stores values in a circular progression. not a linear progression like we learn in mathematics. it is because the memory is not infinite. it is finite. so every data type is storing values as a circular progression. to learn more go to this link and read the article.
https://medium.com/#hmsathyajith/numbering-system-edge-cases-of-java-237377553444

byte.parse() doesn't like 8 bit

This code
byte b = Byte.parseByte("10000000", 2);
throws an exception in Java. This should be -128 or 255.
Byte has 8 bit. Why can't I parse a 8 bit string?
The reason is down to the range of a byte in Java. Bytes are signed, so you can have anything from -128 ("-10000000") through to 127 ("1111111"), but no values outside that range.
The MAX_VALUE of a byte in java (or in C#) is 127 where as 10000000 return 128 which cannot be stored in a byte variable
What you can do treat the value as signed this way.
byte b = (byte) Integer.parseInt("10000000", 2);
Strictly, you passed not eight bits to parse method, but string representation of usual number with radix 2. And it may contain a sign character. Particularly, byte b = Byte.parseByte("-10000000", 2) works nice and gives -128.
That's not an 8-bit string, it's an 8-character string, and it's not being read the way you think it is...
From the MSDN documentation (here), you'll see that byte.Parse accepts strings in the integer format. So, you're trying to parse 10 million, not -1. The exception you're getting gives this away: you should see an OverflowException.
byte.parse("255") gives the effect you expect (byte is unsigned; using -128 also gives an overflow).

Categories

Resources