How to indicate byte-format in numeral constants

  • Thread starter =?iso-8859-1?q?J=FCrgen_Gerstacker?=
  • Start date
?

=?iso-8859-1?q?J=FCrgen_Gerstacker?=

Hello,
very frequently I have to use Byte-constants, numbers in the range 0x80
- 0x7f. If I don't cast such a constant with '(byte)' I get an error
like:

write_4bytes(byte,byte) in ser.yyy_ser cannot be applied to (int,byte)
cls.write_4bytes(0xf0,(byte)0x4d); //sw

C has several suffixes like 0b, 10u, 7l to indicate byte-constants, or
unsigned constants, or long constants, .... Is such possible also with
java, or is casting with '(byte)' obligatory?

Juergen
 
E

Eric Sosman

Jürgen Gerstacker wrote On 12/18/06 04:53,:
Hello,
very frequently I have to use Byte-constants, numbers in the range 0x80
- 0x7f. If I don't cast such a constant with '(byte)' I get an error
like:

write_4bytes(byte,byte) in ser.yyy_ser cannot be applied to (int,byte)
cls.write_4bytes(0xf0,(byte)0x4d); //sw

C has several suffixes like 0b, 10u, 7l to indicate byte-constants, or
unsigned constants, or long constants, .... Is such possible also with
java, or is casting with '(byte)' obligatory?

C has 'l' to denote a long constant, and so does Java.

C has 'u' to denote an unsigned constant, but since Java's
only unsigned integer type is 'char' Java doesn't use 'u'.

C does not use a 'b' suffix, and neither does Java.

You could solve your problem by writing casts in each
call. A possibly more convenient approach might be to change
the write_4bytes method to take two int arguments instead of
two bytes, with the (documented) understanding that only the
low-order byte of each int is written. See, for example, the
write(int) method of java.io_OutputStream for precedent.
 
D

Daniel Pitts

Jürgen Gerstacker said:
Hello,
very frequently I have to use Byte-constants, numbers in the range 0x80
- 0x7f. If I don't cast such a constant with '(byte)' I get an error
like:

write_4bytes(byte,byte) in ser.yyy_ser cannot be applied to (int,byte)
cls.write_4bytes(0xf0,(byte)0x4d); //sw

C has several suffixes like 0b, 10u, 7l to indicate byte-constants, or
unsigned constants, or long constants, .... Is such possible also with
java, or is casting with '(byte)' obligatory?

Juergen
You could pass in integers, and have the casting be done in the
write_4bytes() method.

Also, tt is better to use constants with meaningful names, instead of
magic values.
for example: Assuming that 0xF0 is something meaningful, such as the
beginning of the chicken section?
private static final byte START_OF_CHICKEN_SECTION = -16; // F0
private static final byte CHICKENS_GO_MOO = 77; // 4d
....
cls.write_4bytes(START_OF_CHICKEN_SECTION, CHICKENS_GO_MOO);

Also, I think write_4bytes is miss named if it only takes two bytes :)

Anyway, hope this helps.
GL.
 
?

=?iso-8859-1?q?J=FCrgen_Gerstacker?=

Daniel said:
Also, tt is better to use constants with meaningful names, instead of
magic values.
for example: Assuming that 0xF0 is something meaningful, such as the
beginning of the chicken section?
private static final byte START_OF_CHICKEN_SECTION = -16; // F0
private static final byte CHICKENS_GO_MOO = 77; // 4d
...
cls.write_4bytes(START_OF_CHICKEN_SECTION, CHICKENS_GO_MOO);

But those values must be known in other modules (embedded system
attached to the serial port), written in C.
Also, I think write_4bytes is miss named if it only takes two bytes :)

No, acually 4 bytes are written, a start byte (not specified in the
function call), two given data bytes and a checksum byte (not given,
but calculated)
Anyway, hope this helps.
GL

I knew the trick of int-arguments but hoped there would be another
solution. Now I know there isn't.
Thanks
 
D

Daniel Pitts

Jürgen Gerstacker said:
But those values must be known in other modules (embedded system
attached to the serial port), written in C.


No, acually 4 bytes are written, a start byte (not specified in the
function call), two given data bytes and a checksum byte (not given,
but calculated)


I knew the trick of int-arguments but hoped there would be another
solution. Now I know there isn't.
Thanks

I still think the name method is misnamed. Sure it writes four bytes,
but it might be better to describe what that means to someone who
doesn't necessarilly know the ins and outs of the protocol.

sendCommand, maybe.

Also, even if your values have to be known in a different module all
together, it is still good to have named constants in the Java code.
You can have similar #define directives in the C code to clarify THAT
code. There is often very few reasons to have unlabeled numbers just
sitting around in code.

In any case, depending on the use of your write_4bytes method, you
might be better to have a single integer as the parameter. If the byte
values are always paired up (hard coded), then instead of write4b(0xF0,
0x4D), you would write4b(0xF04D) (or 0x4DF0, depending on how you
endianed it)

Oh, and if you happen to have methods like write_5bytes and
write_3bytes, it might be even better to take an array of bytes and
just call it write instead. Assuming you're java isn't embeded (I don't
know the implications if it is embeded), you're overhead for using
arrays should be small.
If you're using java 1.5, you could even use the varargs approach.

public void write(byte...bytes) {
writeFirstByte();
writeAllBytes(bytes);
writeChecksum(bytes);
}

Ohwell, I'm doing a lot of speculating for not knowing your domain. :)
But I hope I've given you some ideas that might make your design better
in the end.
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top