Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade Newsgroups: comp.lang.java.programmer Subject: Re: number to byte[] Date: Mon, 19 Oct 2015 10:43:36 +0100 Lines: 18 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net WW6C/iXa09gs9fCzx/RztgC0oP4etYYgam3r0A7FTVV9pVVHPi Cancel-Lock: sha1:LW2N3AHcBDSY4WGaafgLQ1LCy+w= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:32898 On 17/10/15 22:03, Askfor wrote: > It is going to be a part of a data header, which needs to be of fixed length. Also it will contain a value of an offset > which cabn't be longer than 32-bits. So, I don't care how big int actually is, as long as it is big enough to hold 32 > bit unsigned value. int is signed, as are all Java integers. int won't hold a 32bit unsigned integer. It will hold a 4-byte value as long as you don't attempt to interpret that value as a number, for example by performing any arithmetic on the value. If the MSB of the MSbyte is 1, it will be interpreted as being a negative number. Don't forget that byte is also signed, so the MSbyte would also be negative. If you need a 32bit unsigned value then you must promote it to 64bit, without sign extension. Of course, you could write your own UnsignedInteger class, with appropriate storage, and input/output methods. In the long run it might cause you the least surprises. For storing/retrieving values in byte[] arrays ByteBuffer is one way to go.