# Convert ascii to character and vice-versa

• 01-16-2001, 11:24 PM
Steve
Convert ascii to character and vice-versa

How do I convert an ascii integer to a character and vice-versa in Java?

Thanks,

Steve Shier
• 01-17-2001, 10:38 AM
Paul Clapham
Re: Convert ascii to character and vice-versa
There's no such thing as an ASCII integer. ASCII is a character set. Maybe
you could explain what you're trying to do in some other words?

PC2

Steve <sshier@3millsoft.com> wrote in message
news:3a651e67\$1@news.devx.com...
>
> How do I convert an ascii integer to a character and vice-versa in Java?
>
>
> Thanks,
>
>
> Steve Shier

• 01-17-2001, 03:13 PM
Steve Shier
Re: Convert ascii to character and vice-versa

Thank you Paul. I'm trying to do 2 things :

1) Take a decimal integer , say, 65 and convert it to its ascii equivalent,
"A". In VB this is done by using the function Chr(65) which returns "A"
. I have no solution for doing this in Java other than chreating a map to
map all ascii integers to their corresponding characters. This problem is
so basic that I feel I'm missing something.

2) Take a character, say , "A" and find out the ascii byte integer which
corresponds to it. In VB this is done by using
Asc("A") which returns the integer 65 . I have temporarily solved this second
problem by using:

int iAscii;
byte[] charbytes;

charbytes = sValue.substring(i, 1).getBytes();
iAscii=charbytes[0];

sValue contains a string in which I need to convert each byte to its Ascii
code (its in a loop and the index i iterates through the string). My method
appears to be overly complex-in VB this can be done in one line and I wonder
if it might be possible in Java to do this more simply.

Steve Shier

"Paul Clapham" <pclapham@core-mark.com> wrote:
>There's no such thing as an ASCII integer. ASCII is a character set. Maybe
>you could explain what you're trying to do in some other words?
>
>PC2
>
>Steve <sshier@3millsoft.com> wrote in message
>news:3a651e67\$1@news.devx.com...
>>
>> How do I convert an ascii integer to a character and vice-versa in Java?
>>
>>
>> Thanks,
>>
>>
>> Steve Shier

>
>

• 01-18-2001, 11:30 AM
Paul Clapham
Re: Convert ascii to character and vice-versa
You are correct, it is so basic that you are missing something. You can
cast between char and int:

int i = 65;
char c = (char)i;

char c = 'A';
int i = (int)c;

PC2

Steve Shier <sshier@3millsoft.com> wrote in message
news:3a65fd06\$1@news.devx.com...
>
> Thank you Paul. I'm trying to do 2 things :
>
> 1) Take a decimal integer , say, 65 and convert it to its ascii

equivalent,
> "A". In VB this is done by using the function Chr(65) which returns "A"
> I have no solution for doing this in Java other than chreating a map to
> map all ascii integers to their corresponding characters. This problem is
> so basic that I feel I'm missing something.
>
> 2) Take a character, say , "A" and find out the ascii byte integer which
> corresponds to it. In VB this is done by using
> Asc("A") which returns the integer 65 .

• 01-18-2001, 08:43 PM
Steve Shier
Re: Convert ascii to character and vice-versa

Thanks a bunch Paul. If I was any smarter I'd feel like an idiot :)) . I
can't tell you how much time I wasted on this, but I chalk it up to being
a future expert.

Thanks again.

Steve

"Paul Clapham" <pclapham@core-mark.com> wrote:
>You are correct, it is so basic that you are missing something. You can
>cast between char and int:
>
>int i = 65;
>char c = (char)i;
>
>char c = 'A';
>int i = (int)c;
>
>PC2
>
>Steve Shier <sshier@3millsoft.com> wrote in message
>news:3a65fd06\$1@news.devx.com...
>>
>> Thank you Paul. I'm trying to do 2 things :
>>
>> 1) Take a decimal integer , say, 65 and convert it to its ascii

>equivalent,
>> "A". In VB this is done by using the function Chr(65) which returns "A"
>> I have no solution for doing this in Java other than chreating a map

to
>> map all ascii integers to their corresponding characters. This problem

is
>> so basic that I feel I'm missing something.
>>
>> 2) Take a character, say , "A" and find out the ascii byte integer which
>> corresponds to it. In VB this is done by using
>> Asc("A") which returns the integer 65 .

>
>