char to decimal

Discussion in 'Java' started by Dirk Bruere at NeoPax, May 5, 2011.

  1. IU need to take a char in UTF-8 and convert it to an ascii int (decimal)

    eg "a" = 97

    which is then converted to the string "97"
    Is there a simple way to do this?

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
     
    Dirk Bruere at NeoPax, May 5, 2011
    #1
    1. Advertising

  2. On 5/4/2011 5:43 PM, Dirk Bruere at NeoPax wrote:
    > IU need to take a char in UTF-8 and convert it to an ascii int (decimal)
    >
    > eg "a" = 97
    >
    > which is then converted to the string "97"
    > Is there a simple way to do this?
    >


    You can cast it to a short.

    --

    Knute Johnson
    s/knute/nospam/
     
    Knute Johnson, May 5, 2011
    #2
    1. Advertising

  3. Dirk Bruere at NeoPax

    Ian Shef Guest

    Dirk Bruere at NeoPax <> wrote in news:92ea64F3avU1
    @mid.individual.net:

    > IU need to take a char in UTF-8 and convert it to an ascii int (decimal)
    >
    > eg "a" = 97
    >
    > which is then converted to the string "97"
    > Is there a simple way to do this?
    >


    Yes. The details depend upon what you really meant. Here are two ways:


    public class CharToDecimal {
    public static void main(String[] args) {
    char c = 'a' ;
    System.out.println((int)c) ; // Directly output the string.
    String s = Integer.toString(c) ; // Create a String without output.
    System.out.println(s); // Output the String for verification.
    }
    }

    A char is much like an int except that:

    It has 16 bits instead of 32.
    It is unsigned, with a value from 0 through 65535.
    It gets special handling in some places, such as by System.out.println. The
    special handling by System.out.println can be avoided by casting to an int.
     
    Ian Shef, May 5, 2011
    #3
  4. On 05/05/2011 01:43, Dirk Bruere at NeoPax wrote:
    > IU need to take a char in UTF-8 and convert it to an ascii int (decimal)
    >
    > eg "a" = 97
    >
    > which is then converted to the string "97"
    > Is there a simple way to do this?
    >


    Thanks.
    Never knew it was so easy!

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
     
    Dirk Bruere at NeoPax, May 5, 2011
    #4
  5. Dirk Bruere at NeoPax

    Lew Guest

    On 05/04/2011 08:43 PM, Dirk Bruere at NeoPax wrote:
    > IU need to take a char in UTF-8 and convert it to an ascii [sic] int (decimal)
    >
    > eg "a" = 97
    >
    > which is then converted to the string "97"
    > Is there a simple way to do this?


    First of all, a 'char' constant in Java is indicated with single quotes, not
    double quotes.

    char letter = 'a';

    Second of all, 'char' in Java is a numeric type, so conversion to a numeric
    value is not needed.

    Third of all, are you absolutely certain you want to convert the 16-bit value
    of a 'char' to a 7-bit ASCII value? What about code points that require 32
    bits to express? How would you convert those to a 7-bit encoding?

    Finally, are you asking about direct numeric conversions, i.e., to output
    simply the numeric 16-bit value of the 'char', or are you asking about
    converting between different text encodings, e.g., between UTF-8 and ASCII?
    The answer to this question is key, because if you are talking about encodings
    then the answer is anything but "that easy"!

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, May 5, 2011
    #5
  6. Dirk Bruere at NeoPax

    Roedy Green Guest

    On Thu, 05 May 2011 01:43:51 +0100, Dirk Bruere at NeoPax
    <> wrote, quoted or indirectly quoted someone who
    said :

    >IU need to take a char in UTF-8 and convert it to an ascii int (decimal)
    >
    >eg "a" = 97
    >
    >which is then converted to the string "97"
    >Is there a simple way to do this?


    see http://mindprod.com/applet/converter.html
    to generate code for most any conversion need.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Politicians complain that Kindles and iBooks are killing jobs by
    destroying the paper book industry. I see it that they have create a way
    to produce books for less than a third the cost without destroying forests
    and emitting greenhouse gases in the process. They have created wealth.
    They are encouraging literacy and cutting the costs of education.
     
    Roedy Green, May 5, 2011
    #6
  7. In message <Xns9EDBB8224D5B6vaj4088ianshef@138.125.254.103>, Ian Shef wrote:

    > A char is much like an int except that:
    >
    > It has 16 bits instead of 32.
    > It is unsigned, with a value from 0 through 65535.
    > It gets special handling in some places, such as by System.out.println.
    > The special handling by System.out.println can be avoided by casting to an
    > int.


    Funny, they could do all this for char, but not for boolean.
     
    Lawrence D'Oliveiro, May 5, 2011
    #7
  8. On 5/4/2011 10:03 PM, Roedy Green wrote:
    > On Thu, 05 May 2011 01:43:51 +0100, Dirk Bruere at NeoPax
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    >> IU need to take a char in UTF-8 and convert it to an ascii int (decimal)
    >>
    >> eg "a" = 97
    >>
    >> which is then converted to the string "97"
    >> Is there a simple way to do this?

    >
    > see http://mindprod.com/applet/converter.html
    > to generate code for most any conversion need.



    That is a really nice and useful applet. Thanks for sharing it.

    --Nasser
     
    Nasser M. Abbasi, May 5, 2011
    #8
  9. Dirk Bruere at NeoPax

    Lew Guest

    Lawrence D'Oliveiro wrote:
    > Ian Shef wrote:
    >
    >> A char is much like an int except that:
    >>
    >> It has 16 bits instead of 32.
    >> It is unsigned, with a value from 0 through 65535.
    >> It gets special handling in some places, such as by System.out.println.
    >> The special handling by System.out.println can be avoided by casting to an
    >> int.

    >
    > Funny, they could do all this for char, but not for boolean.


    Booleans are not numbers. There's nothing funny about that choice at all. As
    you well know, having been down this road before.

    --
    Lew
    Awaiting the inevitable now.
     
    Lew, May 5, 2011
    #9
  10. Lew <> wrote:
    > Lawrence D'Oliveiro wrote:
    >> Ian Shef wrote:
    >>> A char is much like an int except that:
    >>> It has 16 bits instead of 32.
    >>> It is unsigned, with a value from 0 through 65535.
    >>> It gets special handling in some places, such as by System.out.println.
    >>> The special handling by System.out.println can be avoided by casting to an
    >>> int.

    >> Funny, they could do all this for char, but not for boolean.

    > Booleans are not numbers.


    But characters are. So, 'a' is just as naturally 97 as true isn't 1
     
    Andreas Leitgeb, May 5, 2011
    #10
  11. In message <>, Patricia
    Shanahan wrote:

    > There is a strong natural association between a character and the numeric
    > value of its Unicode representation. There are several different ways of
    > mapping between the two Boolean values and integers ...


    Only one “strong natural associationâ€, though.
     
    Lawrence D'Oliveiro, May 5, 2011
    #11
  12. Dirk Bruere at NeoPax

    Mayeul Guest

    On 05/05/2011 15:59, Lawrence D'Oliveiro wrote:
    > In message<>, Patricia
    > Shanahan wrote:
    >
    >> There is a strong natural association between a character and the numeric
    >> value of its Unicode representation. There are several different ways of
    >> mapping between the two Boolean values and integers ...

    >
    > Only one “strong natural associationâ€, though.


    Says who?

    #1
    true => 0
    false => 1

    #2
    true => 1
    false => 0

    #3
    true => 1
    false => 2

    #4
    true => 2
    false => 1

    #5
    true => 1
    false => -1

    #6
    true => -1
    false => 1

    --
    Mayeul
     
    Mayeul, May 5, 2011
    #12
  13. Patricia Shanahan <> wrote:
    > On 5/5/2011 4:18 AM, Andreas Leitgeb wrote:
    >> Lew<> wrote:
    >>> Lawrence D'Oliveiro wrote:
    >>>> Ian Shef wrote:
    >>>>> A char is much like an int except that:
    >>>>> It has 16 bits instead of 32.
    >>>>> It is unsigned, with a value from 0 through 65535.
    >>>>> It gets special handling in some places, such as by System.out.println.
    >>>>> The special handling by System.out.println can be avoided by casting to an
    >>>>> int.
    >>>> Funny, they could do all this for char, but not for boolean.
    >>> Booleans are not numbers.

    >> But characters are. So, 'a' is just as naturally 97 as true isn't 1

    > I would phrase it slightly differently. There is a strong natural
    > association between a character and the numeric value of its Unicode
    > representation.


    Well, just as "natural", as Unicode itself is.
    I've yet to see Unicode appear in nature, though. ;-)

    > There are several different ways of mapping between the
    > two Boolean values and integers,


    One of which Java happens to have chosen already,
    just for "internal use only."

    The mapping from numbers to booleans is, of course, not injective.
    The mapping of numbers (even 16 bit) to characters according to the
    Unicode-table isn't even *defined* for certain numbers.
    (e.g. 0xfffe, iirc)
     
    Andreas Leitgeb, May 5, 2011
    #13
  14. On 05/05/2011 02:59, Lew wrote:
    > On 05/04/2011 08:43 PM, Dirk Bruere at NeoPax wrote:
    >> IU need to take a char in UTF-8 and convert it to an ascii [sic] int
    >> (decimal)
    >>
    >> eg "a" = 97
    >>
    >> which is then converted to the string "97"
    >> Is there a simple way to do this?

    >
    > First of all, a 'char' constant in Java is indicated with single quotes,
    > not double quotes.
    >
    > char letter = 'a';
    >
    > Second of all, 'char' in Java is a numeric type, so conversion to a
    > numeric value is not needed.
    >
    > Third of all, are you absolutely certain you want to convert the 16-bit
    > value of a 'char' to a 7-bit ASCII value? What about code points that
    > require 32 bits to express? How would you convert those to a 7-bit
    > encoding?
    >
    > Finally, are you asking about direct numeric conversions, i.e., to
    > output simply the numeric 16-bit value of the 'char', or are you asking
    > about converting between different text encodings, e.g., between UTF-8
    > and ASCII? The answer to this question is key, because if you are
    > talking about encodings then the answer is anything but "that easy"!
    >

    I need it to match the packet i/f specs designed by somemone else that
    requires text characters be sent as decimal ascii

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
     
    Dirk Bruere at NeoPax, May 5, 2011
    #14
  15. On 05/05/2011 09:59 AM, Lawrence D'Oliveiro wrote:
    > In message<>, Patricia
    > Shanahan wrote:
    >
    >> There is a strong natural association between a character and the numeric
    >> value of its Unicode representation. There are several different ways of
    >> mapping between the two Boolean values and integers ...

    >
    > Only one “strong natural associationâ€, though.


    0,1; 1, -1; and 0, -1 all have arguments for their use. If you dislike
    the middle one, think about signs of non-zero numbers.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, May 5, 2011
    #15
  16. Dirk Bruere at NeoPax

    markspace Guest

    On 5/5/2011 8:14 AM, Dirk Bruere at NeoPax wrote:

    > I need it to match the packet i/f specs designed by somemone else that
    > requires text characters be sent as decimal ascii



    That's a really odd requirement. Your spec might mean just regular
    text. Not ascii text, as in the character 9 followed by the character
    7, but just 'a' as a literal 97 byte value.

    Just saying.
     
    markspace, May 5, 2011
    #16
  17. Dirk Bruere at NeoPax

    Paul Cager Guest

    On May 5, 3:56 pm, Andreas Leitgeb <>
    wrote:
    > Patricia Shanahan <> wrote:

    ....
    > > There are several different ways of mapping between the
    > > two Boolean values and integers,

    >
    > One of which Java happens to have chosen already,
    > just for "internal use only."


    Two values are defined for "public use" - 1231 and 1237
    (java.lang.Boolean's hashcode()). I think they should take precedence
    over any "internal use only" values.

    :)
     
    Paul Cager, May 5, 2011
    #17
  18. Dirk Bruere at NeoPax

    Lew Guest

    On 05/05/2011 07:18 AM, Andreas Leitgeb wrote:
    > Lew<> wrote:
    >> Lawrence D'Oliveiro wrote:
    >>> Ian Shef wrote:
    >>>> A char is much like an int except that:
    >>>> It has 16 bits instead of 32.
    >>>> It is unsigned, with a value from 0 through 65535.
    >>>> It gets special handling in some places, such as by System.out.println.
    >>>> The special handling by System.out.println can be avoided by casting to an
    >>>> int.
    >>> Funny, they could do all this for char, but not for boolean.

    >> Booleans are not numbers.

    >
    > But characters are. So, 'a' is just as naturally 97 as true isn't 1


    I am not defending that decision, but the one not to make booleans numeric.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, May 5, 2011
    #18
  19. Dirk Bruere at NeoPax

    Lew Guest

    On 05/05/2011 09:59 AM, Lawrence D'Oliveiro wrote:
    > In message<>, Patricia
    > Shanahan wrote:
    >
    >> There is a strong natural association between a character and the numeric
    >> value of its Unicode representation. There are several different ways of
    >> mapping between the two Boolean values and integers ...

    >
    > Only one “strong natural associationâ€, though.


    Which you, naturally, keep to yourself?

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, May 5, 2011
    #19
  20. Dirk Bruere at NeoPax

    Lew Guest

    On 05/05/2011 02:48 PM, Paul Cager wrote:
    > On May 5, 3:56 pm, Andreas Leitgeb<>
    > wrote:
    >> Patricia Shanahan<> wrote:

    > ...
    >>> There are several different ways of mapping between the
    >>> two Boolean values and integers,

    >>
    >> One of which Java happens to have chosen already,
    >> just for "internal use only."

    >
    > Two values are defined for "public use" - 1231 and 1237
    > (java.lang.Boolean's hashcode()). I think they should take precedence
    > over any "internal use only" values.
    >
    > :)


    Those are, of course, the natural numeric representations for booleans in
    Java, whatever Larry-baby thinks.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, May 5, 2011
    #20
    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. wwj
    Replies:
    7
    Views:
    556
  2. wwj
    Replies:
    24
    Views:
    2,518
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    479
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    587
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,056
    Ian Collins
    May 9, 2006
Loading...

Share This Page