char to decimal

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

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?
 
K

Knute Johnson

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

Ian Shef

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

Dirk Bruere at NeoPax

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

Lew

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"!
 
R

Roedy Green

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

Lawrence D'Oliveiro

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

Lew

Lawrence said:
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.
 
L

Lawrence D'Oliveiro

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

Mayeul

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
 
A

Andreas Leitgeb

Patricia Shanahan said:
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)
 
D

Dirk Bruere at NeoPax

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
 
J

Joshua Cranmer

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

markspace

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

Paul Cager

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.

:)
 
L

Lew

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.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top