+ 12

Int to Byte

I wish to convert an int to a byte in Java. How does this work?

7th Nov 2018, 7:39 PM
Hamed Rahimvand
Hamed Rahimvand - avatar
23 Réponses
+ 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...
8th Nov 2018, 9:28 AM
Ahmed Mustafa
+ 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
7th Nov 2018, 7:52 PM
Valen.H. ~
Valen.H. ~ - avatar
8th Nov 2018, 4:58 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
256-120=136 (2s complement rule)
7th Nov 2018, 7:49 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 2
Valen.H. ~ nice answers tnx :)
7th Nov 2018, 7:51 PM
Hamed Rahimvand
Hamed Rahimvand - avatar
+ 2
What is a int?
8th Nov 2018, 8:33 AM
Endorey
+ 2
Ah! Thanks!
8th Nov 2018, 8:51 AM
Endorey
+ 2
Seems there is one extra 2: 2*2*2*2*2*2*2*2*2 = 256 really it's 512
8th Nov 2018, 8:22 PM
Микола Федосєєв
Микола Федосєєв - avatar
+ 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...
9th Nov 2018, 11:16 AM
Shiva Sankar
Shiva Sankar - avatar
+ 1
Valen.H. ~ system how to do it? for example: int 136 is equals to byte -120
7th Nov 2018, 7:44 PM
Hamed Rahimvand
Hamed Rahimvand - avatar
7th Nov 2018, 7:50 PM
Hamed Rahimvand
Hamed Rahimvand - avatar
+ 1
Endorey int is Integer
8th Nov 2018, 8:38 AM
Hamed Rahimvand
Hamed Rahimvand - avatar
+ 1
Integer? I have a lot to learn here
8th Nov 2018, 8:40 AM
Endorey
+ 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
8th Nov 2018, 8:49 AM
Hamed Rahimvand
Hamed Rahimvand - avatar
+ 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
8th Nov 2018, 9:30 AM
Hamed Rahimvand
Hamed Rahimvand - avatar
+ 1
Ahmed Mustafa finally i think your formula is complete.❤❤❤
8th Nov 2018, 9:39 AM
Hamed Rahimvand
Hamed Rahimvand - avatar
+ 1
int i1 = 10; byte b1 = (byte) i1
8th Nov 2018, 5:51 PM
ysmn
ysmn - avatar
+ 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.
9th Nov 2018, 11:09 AM
S.Vijayarasan Shivanandham
0
What is mean by stack over flow?
9th Nov 2018, 5:58 AM
S.Vijayarasan Shivanandham