Path: csiph.com!news.swapon.de!weretis.net!feeder1.news.weretis.net!newsfeed.CARNet.hr!news.metronet.hr!news1.t-com.hr!.POSTED!not-for-mail From: Askfor Newsgroups: comp.lang.java.programmer Subject: Re: number to byte[] Date: Sun, 18 Oct 2015 12:44:18 +0200 Organization: T-Com Lines: 55 Message-ID: References: NNTP-Posting-Host: 93-138-185-14.adsl.net.t-com.hr Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ls237.t-com.hr 1445165058 4893 93.138.185.14 (18 Oct 2015 10:44:18 GMT) X-Complaints-To: abuse@t-com.hr NNTP-Posting-Date: Sun, 18 Oct 2015 10:44:18 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:32895 On 10/18/2015 12:13 PM, Askfor wrote: > On 10/17/2015 11:45 PM, Eric Sosman wrote: >> On 10/17/2015 5:03 PM, Askfor wrote: >>> On 10/17/2015 02:03 PM, Eric Sosman wrote: >>>> On 10/17/2015 4:02 AM, Askfor wrote: >>>>> I am trying to get 4 byte[] representation of an integer, which >>>>> might be >>>>> 32 bit long. >>>>> >>>>> Obviously, I could AND with 255 and take byteValue(), then shift 8 >>>>> times >>>>> to the right and repeat the process 3 times. Is there any more elegant >>>>> way ? >>>> >>>> I can't think of one, off-hand. >>>> >>>> But *why* do you want this four-byte array? Are you planning to >>>> write it to some output destination? If so, perhaps you should use a >>>> DataOutputStream (or other DataOutput implementation) and call its >>>> writeInt() method. Or, perhaps you should use an IntBuffer. What >>>> are you trying to do? >>>> >>> >>> 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. >> >> It's probably simplest just to read and write the int directly, >> letting the stream (or buffer) take care of assembling and disassembling >> the bytes. Classes that implement DataOutput and DataInput already have >> the necessary machinery, provided you want BigEndian byte order. If you >> need LittleEndian, I think you can manage it with a Buffer. >> >> (I didn't mention it before, but RandomAccessFile can also read and >> write BigEndian integers.) >> >>> I started with serialization, too, but I realized it won't work. >> >> I didn't suggest serialization. Perhaps you've confused DataOutput >> with ObjectOutput; they are not the same. >> > > I understand that, but then I will have to what the int is 32 bit. I am > not sure that int is 32-bit on all platforms and all VM's . I need > exactly 4 bytes to be written and read. Endian-ness wouldn't matter if > I could only be sure that int is 32-bit always. Java tutorial at > Oracle's site says that int is 32-bi 'by default', which I understand > as 'unless someone has different implementation'. > > The same source states firmly that writeInt methods write 4 bytes, always... I think I am going to isolate it in a separate method, so if any VM proves to be different, I am going to modify the method.