I believe these are frequently used for Java I/O, and can be used for
Clojure I/O as well.
Andy
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
Well, except for char. That's unsigned. An odd language, Java.
// Ben
-Rich
For the record, unchecked coercions are in progress:
http://dev.clojure.org/jira/browse/CLJ-441
--Aaron
And until those are available, I have sometimes worked around it by
making a tiny function that takes an int in the range 0..255 and
returns a byte:
(defn ubyte [val]
(if (>= val 128)
(byte (- val 256))
(byte val)))
Andy
What the HELL?
That's incredibly icky and inefficient. :)
Why not
if (i < 128 || i > 127)
throw something;
else
b = (byte)i;
which still bounds-checks the conversion but has got to be orders of
magnitude faster?
Don't worry, it's not actually doing that:
https://github.com/clojure/clojure/blob/f128af9d36dfcb268b6e9ea63676cf254c0f1c40/src/jvm/clojure/lang/RT.java#L902
Oh, good. That's basically what I said it should do.
(bit-and the-byte 0xff)
The byte (for example 0x80, which is negative) will be extended to an
int (0xffffff80) and anded with 0x000000ff (and you get 0x00000080,
which is positive).
The javadoc for the methods of DataInput[1] contain formulas for how
to convert shorts and ints and longs too. DataOutput[2] contains
comments for the opposite opereration.
1. http://download.oracle.com/javase/6/docs/api/java/io/DataInput.html
2. http://download.oracle.com/javase/6/docs/api/java/io/DataOutput.html