• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make all bytes of array in positive values in decimal  RSS feed

 
johnsoan smith
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to make possible to change the negative values -1 to -127 to the values of 128 to 255 . I use the following code but still got negative values



Any suggestion please?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The range for bytes is -128 to 127 (inclusive), so the cannot become 128 to 255. You can convert to int; x & 0xFF turns negative bytes into positive ints between 0 and 255 (inclusive). As soon as you cast that to byte values between 128 and 255 become negative again.
 
johnsoan smith
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob Spoor what if i use these negative values to the GF(256) would it be a problem ?



i have this method add to GF(256) and i would like to use this add method to this method



where byte [] a is the one i mentioned to my first post which will have also negative values

Please give me a suggestion for that
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a complicated solution. If I see anything that complicated I assume it is wrong on spec. Even if it isn't.

What is wrong with the promotion operator? Apart from the fact that it operates by sign extension.So that will print 0 1 2 … 126 127 -128 -127 … -2 -1
No better. But it is. You are now dealing with ints not bytes. You can now use the bitwise AND operator to apply a mask to convert everything to a non‑negative value. At which point you will probably find the promotion operator is redundant and can be removed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!