Re: silly unsigned/signed byte conversion question
From: aspa (aspa_at_foobar.int)
Date: 10/29/03
- Next message: eeyimaya: "Do you still choose java????"
- Previous message: Eddie McCreary: "Re: NetBeans 3.5.1 unusable"
- In reply to: Steve Horsley: "Re: silly unsigned/signed byte conversion question"
- Next in thread: Steve Horsley: "Re: silly unsigned/signed byte conversion question"
- Reply: Steve Horsley: "Re: silly unsigned/signed byte conversion question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Oct 2003 08:32:45 +0200
Steve Horsley wrote:
> Java has no such type as an unsigned byte.
you're right. i was misleadingly trying to refer to an octet value's
"unsigned" representation as short, int or long.
> <opinion>
> Whoever decided that byte was signed was as mad as a bucket of frogs, but
> the deed is done now, and people will shake their heads in wonder for ever
> more.
and some of us, in confusion too :)
> </opinion>
>
> For efficiency, I guess it is best to accept that bytes are signed, and
> not try to convert to other types at all. If you are working with bytes,
> then work with bytes. -128...+127. Live with it. What's the problem?
> Instead of "if b > 127", use "if b < 0". It can be done.
my original problem was reading 8-bit data from a character stream and
then interpreting that data like in the following peace of code:
File file = new File("/foo/data.d");
int len = (int)file.length();
byte[] buffer = new byte[len];
FileInputStream fis = new FileInputStream(file);
int n = fis.read(buffer, 0, len);
fis.close();
for(int i=0; i<n; i++) {
int d = buffer[i] & 0xff;
System.out.print(Integer.toHexString(d)+" ");
}
System.out.println();
this seems to work fine, now.
thanks for the help!
best regards,
-- aspa
- Next message: eeyimaya: "Do you still choose java????"
- Previous message: Eddie McCreary: "Re: NetBeans 3.5.1 unusable"
- In reply to: Steve Horsley: "Re: silly unsigned/signed byte conversion question"
- Next in thread: Steve Horsley: "Re: silly unsigned/signed byte conversion question"
- Reply: Steve Horsley: "Re: silly unsigned/signed byte conversion question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|