## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

Simplest way to convert integer to char Posted by blackr1234 on 5 Feb 2011 at 5:42 AM
Integer to char, the simplest way.

Like the str function: str(integer VAR, string VAR).
But I need integer VAR --> char VAR.

Example:
1 to '1'
123 to '123'
Also, -1 to '-1' if possible.

Re: Simplest way to convert integer to char Posted by _Atex_ on 5 Feb 2011 at 3:38 PM
: Integer to char, the simplest way.
:
: Like the str function: str(integer VAR, string VAR).
: But I need integer VAR --> char VAR.
:
: Example:
: 1 to '1'
: 123 to '123'
: Also, -1 to '-1' if possible.
:
:

You gotta use the str procedure and process the resulting string, easy, since every string is an array of chars. Even if would be possible, you couldn't convert 123 or -1, only single digit positive integers. Here's a simple method:
```var i,j:integer;
s:string;
ch:array[0..255] of char absolute s;
{ "ch" occupies the same place in memory as "s"}

begin
repeat
write('Enter a value (0 to quit): ');readln(i);
str(i,s);
for j:=1 to byte(ch[0]) do  { ch[0] is the length marker }
writeln('ch[',j,']=',ch[j]);writeln;
until i=0;
end.```

Re: Simplest way to convert integer to char Posted by blackr1234 on 6 Feb 2011 at 10:05 AM

Question: As you have said only a single digit is possible, then how about chr(integer+48)?

Please check your mailbox, I have mailed you a letter 2 days ago.

Thanks v. much,
blackr1234
Re: Simplest way to convert integer to char Posted by _Atex_ on 6 Feb 2011 at 11:31 PM

:
: Question: As you have said only a single digit is possible, then how