8-bit unsigned integers in Java

Discussion in 'Java' started by jeff, Mar 7, 2004.

  1. jeff

    jeff Guest

    hi, i'm porting a toy crypto algorithm to java. the original cipher was
    written in C and used unsigned char's for 8-bit bytes with the range 0..255.
    i need a similar datatype in java, but the primitive 'byte' is signed
    (range -128..+127). does anyone have any suggestions for a 8-bit
    unsigned datatype in java?

    thanks in advance
    jeff, Mar 7, 2004
    #1
    1. Advertising

  2. "jeff" <> wrote in message
    news:c2fj6j$1sq6bd$-berlin.de...
    > hi, i'm porting a toy crypto algorithm to java. the original cipher was
    > written in C and used unsigned char's for 8-bit bytes with the range

    0..255.
    > i need a similar datatype in java, but the primitive 'byte' is signed
    > (range -128..+127). does anyone have any suggestions for a 8-bit
    > unsigned datatype in java?


    Use shorts and mask to 8 bits where necessary.
    Mike Schilling, Mar 7, 2004
    #2
    1. Advertising

  3. jeff wrote:
    > hi, i'm porting a toy crypto algorithm to java. the original cipher was
    > written in C and used unsigned char's for 8-bit bytes with the range
    > 0..255.
    > i need a similar datatype in java, but the primitive 'byte' is signed
    > (range -128..+127). does anyone have any suggestions for a 8-bit
    > unsigned datatype in java?
    >
    > thanks in advance
    >


    There are several choices. One is to use byte as it is (signed) but not
    that many operations produce the same result (bit pattern) for both
    signed and unsigned values it is merely a matter of interpretation. For
    those operations where the sign actually matters you will have to rework
    the equations.
    Another possibility is to use the char type for the calculations. This
    is unsigned albeit with 16 bits instead of 8 ... just ignore the content
    of the upper 8 bits.

    Mark Thornton
    Mark Thornton, Mar 7, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    36
    Views:
    3,412
    Brannon
    Jul 15, 2006
  2. seung

    using 64-bit integers on 32-bit machine

    seung, Sep 28, 2007, in forum: C Programming
    Replies:
    5
    Views:
    1,486
    David Thompson
    Oct 15, 2007
  3. Replies:
    3
    Views:
    400
    James Kanze
    Nov 19, 2008
  4. pozz
    Replies:
    12
    Views:
    716
    Tim Rentsch
    Mar 20, 2011
  5. James Harris
    Replies:
    37
    Views:
    526
    Tim Rentsch
    Aug 8, 2013
Loading...

Share This Page