8-bit unsigned integers in Java

J

jeff

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
 
M

Mike Schilling

jeff said:
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.
 
M

Mark Thornton

jeff said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top