format() and hex characters

J

jimgardener

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
 
L

Lasse Reichstein Nielsen

jimgardener said:
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:
 
K

Kevin McMurtrie

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


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


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.
 
L

Lasse Reichstein Nielsen

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
 
S

Stefan Ram

Lasse Reichstein Nielsen said:
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.
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top