String problems

Discussion in 'Java' started by V S Rawat, Jun 15, 2004.

  1. V S Rawat

    V S Rawat Guest

    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
    V S Rawat, Jun 15, 2004
    #1
    1. Advertising

  2. V S Rawat

    ak Guest

    > 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)

    --
    http://uio.dev.java.net Unified I/O for Java
    http://reader.imagero.com Java image reader
    ak, Jun 15, 2004
    #2
    1. Advertising

  3. V S Rawat

    Roedy Green Guest

    On Tue, 15 Jun 2004 22:50:48 +0530, V S Rawat <>
    wrote or quoted :

    >------------
    >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 );
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 15, 2004
    #3
  4. ak wrote:

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

    >
    >
    > 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.
    Michael Borgwardt, Jun 16, 2004
    #4
  5. V S Rawat

    V S Rawat Guest

    Michael Borgwardt wrote:
    > ak wrote:
    >
    >>> how to get unicode/ascii code of a character assigned
    >>> in a string.

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


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

    -Rawat
    V S Rawat, Jun 16, 2004
    #5
  6. V S Rawat wrote:
    > > 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.


    Well, you'de already discovered the charAt() method by yourself,
    and ak's suggestion would also require you to use that first.
    Michael Borgwardt, Jun 16, 2004
    #6
  7. V S Rawat

    Chris Smith Guest

    V S Rawat wrote:
    > 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).

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


    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
    Chris Smith, Jun 17, 2004
    #7
  8. Dale King wrote:

    > Hello, Chris Smith !
    > You wrote:
    >
    >
    >>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).


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

    Mark Thornton
    Mark Thornton, Jun 21, 2004
    #8
  9. V S Rawat

    Roedy Green Guest

    On Sat, 19 Jun 2004 07:06:01 GMT-5, (Dale
    King) wrote or quoted :

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

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 21, 2004
    #9
  10. V S Rawat

    Roedy Green Guest

    Roedy Green, Jun 21, 2004
    #10
  11. V S Rawat

    Dale King Guest

    Hello, Chris Smith !
    You wrote:

    > 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.
    --
    Dale King
    My Blog: http://daleking.homedns.org/Blog
    Dale King, Apr 15, 2006
    #11
    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. Mladen Adamovic
    Replies:
    0
    Views:
    731
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,586
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    496
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,899
    John C. Bollinger
    Nov 19, 2004
  5. Susan Baker
    Replies:
    2
    Views:
    806
    kelvSYC
    Jun 26, 2005
Loading...

Share This Page