Recent

Author Topic: [SOLVED] Convert String to ASCII code as Integer  (Read 23519 times)

RobWitcher

  • New Member
  • *
  • Posts: 31
[SOLVED] Convert String to ASCII code as Integer
« on: April 16, 2013, 10:07:46 pm »
Hi

I am trying to convert a string (it has to be a string, not a char) of one letter (A/B/C/D/...) into it's ASCII code stored as an integer.
For example the letter 'A' in ASCII is the number 65 as an integer and 'B' is 66, and so on.
This seems like a fairly simple thing to do but I've been coding my program for many hours and I can barely think now.
Any help is appreciated, thanks.

Rob
« Last Edit: April 16, 2013, 10:47:20 pm by RobWitcher »
-Rob

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Convert String to ASCII code as Integer
« Reply #1 on: April 16, 2013, 10:10:54 pm »
Code: [Select]
ShowMessage(Chr(Ord('A')));
« Last Edit: April 16, 2013, 10:28:04 pm by typo »

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Convert String to ASCII code as Integer
« Reply #2 on: April 16, 2013, 10:13:19 pm »
I tried that before but get this message:
Quote
unit1.pas(110,19) Error: Ordinal expression expected
-Rob

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Convert String to ASCII code as Integer
« Reply #3 on: April 16, 2013, 10:27:19 pm »
Click on the word "Select" on post above, copy it and try again.

Silvio Clécio

  • Guest
Re: Convert String to ASCII code as Integer
« Reply #4 on: April 16, 2013, 10:30:25 pm »
To get ASCII code of A char, you can do:

Code: [Select]
Write(#$41);
or:

Code: [Select]
Write(Char($41));
or:

Code: [Select]
with TFileStream.Create('out.txt', fmCreate) do
try
  WriteByte($41);
finally
  Free;
end;

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Convert String to ASCII code as Integer
« Reply #5 on: April 16, 2013, 10:33:31 pm »
Click on the word "Select" on post above, copy it and try again.

it works like that but I want it similar to this
Code: [Select]
ShowMessage(IntToStr(Ord('A')));
So it outputs the number, but instead of using 'A', I want to do it with a string, for example
Code: [Select]
ShowMessage(IntToStr(Ord(MyString)));

But that doesn't work.

 %)
-Rob

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Convert String to ASCII code as Integer
« Reply #6 on: April 16, 2013, 10:36:21 pm »
To get ASCII code of A char, you can do:

Code: [Select]
Write(#$41);
or:

Code: [Select]
Write(Char($41));
or:

Code: [Select]
with TFileStream.Create('out.txt', fmCreate) do
try
  WriteByte($41);
finally
  Free;
end;

Thanks but I'm not after it from a char.
The data I want converted is a string eg
Code: [Select]
MyString : String;
...
MyString := 'A';

and I want to be able to convert that string of 'A' into an integer value, which would be 65, to do calculations etc with it.
-Rob

Silvio Clécio

  • Guest
Re: Convert String to ASCII code as Integer
« Reply #7 on: April 16, 2013, 10:38:46 pm »
Ah, OK. :)

So you can:

Code: [Select]
var
  S: string;
begin
  S := 'A';
  Write(IntToStr(Ord(S[1])));
end;

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Convert String to ASCII code as Integer
« Reply #8 on: April 16, 2013, 10:41:45 pm »
Ah, OK. :)

So you can:

Code: [Select]
var
  S: string;
begin
  S := 'A';
  Write(IntToStr(Ord(S[1])));
end;

Thank you so much...
...I don't suppose you know what the
Quote
[1]
does? I've never seen that before.

Thanks, again.
-Rob

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Convert String to ASCII code as Integer
« Reply #9 on: April 16, 2013, 10:43:21 pm »
Code: [Select]
var
  S: string;
begin
  S := 'SomeString';
  for i := 1 to Length(S) do
    Write(IntToStr(Ord(S[i])));
end;
« Last Edit: April 16, 2013, 10:46:18 pm by typo »

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Convert String to ASCII code as Integer
« Reply #10 on: April 16, 2013, 10:44:34 pm »
Code: [Select]
var
  S: string;
begin
  S := 'SomeString';
  for i := 1 to Length(S) do
    Write(IntToStr(Ord(S[1])));
end;

Thanks for your help, I guess that could help too.
-Rob

Silvio Clécio

  • Guest
Re: Convert String to ASCII code as Integer
« Reply #11 on: April 16, 2013, 10:47:13 pm »
Ah, OK. :)

So you can:

Code: [Select]
var
  S: string;
begin
  S := 'A';
  Write(IntToStr(Ord(S[1])));
end;

Thank you so much...
...I don't suppose you know what the
Quote
[1]
does? I've never seen that before.

Thanks, again.

S[1] is same to Copy(S, 1, 1), ie, it return the first char of a string. :)

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Convert String to ASCII code as Integer
« Reply #12 on: April 16, 2013, 10:48:45 pm »
Ah, OK. :)

So you can:

Code: [Select]
var
  S: string;
begin
  S := 'A';
  Write(IntToStr(Ord(S[1])));
end;

Thank you so much...
...I don't suppose you know what the
Quote
[1]
does? I've never seen that before.

Thanks, again.

S[1] is same to Copy(S, 1, 1), ie, it return the first char of a string. :)

Ohhhh... I see now. My bad, I have used that before, it just didn't click in my mind.
Cheers.
-Rob

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: [SOLVED] Convert String to ASCII code as Integer
« Reply #13 on: April 16, 2013, 10:50:24 pm »
It is not 1 (one) but i (I).

Silvio Clécio

  • Guest
Re: [SOLVED] Convert String to ASCII code as Integer
« Reply #14 on: April 16, 2013, 10:51:37 pm »
... and, if you want to get code of char by char, you can use a small code like:

Code: [Select]
var
  C: Char;
  S: string;
begin
  S := 'SomeString';
  for C in S do
    WriteLn(IntToStr(Ord(C)));
end;

PS1: sorry for my english plz.
PS2: I love Free Pascal. S2
« Last Edit: April 16, 2013, 10:53:55 pm by silvioprog »

 

TinyPortal © 2005-2018