Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String conversion

Status
Not open for further replies.

664950

Programmer
Aug 27, 2001
28
0
0
BA
PLease tell me how to pass string to Ord(x) function.
How to convert string to char data type because my x is string and ord(x) is working with char
 
[blue]
Code:
:
:
var
  n:integer;
  s:string;
begin
  n := Ord( s[1] );
:
:
[/color]
 
Thank you


And reverse char to string please?
 
You can assign a char directly to a string. After all a char is a string of length = 1. You would do the following in code:
Code:
var
  c: char;
  s: string;
begin
  c := 'a';
  s := c; // string s now contains the letter 'a'
  ShowMessage(s); 
end;

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
If by reverse char to string you mean ascii number to string, then you can do this:
[blue]
Code:
:
:
var
  n:integer;
  s:string;
begin
  s := Chr(n);
:
:
[/color]


 
This is a little program I tried to convert from VB to Delphi. I have error in line DecryptStr := DecryptStr + Chr(Ord(sPom[1]) - EncCode);




unit fileEncriptDecript;

interface
uses
Messages, SysUtils, Variants, Classes,
ExtCtrls, ComCtrls,StrUtils;


implementation

Function Add128(strul : String) : String;
var
strIz :String;
i : Integer;
sPom : String;
sPomi : Integer;
begin
strIz :='';
For i:=1 to Length(strul) do
begin
sPom := MidBStr(strul,i,1);
sPomi := StrToInt(sPOm);
strIz := strIz + Chr(Ord(sPomi + 128));
end;
Add128 := strIz
end;

Function CreateEncryptCode(Key : String) : Integer;
var
Total1,Total2 : Integer;
NbChars1, NbChars2 : Integer;
Counter : Integer;
sPom : String;

begin
Total1 := 0;
Total2 := 0;
NbChars1 := 0;
NbChars2 := 0;

For Counter := 1 to Length (Key) do
case (Counter mod 2) of
0 : begin
sPom := MidStr(Key, Counter, 1);
Total1 := Total1 + Ord(sPom[1]);
NbChars1 := NbChars1 + 1
end
else
begin
sPom := MidBStr(Key, Counter, 1);
Total2 := Total2 + Ord(sPom[1]);
NbChars2 := NbChars2 + 1
end
end;

If (NbChars1>0) AND (NbChars2>0) Then
CreateEncryptCode := Abs((Total1 div NbChars1)-(Total2 div NbChars2))
else
CreateEncryptCode := 1
end;

Function DecryptStr(EncStr : String; Key : String) : String;
var
Counter : Integer;
EncCode : Integer;
sPom : String;
begin
EncCode := CreateEncryptCode(Key);
DecryptStr := '';
EncStr := Add128(EncStr);
For Counter := 1 To Length (EncStr) do
case (Counter mod 2) of
0 : begin
sPom := MidBStr(EncStr, Counter, 1);
DecryptStr := DecryptStr + Chr(Ord(sPom[1]) - EncCode);
end
else
begin
sPom := MidBStr(EncStr, Counter, 1);
DecryptStr := DecryptStr + Chr(Ord(sPom[1]) + EncCode);
end
end;

end;
end.


 
When I put

DecryptStr := Chr(Ord(sPom[1]) + EncCode);

it is OK
 
I don't know if this will help (it is equivalent to using the '+') but try it anyway:
Code:
DecryptStr := Concat(DecryptStr, Chr(Ord(sPom[1]) + EncCode));

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Its a conversion thing. It's trying to read "DecryptStr" on the right-hand side of the equal sign by making a (recursive) call to DecryptStr.

Do it like this instead:
Code:
  Result :=  Result + Chr(Ord(sPom[1]) - EncCode);
  :
  :
  Result :=  Result + Chr(Ord(sPom[1]) + EncCode);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top