Re: silly unsigned/signed byte conversion question

From: aspa (aspa_at_foobar.int)
Date: 10/29/03


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


Relevant Pages

  • Re: silly unsigned/signed byte conversion question
    ... >> Java has no such type as an unsigned byte. ... i was misleadingly trying to refer to an octet value's ... > my original problem was reading 8-bit data from a character stream and ...
    (comp.lang.java.programmer)
  • Re: Cohens paper on byte order
    ... > length of the complete representation was known in advance. ... Some registers on some architectures are reserved for floating-point values, ... that operate on octets, the octet values correspond unambiguously to integers ... >> What is the type of the abstraction function associated with this ...
    (sci.crypt)
  • Re: Instantiate a class with a variable as its name
    ... I want to create an instance of a class, but want to refer to the name ... of the instance, at creation time, via a variable. ... All of the Java documentation regarding anonymity refers to inner ... Is this possible in Java? ...
    (comp.lang.java.programmer)
  • How to debug EXCEPTION_ACCESS_VIOLATION?
    ... appreciate any helpfule debugging tips! ... Please refer to release documentation for possible ... # Java VM: Java HotSpotClient VM ... # An error report file has been saved as hs_err_pid4308.log. ...
    (comp.lang.java.help)
  • Re: Cohens paper on byte order
    ... if the bits of an octet in ASN.1 are ordered, then you should be able to ... What could "significance" possibly refer to other than numeric significance? ... public key but refuse to specify why, it is because the private key has been ...
    (sci.crypt)