+ 12
Int to Byte
I wish to convert an int to a byte in Java. How does this work?
23 Respostas
+ 12
Let me try to give the simple formula:
Byte ranges between -128 and 127 inclusive. When int is assigned to the value between -128 to 127 inclusive, the conversion to the byte of that number will remain the same.
If int is less than -128, we just add than number with 256 to convert into byte.
If int ranges between 128 to 256 inclusive, that number will be deducted with 256.
If the number is greater than 256, We will divide that number with 256 and take the reminder...
+ 14
ints are 8 bytes long:
2*2*2*2*2*2*2*2*2 = 256
byte is one byte long
signed/negative numbers start from max to zero, opposite of positives
positives: 0, 1, 2, 3, 4 ....
negatives: 256 (or 0), 255 (or - 1), 254 ....
negatives are represented as reverse positive in memory
+ 13
Read mine answer here ☺
https://www.sololearn.com/Discuss/1011899/?ref=app
+ 7
256-120=136
(2s complement rule)
+ 2
Valen.H. ~ nice answers tnx :)
+ 2
What is a int?
+ 2
Ah! Thanks!
+ 2
Seems there is one extra 2:
2*2*2*2*2*2*2*2*2 = 256
really it's 512
+ 2
If int is less than -128, we just add than number with 256 to convert into byte.
If int ranges between 128 to 256 inclusive, that number will be deducted with 256.
If the number is greater than 256, We will divide that number with 256 and take the reminder...
+ 1
Valen.H. ~ system how to do it? for example:
int 136 is equals to byte -120
+ 1
Valen.H. ~ this is an example
https://code.sololearn.com/cDZA9UpYKYEs/?ref=app
+ 1
Endorey int is Integer
+ 1
Integer? I have a lot to learn here
+ 1
Endorey
The set of integers, denoted Z, is formally defined as follows:
Z = {..., -3, -2, -1, 0, 1, 2, 3, ...}
int size is 16bits, means between -32768 , +32767
+ 1
deepak sharma Valen.H. ~ Endorey Ahmed Mustafa
i find easy solution for caculate it mentally, for example:
8666 to byte->
8666%(modulus) 256 = 218
and now 218 - 256 = -38 byte :)
edit:
this is for 256 and bigger
+ 1
Ahmed Mustafa finally i think your formula is complete.❤❤❤
+ 1
int i1 = 10;
byte b1 = (byte) i1
+ 1
If you want to convert data type int to byte
using the cast operator
int val;
byte val1=(byte)val;
this parentheses inside you given which data type you want to convert
this easy way to convert the data type.
0
What is mean by stack over flow?