Question

I'm currently attempting to write a serial implementation of MODbus in java. The main problem I'm having is that when I declare a byte (or short for that matter) as something like 0xC4 (for a byte) I get a "Loss of precision error".

Is there someway around this? Or am I forced to treat all numeric types like their 1 bit shorter then they really are (ala 7bit, 15 bit, 31 bit, 63 bit)?

And example:

 byte[] test = 
    {
        0x11,
        0x02,
        0x00,
        0xC4,
        0x00,
        0x16
    };

This throws a warning on 0xC4 that "Possible loss of precision" required byte, found int.

Was it helpful?

Solution

When you say 0xC4, that is an integer literal that is bigger than the maximum value for a byte, 127, so you must explicitly cast it to a byte.

Try

byte[] test = 
{
    0x11,
    0x02,
    0x00,
    (byte) 0xC4,
    0x00,
    0x16
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top