Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32896

Re: number to byte[]

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: number to byte[]
Date 2015-10-18 08:40 -0400
Organization A noiseless patient Spider
Message-ID <n003so$1r8$1@dont-email.me> (permalink)
References <mvsv9r$ohb$1@ls237.t-com.hr> <mvtd9u$c4m$1@dont-email.me> <mvud46$eat$1@ls237.t-com.hr> <mvufdc$cih$1@dont-email.me> <mvvrbe$3uf$1@ls237.t-com.hr>

Show all headers | View raw


On 10/18/2015 6:13 AM, Askfor wrote:
> [...]
> 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'.

     A Java `int' is defined as having the value range -2147483648 to
2147483647, inclusive.

http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1

We cannot know how a particular JVM chooses to represent this range
of values internally, because Java has no way to examine or fiddle
with the representation: You can look at an `int' and determine its
value, but you cannot "see" the individual pieces of that value.  (Not
even by bit-wise operations like shifting and masking: The results of
these operations are defined in terms of the values of their operands,
not their physical structure.)  So, it is possible -- highly unlikely,
but possible -- that a particular JVM might encode `int' values as
96-bit Gray-coded objects, but there's no way you could tell.

     ... which makes no difference at all to you, because you don't
care even one little bit (sorry) about what the JVM does in private.
You care only about what the JVM does when it "exports" the `int'
value, making it visible to the gaze of the outside world.  When
that happens, the bytes produced by DataOutput and the like *are*
specified, and are independent of whatever the JVM does internally.

     Conclusion: Regardless of what the JVM does in the privacy of its
own bedroom, the observable behavior is *always* "as if" an `int' had
exactly 32 bits.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

number to byte[] Askfor <askfor@nowhere.net> - 2015-10-17 10:02 +0200
  Re: number to byte[] Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-17 08:03 -0400
    Re: number to byte[] Askfor <askfor@nowhere.net> - 2015-10-17 23:03 +0200
      Re: number to byte[] Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-17 17:45 -0400
        Re: number to byte[] Askfor <askfor@nowhere.net> - 2015-10-18 12:13 +0200
          Re: number to byte[] Askfor <askfor@nowhere.net> - 2015-10-18 12:44 +0200
            Re: number to byte[] Lew <lewbloch@gmail.com> - 2015-11-29 23:07 -0800
          Re: number to byte[] Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-18 08:40 -0400
      Re: number to byte[] Nigel Wade <nmw@ion.le.ac.uk> - 2015-10-19 10:43 +0100
        Re: number to byte[] Wayne <nospam@all.invalid> - 2015-10-20 00:45 -0400
        Re: number to byte[] Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2015-10-20 13:35 -0700
  Re: number to byte[] Lothar Kimmeringer <news200709@kimmeringer.de> - 2015-10-25 10:22 +0100

csiph-web