format() and hex characters

Discussion in 'Java' started by jimgardener, Jun 22, 2008.

  1. jimgardener

    jimgardener Guest

    hi
    i have an array of chars like 0x81,0x7e,0xff etc,
    when i try to display such characters using
    format()

    char c=0x81;
    System.out.print("%x",c) it gives
    java.util.IllegalFormatConversionException: x != java.lang.Character
    but
    System.out.print("%x",0x81) and
    int i=0x81;
    System.out.print("%x",i) works
    why is this ?can someone explain?
    jim
     
    jimgardener, Jun 22, 2008
    #1
    1. Advertising

  2. jimgardener <> writes:

    > i have an array of chars like 0x81,0x7e,0xff etc,
    > when i try to display such characters using
    > format()
    >
    > char c=0x81;
    > System.out.print("%x",c) it gives


    I take it you meant to call format or printf, not print.
    Please make sure you post the actual code that fails!

    > java.util.IllegalFormatConversionException: x != java.lang.Character


    So the "x" conversion does not accept a Character argument.

    > but
    > System.out.print("%x",0x81) and
    > int i=0x81;
    > System.out.print("%x",i) works


    But it does accept an Integer argument.

    From the javadoc, "x" requires an integral argument:
    ---
    'x', 'X' integral The result is formatted as a hexadecimal integer
    ---
    And integral means:
    ---
    Integral - may be applied to Java integral types: byte, Byte, short,
    Short, int and Integer, long, Long, and BigInteger
    ---
    So Character is not considered integral. Cast it to int and you should
    be safe.

    /L
    --
    Lasse Reichstein Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 22, 2008
    #2
    1. Advertising

  3. In article <>,
    Lasse Reichstein Nielsen <> wrote:

    > jimgardener <> writes:
    >
    > > i have an array of chars like 0x81,0x7e,0xff etc,
    > > when i try to display such characters using
    > > format()
    > >
    > > char c=0x81;
    > > System.out.print("%x",c) it gives

    >
    > I take it you meant to call format or printf, not print.
    > Please make sure you post the actual code that fails!
    >
    > > java.util.IllegalFormatConversionException: x != java.lang.Character

    >
    > So the "x" conversion does not accept a Character argument.
    >
    > > but
    > > System.out.print("%x",0x81) and
    > > int i=0x81;
    > > System.out.print("%x",i) works

    >
    > But it does accept an Integer argument.
    >
    > From the javadoc, "x" requires an integral argument:
    > ---
    > 'x', 'X' integral The result is formatted as a hexadecimal integer
    > ---
    > And integral means:
    > ---
    > Integral - may be applied to Java integral types: byte, Byte, short,
    > Short, int and Integer, long, Long, and BigInteger
    > ---
    > So Character is not considered integral. Cast it to int and you should
    > be safe.
    >
    > /L


    That's annoying, because those designing Java's specs claim that 'char'
    can be used for unsigned 16 bit integer math.

    --
    I will not see your reply if you use Google.
     
    Kevin McMurtrie, Jun 22, 2008
    #3
  4. Kevin McMurtrie <> writes:

    > In article <>,
    > Lasse Reichstein Nielsen <> wrote:

    [wrt format method]
    >> So Character is not considered integral. Cast it to int and you should
    >> be safe.


    > That's annoying, because those designing Java's specs claim that 'char'
    > can be used for unsigned 16 bit integer math.


    I guess it's a matter of intent. Characters are represented by their
    code point, which is a number, and you can do arithemtic on it to
    produce the code point of another character (e.g. 'a'+1 == 'b'), but a
    character is not itself a number. The intended representation of the
    char with value 65 is a capital A, not the numeral consisting of the
    digits 6 and 5.

    This is how Java has treated characters in other cases as well.
    "foo" + 'a' => "fooa"
    "foo" + (int)a => "foo97"
    and the same for StringBuilder#append(char).

    /L
    --
    Lasse Reichstein Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 22, 2008
    #4
  5. jimgardener

    Stefan Ram Guest

    Lasse Reichstein Nielsen <> writes:
    >I guess it's a matter of intent. Characters are represented by their
    >code point, which is a number, and you can do arithemtic on it to
    >produce the code point of another character (e.g. 'a'+1 == 'b'), but a


    Unicode code points have 21 bits.

    In Java, no type fits exactly, but usually either an int
    variable or, preferably, a java.lang.String object with a
    single code point is used to represent a code point.
     
    Stefan Ram, Jun 22, 2008
    #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. Replies:
    10
    Views:
    6,229
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    474
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    590
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,892
  5. tim
    Replies:
    2
    Views:
    1,574
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page