Chr function

N

Novello

Hello everybody,

Visual Basic has this function:

Chr(2) that is the 0x2 char

How can I get it in java?

thank you
bye
 
O

Oliver Wong

Novello said:
Hello everybody,

Visual Basic has this function:

Chr(2) that is the 0x2 char

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
 
M

Mike Schilling

Oliver said:
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.

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

Danno

Mike said:
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.

I think you meant to do

char myChar = (char) i; // whereas i is an integer;
 
D

Danno

Mike said:
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.

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

Lew

Danno said:
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.

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

- Lew
 
D

Danno

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

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

John O'Conner

Novello said:
Hello everybody,

Visual Basic has this function:

Chr(2) that is the 0x2 char

How can I get it in java?

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
 
O

Oliver Wong

Danno said:
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.

I'm guessing you meant:

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

=)

- Oliver
 

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

Similar Threads

Argparse error using NodeJS 0
Function Help 1
chr / ord 3
Correspondence of chr$()? 5
Boomer trying to learn coding in C and C++ 6
Newbie... 2
Bool inside switch 1
Version of Pythons eval() function in Java 0

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top