Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

How to indicate byte-format in numeral constants

Thread view: 
Jürgen Gerstacker - 18 Dec 2006 09:53 GMT
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
Eric Sosman - 18 Dec 2006 22:54 GMT
Jürgen Gerstacker wrote On 12/18/06 04:53,:
> Hello,
> very frequently I have to use Byte-constants, numbers in the range 0x80
[quoted text clipped - 7 lines]
> 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.

Signature

Eric.Sosman@sun.com

Daniel Pitts - 19 Dec 2006 00:18 GMT
J?rgen Gerstacker wrote:
> Hello,
> very frequently I have to use Byte-constants, numbers in the range 0x80
[quoted text clipped - 9 lines]
>
> 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.
Jürgen Gerstacker - 19 Dec 2006 17:33 GMT
> Also, tt is better to use constants with meaningful names, instead of
> magic values.
[quoted text clipped - 4 lines]
> ...
> 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
Daniel Pitts - 19 Dec 2006 18:43 GMT
J?rgen Gerstacker wrote:

> > Also, tt is better to use constants with meaningful names, instead of
> > magic values.
[quoted text clipped - 20 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.