How to indicate byte-format in numeral constants

Discussion in 'Java' started by =?iso-8859-1?q?J=FCrgen_Gerstacker?=, Dec 18, 2006.

  1. 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
    =?iso-8859-1?q?J=FCrgen_Gerstacker?=, Dec 18, 2006
    #1
    1. Advertising

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

    Eric Sosman Guest

    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.

    --
    Eric Sosman, Dec 18, 2006
    #2
    1. Advertising

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

    Daniel Pitts Guest

    Jürgen Gerstacker wrote:
    > 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.
    Daniel Pitts, Dec 19, 2006
    #3
  4. Re: Howport to indicate byte-format in numeral constants

    Daniel Pitts wrote:

    > 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
    =?iso-8859-1?q?J=FCrgen_Gerstacker?=, Dec 19, 2006
    #4
  5. =?iso-8859-1?q?J=FCrgen_Gerstacker?=

    Daniel Pitts Guest

    Re: Howport to indicate byte-format in numeral constants

    Jürgen Gerstacker wrote:
    > Daniel Pitts wrote:
    >
    > > 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


    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.
    Daniel Pitts, Dec 19, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vlajko Knezic
    Replies:
    1
    Views:
    10,605
    Joe Smith
    Mar 6, 2005
  2. level9

    roman numeral to integer

    level9, Sep 12, 2003, in forum: C++
    Replies:
    9
    Views:
    938
    Stewart Gordon
    Sep 16, 2003
  3. Replies:
    8
    Views:
    463
    user923005
    Apr 9, 2007
  4. Phlip
    Replies:
    4
    Views:
    512
    angus
    Mar 2, 2004
  5. Brian Tickler

    Roman numeral conversion

    Brian Tickler, Sep 18, 2009, in forum: Ruby
    Replies:
    3
    Views:
    299
    trans
    Sep 20, 2009
Loading...

Share This Page