converting characters to octal

H

Hostos

how would I convert character objects to their octal representation
according to the ASCII table?


Please help
 
S

Steve W. Jackson

Gordon Beaton <[email protected]> said:
:On 9 Sep 2003 07:45:51 -0700, Hostos wrote:
:> how would I convert character objects to their octal representation
:> according to the ASCII table?
:
: Integer.toString(int i, int radix)
:
:/gordon

I won't contest whether this works, since I haven't tried to use it.
But the OP did say "character objects". If we assume that means
character (char) primitives, that should be:

Character.digit(char ch, int radix)

If indeed it did mean a Character object, then that object's charValue()
method will return the primitive value needed for that static method.

= Steve =
 
G

Gordon Beaton

I won't contest whether this works, since I haven't tried to use it.
But the OP did say "character objects". If we assume that means
character (char) primitives, that should be:

Character.digit(char ch, int radix)

Aside from the object/primitive distinction (I didn't notice that),
the method you suggest does something completely different than the
one I suggested. It does the following conversion:

'0' -> 0
'1' -> 1
...
'9' -> 9
'a' -> 10
'b' -> 11
...
'z' -> 35

i.e. the value represented by a character when used as a digit to
display numbers in a given radix.

My interpretation of his question was how to get the ascii value of
the character ('a' -> 97 etc) and convert that to octal (141). The
value of 'a' already is 97 (no conversion necessary), so only the
conversion to octal is needed. The Character class seems to be lacking
a toString() that lets you specify radix.

The original post does seem a little ambiguous though, I had to reread
it several times before posting this...

/gordon
 
S

Steve W. Jackson

Gordon Beaton <[email protected]> said:
:On Tue, 09 Sep 2003 10:12:08 -0500, Steve W. Jackson wrote:
:> I won't contest whether this works, since I haven't tried to use it.
:> But the OP did say "character objects". If we assume that means
:> character (char) primitives, that should be:
:>
:> Character.digit(char ch, int radix)
:
:Aside from the object/primitive distinction (I didn't notice that),
:the method you suggest does something completely different than the
:eek:ne I suggested. It does the following conversion:
:
: '0' -> 0
: '1' -> 1
: ...
: '9' -> 9
: 'a' -> 10
: 'b' -> 11
: ...
: 'z' -> 35
:
:i.e. the value represented by a character when used as a digit to
:display numbers in a given radix.
:
:My interpretation of his question was how to get the ascii value of
:the character ('a' -> 97 etc) and convert that to octal (141). The
:value of 'a' already is 97 (no conversion necessary), so only the
:conversion to octal is needed. The Character class seems to be lacking
:a toString() that lets you specify radix.
:
:The original post does seem a little ambiguous though, I had to reread
:it several times before posting this...
:
:/gordon

You're quite right. In fact, the digit method actually says that, so I
guess I didn't look far enough when replying.

Your method leads to the assumption that char values are indeed usable
as int values. I know the question pops up in these USENET forums often
about whether such things are possible, and I've been led to believe
that it's "not supposed to work that way" in Java. It seems that the C
roots are showing after all, I guess.

= Steve =
 
G

Gordon Beaton

Your method leads to the assumption that char values are indeed
usable as int values. I know the question pops up in these USENET
forums often about whether such things are possible, and I've been
led to believe that it's "not supposed to work that way" in Java. It
seems that the C roots are showing after all, I guess.

Well the JLS lists char among the integral types (sec 4.2) and the
usual promotion rules apply. One thing you need to watch out for
though is sign extension when using char as int, since char is
unsigned.

/gordon
 
L

La'ie Techie

Well the JLS lists char among the integral types (sec 4.2) and the usual
promotion rules apply. One thing you need to watch out for though is sign
extension when using char as int, since char is unsigned.

The current JVM white papers show char primitives as a 16-bit unsigned
integer. Therefore, when converting a char to an int, you will never get
a negative value.

Aloha,
La'ie Techie
 

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,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top