Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > char to decimal

Reply
Thread Tools

char to decimal

 
 
Dirk Bruere at NeoPax
Guest
Posts: n/a
 
      05-05-2011
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
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      05-05-2011
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/
 
Reply With Quote
 
 
 
 
Ian Shef
Guest
Posts: n/a
 
      05-05-2011
Dirk Bruere at NeoPax <(E-Mail Removed)> 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.


 
Reply With Quote
 
Dirk Bruere at NeoPax
Guest
Posts: n/a
 
      05-05-2011
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-05-2011
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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-05-2011
On Thu, 05 May 2011 01:43:51 +0100, Dirk Bruere at NeoPax
<(E-Mail Removed)> 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.


 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      05-05-2011
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.
 
Reply With Quote
 
Nasser M. Abbasi
Guest
Posts: n/a
 
      05-05-2011
On 5/4/2011 10:03 PM, Roedy Green wrote:
> On Thu, 05 May 2011 01:43:51 +0100, Dirk Bruere at NeoPax
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-05-2011
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.
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      05-05-2011
Lew <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments