Pregunta

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.

¿Fue útil?

Solución

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
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top