converting characters to octal

Discussion in 'Java' started by Hostos, Sep 9, 2003.

  1. Hostos

    Hostos Guest

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


    Please help
    Hostos, Sep 9, 2003
    #1
    1. Advertising

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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Sep 9, 2003
    #2
    1. Advertising

  3. In article <3f5de9c5$>, Gordon Beaton <>
    wrote:

    >: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 =
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Sep 9, 2003
    #3
  4. 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
    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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Sep 9, 2003
    #4
  5. In article <3f5df877$>, Gordon Beaton <>
    wrote:

    >: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 =
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Sep 9, 2003
    #5
  6. Hostos

    Roedy Green Guest

    On 9 Sep 2003 07:45:51 -0700, (Hostos) wrote or
    quoted :

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


    If you rephrase your question to how to I convert ints to String using
    radix 8, the answer is waiting for you at
    http://mindprod.com/converter.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 9, 2003
    #6
  7. On Tue, 09 Sep 2003 12:48:18 -0500, Steve W. Jackson wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Sep 9, 2003
    #7
  8. Hostos

    La'ie Techie Guest

    On Tue, 09 Sep 2003 20:14:27 +0200, Gordon Beaton wrote:

    > 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
    La'ie Techie, Oct 15, 2003
    #8
    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. Kermit Lowry
    Replies:
    0
    Views:
    375
    Kermit Lowry
    Sep 25, 2003
  2. Replies:
    2
    Views:
    596
    Christos TZOTZIOY Georgiou
    Dec 24, 2004
  3. Replies:
    15
    Views:
    11,948
    Eric Sosman
    Jun 23, 2006
  4. Michael Goerz

    converting to and from octal escaped UTF--8

    Michael Goerz, Dec 3, 2007, in forum: Python
    Replies:
    9
    Views:
    1,881
    MonkeeSage
    Dec 4, 2007
  5. Worky Workerson
    Replies:
    11
    Views:
    334
    Ben Morrow
    Jul 29, 2006
Loading...

Share This Page