String problems

V

V S Rawat

I tried to convert a "single character" string to char type,
it tried converting through int, long, float but it kept on
saying "incompatible type" and/or then "inconvertible type".

why is it so, when the tutorial itself recommends the above
method?
--------------
I have eventually managed it through str1/charAt(0) and it
is giving the thing correctly, but I am worried if there are
some loophole in this method.

It is for sure that all my strings have single character
value in them.
-------------

how to get unicode/ascii code of a character assigned in a
string.

the output can be in hex or decimal.

-------------
Are there any builtin functions to convert between hex and
decimal?

-----------
how to display a non-ascii value from within java?
System.out dumps that to ms dos window, but if the value is
non-lower-ascii, then dos displays a questionmark "?" for
each such char and we are none the wiser.

say i have
String Str1 = "\u2554";

I want to see what character is represented by this unicode
code.

which is the simple and quickest method of doing that just
like System.out.print()?

of course I can go on using swing button, but I am not that
familiar with it yet to codify it on the fly.

is there a simpler method?

-Rawat
 
A

ak

I tried to convert a "single character" string to char type,
it tried converting through int, long, float but it kept on
saying "incompatible type" and/or then "inconvertible type".
String#toCharArray()
how to get unicode/ascii code of a character assigned in a
string.

Character#getNumericValue(char ch)
 
R

Roedy Green

------------
I have eventually managed it through str1/charAt(0) and it
is giving the thing correctly, but I am worried if there are
some loophole in this method.

It is for sure that all my strings have single character
value in them.
-------------

see http://mindprod.com/converter.html

charAt is correct, but if you are unsure the strings don't have at
least one char, you must test yourself first and take appropriate
action or field the StringIndexOutOfBoundsException exception.

Perhaps you need a test like this:

boolean valid = ( s != null ?? s.length() == 1 );
 
M

Michael Borgwardt

ak said:
Character#getNumericValue(char ch)

No! That will try to interpret the character as a *digit* in any of a number of ways
you're hardly expecting.

To just get the ASCII / Unicode value, you don't have to do *anything at all* - char
is a numerical type that can be used in arithmetics and cast to int.
 
V

V S Rawat

Michael said:
No! That will try to interpret the character as a *digit*
in any of a number of ways you're hardly expecting.

To just get the ASCII / Unicode value, you don't have to
do *anything at all* - char is a numerical type that can
be used in arithmetics and cast to int.

I had asked about finding the code of something in a
"String" variable.

-Rawat
 
M

Michael Borgwardt

V said:
I had asked about finding the code of something in a
"String" variable.

Well, you'de already discovered the charAt() method by yourself,
and ak's suggestion would also require you to use that first.
 
C

Chris Smith

V said:
I have eventually managed it through str1/charAt(0) and it
is giving the thing correctly, but I am worried if there are
some loophole in this method.

Others have already mentioned that charAt(0) is the right way to do it.
If possible, though, you would be better off avoiding the String
representation to begin with, so long as you know it's only one
character.

You never said what other means you tried to convert from one to the
other. It's possible you might find something else that works, but
charAt is the simplest way. Casting won't work; in Java, when reference
types are involved, a cast does *not* convert from one thing to another.
It merely retypes a reference. You can never cast between primitive and
instance types.
how to get unicode/ascii code of a character assigned in a
string.

Listen to Michael on this one. Once you've got the character, it *is*
the unicode value. That's because 'char' is a numeric type that holds
unicode values. (A little advanced note; that's assuming all your
characters are within the basic multilingual plane. If you have
characters with an assigned value beyond 65535, such as some special
math symbols, then this becomes more complex; you can't store such a
character in a single Java 'char' variable.)
the output can be in hex or decimal.

The means above gives you a number. It doesn't have a base; it's just a
number. (Internally, of course, it's almost certainly stored in binary,
but you don't see that.) If you want the result in text format using a
specific base, then Integer.toString has an overloaded version that
allows you to specify the radix (meaning, the base).

Not directly, no. If you have a String that contains a hexadecimal
number, then use the correct overload of Integer.parseInt to parse it,
then use Integer.toString to convert back to a decimal String, as
follows:

String hexNumber = "A45E";
int value = Integer.parseInt(hexNumber, 16);
String decNumber = Integer.toString(value, 10);
how to display a non-ascii value from within java?
System.out dumps that to ms dos window, but if the value is
non-lower-ascii, then dos displays a questionmark "?" for
each such char and we are none the wiser.

This is, unfortunately, not generally possible. System.out is set up to
use a specific platform encoding, which is very rarely capable of
displaying arbitrary unicode characters. On Windows (since you
mentioned a DOS prompt), that encoding differs depending on the native
locale; in English versions, it's CP1252, which is a slight
bastardization of ISO-8859-1, which some control character replaced by
higher Unicode values. You won't be able to display just any Unicode
character there, and characters that can't be displayed will be
converted into a '?' character.

You can display arbitrary unicode characters in a GUI of some kind, but
make sure you have a font that maps a glyph for the character
(otherwise, you'll get a little box instead). You can also transmit
unicode characters to a different system via a network connection to be
displayed, assuming you use a capable encoding for the transfer. Same
for saving them to a file, etc.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Mark Thornton

Dale said:
Hello, Chris Smith !
You wrote:



set up to



CP1252 is the platform default coding on Windoze, but there is an
additional complication that by default the command window itself
uses a different character encoding (CP-450 or CP-837 IIRC).

850 (international) or 437 (US). At least its 850 here (UK).

Mark Thornton
 
R

Roedy Green

CP1252 is the platform default coding on Windoze, but there is an
additional complication that by default the command window itself
uses a different character encoding (CP-450 or CP-837 IIRC).
This is the DOS character encoding that includes all the line
drawing characters. So even ordinary accented characters from
ISO8859-1 do not display correctly.

DOS box uses Cp437, the original IBM OEM character set.
 
D

Dale King

Hello, Chris Smith !
You said:
This is, unfortunately, not generally possible. System.out is set up to
use a specific platform encoding, which is very rarely capable of
displaying arbitrary unicode characters. On Windows (since you
mentioned a DOS prompt), that encoding differs depending on the native
locale; in English versions, it's CP1252

CP1252 is the platform default coding on Windoze, but there is an
additional complication that by default the command window itself
uses a different character encoding (CP-450 or CP-837 IIRC).
This is the DOS character encoding that includes all the line
drawing characters. So even ordinary accented characters from
ISO8859-1 do not display correctly.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top