Re: Handling of unsigned bytes

2011-02-13 Thread Rasmus Svensson
To turn a signed byte (-128 to 127) into an unsigned one: (bit-and the-byte 0xff) The byte (for example 0x80, which is negative) will be extended to an int (0xff80) and anded with 0x00ff (and you get 0x0080, which is positive). The javadoc for the methods of DataInput[1] contain form

Re: Handling of unsigned bytes

2011-02-12 Thread timc
Thanks for the help. Sorry I got agitated about this - it was just that my code (that was doing lots of byte handling) worked with a previous version of clojure and then stopped working. Thanks Ken, I shall use your little workaround. On Feb 12, 10:04 pm, Ken Wesson wrote: > On Sat, Feb 12, 2011

Re: Handling of unsigned bytes

2011-02-12 Thread Ken Wesson
On Sat, Feb 12, 2011 at 4:54 PM, Aaron Cohen wrote: > On Sat, Feb 12, 2011 at 4:42 PM, Ken Wesson wrote: >> On Sat, Feb 12, 2011 at 8:28 AM, timc wrote: >>> (def b (byte i)) >>> >>> is doing something equivalent to this internally: >>> >>> byte b = Byte.parseByte(String.format("%d",i)); >> >> Wh

Re: Handling of unsigned bytes

2011-02-12 Thread Aaron Cohen
On Sat, Feb 12, 2011 at 4:42 PM, Ken Wesson wrote: > On Sat, Feb 12, 2011 at 8:28 AM, timc wrote: >> Further investigation reveals that >> >> (def b (byte i)) >> >> is doing something equivalent to this internally: >> >> byte b = Byte.parseByte(String.format("%d",i)); > > What the HELL? > > That'

Re: Handling of unsigned bytes

2011-02-12 Thread Ken Wesson
On Sat, Feb 12, 2011 at 8:28 AM, timc wrote: > Further investigation reveals that > > (def b (byte i)) > > is doing something equivalent to this internally: > > byte b = Byte.parseByte(String.format("%d",i)); What the HELL? That's incredibly icky and inefficient. :) Why not if (i < 128 || i >

Re: Handling of unsigned bytes

2011-02-12 Thread Andy Fingerhut
On Feb 12, 2011, at 6:53 AM, Aaron Cohen wrote: On Sat, Feb 12, 2011 at 8:28 AM, timc wrote: 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 NumberFormatExceptio

Re: Handling of unsigned bytes

2011-02-12 Thread Brian Hurt
On Fri, Feb 11, 2011 at 12:22 PM, 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 > > Java guarantees

Re: Handling of unsigned bytes

2011-02-12 Thread Aaron Cohen
I should also mention that for this sort of stuff, people often get tired of raw bit-twiddling and move to something like a wrapper around protocol buffers (such as https://github.com/ninjudd/clojure-protobuf) or a DSL such as gloss (https://github.com/ztellman/gloss/wiki). --Aaron -- You receiv

Re: Handling of unsigned bytes

2011-02-12 Thread Aaron Cohen
On Sat, Feb 12, 2011 at 8:28 AM, timc wrote: > 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 > representati

Re: Handling of unsigned bytes

2011-02-12 Thread timc
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).

Re: Handling of unsigned bytes

2011-02-12 Thread timc
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];

Re: Handling of unsigned bytes

2011-02-11 Thread Richard Lyman
I have to deal with them when processing AMF packets, and I use the Netty library - it's amazing, you should look into it. http://www.jboss.org/netty -Rich On Fri, Feb 11, 2011 at 10:22 AM, timc wrote: > How on earth is one supposed to do communication programming (not to > mention handling bi

Re: Handling of unsigned bytes

2011-02-11 Thread jk
Well, until you start doing "bit-shift-right"s and the sign bit (high bit) doesn't go to 0 after shifting it down. Actually you typically need to represent individual bytes as ints and write them back out using writeByte() when you're trying to do low level bit-twiddling. It's a pain but it works.

Re: Handling of unsigned bytes

2011-02-11 Thread B Smith-Mannschott
On Fri, Feb 11, 2011 at 20:34, Stuart Sierra wrote: > Java doesn't have any unsigned types, and that's not really something we can > change.  Java libraries that need to do binary I/O tend to work with byte > arrays and handle individual bytes as ints. > -Stuart Sierra > clojure.com Well, except

Re: Handling of unsigned bytes

2011-02-11 Thread Stuart Sierra
Java doesn't have any unsigned types, and that's not really something we can change. Java libraries that need to do binary I/O tend to work with byte arrays and handle individual bytes as ints. -Stuart Sierra clojure.com -- You received this message because you are subscribed to the Google Gr

Re: Handling of unsigned bytes

2011-02-11 Thread Miki
> 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? > http://www.darksleep.com/player/JavaAndUnsignedTypes.html ? -- You

Re: Handling of unsigned bytes

2011-02-11 Thread Andy Fingerhut
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 commun

Handling of unsigned bytes

2011-02-11 Thread timc
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 "