Further investigation reveals that

(def b (byte i))

is doing something equivalent to this internally:

byte b = Byte.parseByte(String.format("%d",i));

which does indeed throw a NumberFormatException if the decimal integer
representation given to it produces an out-of-range value (as it
should).

So - what I'm pleading for, is that (byte b) and (int i), (short s),
etc. should simply perform a masking operation (on the appropriate
number of least significant bits) in the way that java clearly does.


On Feb 12, 1:08 pm, timc <timgcl...@gmail.com> wrote:
> Sorry I did not make myself clear - I thought it was obvious given the
> previous postings on this subject.
> This java program:
>
> public class TestByte {
>         public static void main(String[] args)
>         {
>                 int i = 0x123456ab;
>                 byte[] b = new byte[1];
>                 b[0] = (byte) i;
>                 showInt("i",i);
>                 showByte("b[0]",b[0]);
>         }
>
>         private static void showInt(String s, int x)
>         {
>                 System.out.println(String.format("%s=%d(0x%08x 0x%02x)", s, 
> x, x,
> x));
>         }
>
>         private static void showByte(String s, byte x)
>         {
>                 System.out.println(String.format("%s=%d(0x%08x 0x%02x)", s, 
> x, x,
> x));
>         }
>
> }
>
> compiles and runs, producing this output:
>
> i=305419947(0x123456ab 0x123456ab)
> b[0]=-85(0x000000ab 0xab)
>
> But, this bit of clojure:
>
> (defn show [s x] (println (format "%s=%d(0x%08x 0x%02x)" s, x, x, x)))
> (def i 0x123456ab)
> (def b (byte-array 1))
> (aset-byte b 0 (byte i))
> (show "i" i)
> (show "b[0]" (aget b 0))
>
> throws this exception:
> java.lang.IllegalArgumentException: Value out of range for byte:
> 305419947
>
> Shouldn't these two programs be equivalent?
> Presumably they are not because the effect of
>
> int i = (byte) b;
>
> is NOT the same as
>
> (def i (byte b))
>
> -- but surely it should be the same?
>
> On Feb 11, 5:24 pm, Andy Fingerhut <andy.finger...@gmail.com> wrote:
>
> > What can you not do with the signed byte type and arrays of bytes  
> > (Java byte[] and Clojure (byte-array ...))?
>
> > I believe these are frequently used for Java I/O, and can be used for  
> > Clojure I/O as well.
>
> > Andy
>
> > On Feb 11, 2011, at 9:22 AM, timc wrote:
>
> > > How on earth is one supposed to do communication programming (not to
> > > mention handling binary files etc) without an unsigned byte type?
>
> > > I see that this issue has been talked about vaguely - is there a
> > > solution?
>
> > > Thanks
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Clojure" group.
> > > To post to this group, send email to clojure@googlegroups.com
> > > Note that posts from new members are moderated - please be patient  
> > > with your first post.
> > > To unsubscribe from this group, send email to
> > > clojure+unsubscr...@googlegroups.com
> > > For more options, visit this group at
> > >http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to