Can we make unsigned byte in Java

前端 未结 16 1265
青春惊慌失措
青春惊慌失措 2020-11-22 08:02

I am trying to convert a signed byte in unsigned. The problem is the data I am receiving is unsigned and Java does not support unsigned byte, so when it reads the data it tr

16条回答
  •  余生分开走
    2020-11-22 08:32

    Adamski provided the best answer, but it is not quite complete, so read his reply, as it explains the details I'm not.

    If you have a system function that requires an unsigned byte to be passed to it, you can pass a signed byte as it will automatically treat it as an unsigned byte.

    So if a system function requires four bytes, for example, 192 168 0 1 as unsigned bytes you can pass -64 -88 0 1, and the function will still work, because the act of passing them to the function will un-sign them.

    However you are unlikely to have this problem as system functions are hidden behind classes for cross-platform compatibility, though some of the java.io read methods return unsighed bytes as an int.

    If you want to see this working, try writing signed bytes to a file and read them back as unsigned bytes.

提交回复
热议问题