On May 8, 2009, at 10:42 PM, tmountain wrote:

I'm working on a project that makes use of a lot of byte arrays, and
I'm having an issue with certain bytes overflowing to negative values.
I know they're being stored properly because I'm also generating hex
dumps from time to time which indicate the real value is there. Can
someone tell me how to go about making comparisons with the actual
values?

user=> (byte 254)
-2

(def my-byte (make-array (Byte/TYPE) 1))
(aset my-byte 0 (byte 254))
(println (format-hex-string (HexByte/byteArrayToHexString my-byte)))
... returns "0xFE" ...

user=> (= (byte 254) 0xFE)
false

The byte type is always signed in Java. Its range is -128 to 127.

It's documented here:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Byte.html

If you need to compare a byte to another value, one way is coerce both to byte for the comparison:

user=> (= (byte 254) (byte 0xFE))
true

--Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to