Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Chr function

Thread view: 
Novello - 05 Dec 2006 15:53 GMT
Hello everybody,

Visual Basic has this function:

Chr(2)  that is the 0x2 char

How can I get it in java?

thank you
bye
Oliver Wong - 05 Dec 2006 16:32 GMT
> Hello everybody,
>
[quoted text clipped - 3 lines]
>
> How can I get it in java?

   For the benefit of those who are not familiar with VB, you should have
explained that the Chr() function takes a integer representing a char code
(which is either ASCII or ANSI, not sure), and returns the corresponding
character.

   In Java, you can bypass that function altogether, and do something like:

char myChar = 2;

   Which declares a character called "myChar", and assigns it the value 2.
It can simultaneously be thought of as representing the natural number 2,
and the character whose unicode is 2.

   - Oliver
Mike Schilling - 05 Dec 2006 17:14 GMT
>> Hello everybody,
>>
[quoted text clipped - 16 lines]
> value 2. It can simultaneously be thought of as representing the
> natural number 2, and the character whose unicode is 2.

Note that applying this same conversion to an arbitrary integer requires a
cast:

   char myChar = (int) i;

since the range of int exceeds that of char.
Danno - 05 Dec 2006 17:54 GMT
> >> Hello everybody,
> >>
[quoted text clipped - 23 lines]
>
> since the range of int exceeds that of char.

I think you meant to do

char myChar = (char) i; // whereas i is an integer;
Mike  Schilling - 05 Dec 2006 23:23 GMT
>> Note that applying this same conversion to an arbitrary integer requires
>> a
[quoted text clipped - 7 lines]
>
> char myChar = (char) i; // whereas i is an integer;

You are of course correct.
Danno - 05 Dec 2006 18:10 GMT
> >> Hello everybody,
> >>
[quoted text clipped - 23 lines]
>
> since the range of int exceeds that of char.

Actually, now that I think about it....you don't need a cast at all.

It will automatically accept any number from 0-65535 without a cast.
Lew - 05 Dec 2006 18:29 GMT
>>     char myChar = (char) i;
>>
[quoted text clipped - 3 lines]
>
> It will automatically accept any number from 0-65535 without a cast.

Which cannot be determined at compile time. You need the cast.

- Lew
Danno - 05 Dec 2006 20:19 GMT
> >>     char myChar = (char) i;
> >>
[quoted text clipped - 7 lines]
>
> - Lew

char myChar = 2;  //as stated a few messages up works, that's what I
was referring to

int a = 123;
char myChar2 = (char) 2;  //does require a cast

Just to clarify.
Oliver Wong - 05 Dec 2006 21:50 GMT
>> >>     char myChar = (char) i;
>> >>
[quoted text clipped - 13 lines]
>
> Just to clarify.

   I'm guessing you meant:

int a = 123;
char myChar2 = (char) a;  //does require a cast

   =)

   - Oliver
Danno - 06 Dec 2006 04:59 GMT
> >> >>     char myChar = (char) i;
> >> >>
[quoted text clipped - 22 lines]
>
>     - Oliver

Absolutely right, thanks for the help. ;)
Mike  Schilling - 05 Dec 2006 23:25 GMT
>> >> Hello everybody,
>> >>
[quoted text clipped - 26 lines]
>
> Actually, now that I think about it....you don't need a cast at all.

You do when using a variable.
Simon Brooke - 05 Dec 2006 20:01 GMT
> Hello everybody,
>
[quoted text clipped - 3 lines]
>
> How can I get it in java?

(char) 2

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

                               ;; lovely alternative to rice.

John O'Conner - 05 Dec 2006 21:42 GMT
> Hello everybody,
>
[quoted text clipped - 6 lines]
> thank you
> bye

No function necessary:

char ch = '\u0002';

The \u notation requires a 4-digit hex number that represents a UTF-16
Unicode code unit.

For Unicode code points in the supplementary character planes, you must
use a CharSequence, typically a String, with the UTF-16 surrogate pairs:

String str = "\uXXXX\uYYYY"; // XXXX is a leading surrogate value; YYYY
is a trailing surrogate value

Regards,
John O'Conner


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.