Converting a String to a unique integer

I'm writing a program where I need to convert a password - e.g "hello" to an integer value - e.g 4999 - but so the same letters in a different order - e.g lehol would equal a different value.
LVL 1
bogieman_Asked:
Who is Participating?
 
rarigoConnect With a Mentor Commented:
I think something like this would make it.

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}
Uses Math;

function CalculateValue( Letter : Char; Position : Integer ) : Integer;
Var
    i : Integer;
begin
    i := Round( Power( 10, Position ));
    Result := Ord( Letter ) * i;
end;


procedure TForm1.Button1Click(Sender: TObject);
Var
   n, i    : LongInt;
begin
   i := 0;
   for n := 1 to Length( Edit1.Text ) do
       i := i + CalculateValue( Edit1.Text[n], n );
   Label1.Caption := IntToStr( i );

end;

end.



No?


0
 
rarigoCommented:
function CalculateValue( Letter : Char; Position : Integer ) : Integer;
Var
    i : Integer;
begin
    i := Round( Power( 2, Position ));
    Result := Ord( Letter ) * i;
end;


Would be better. Not to run out of integer too fast.

0
 
kretzschmarCommented:
hi rarigo,
and otherwise?
meikl
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
simonetCommented:
What do you need that for?

Depending on your needs (digital signatures) you can use CRC32.

Alex
0
 
rarigoCommented:
Hi Kretzschmar,

   Sorry. I didn't get you.

Reginaldo
0
 
IndefreiCommented:
Reginaldo
meikl likes to see the way back from the integer, you build,
to e.g. the original password (hello)
Indi
0
 
edeyCommented:
If you don't need the integer as one coherent number you could simply recast the string as a pchar


GL
Mike
0
 
edeyCommented:
If you don't need the integer as one coherent number you could simply recast the string as a pchar


GL
Mike
0
 
rarigoCommented:
Hi indi,

  I don't think you need that. In fact that's no recomended to "decrypt" a password.


Reginaldo
0
 
LexZEUSCommented:
Hi,

I think Bogieman want to decrypt the integer to the string instead of just encrypt. If it's the case, I would like to know about the max length of the string, the characters ( I mean u use case sensitive ? You only use alphabet ? etc. )  

LexZEUS
0
 
MotazCommented:
If you use Delphi 4, you can use Int64 if you need long key value :

function Power(a, b: integer): Int64;
var
  i: integer;
begin
  Result:= 1;
  for i:= 1 to b do
    Result:= Result * a;
end;

function GetPasswordValue(Password: string): Int64;
var
  i: integer;
begin
  Result:= 0;
  for i:= 1 to Length(Password) do
    Result:= Result + Power(255, i-1);
end;

Motaz
0
 
LischkeCommented:
Hi guys,

I just stumbled across a function I never seen before which could just do what bogieman_ needs. have a look at

LHashValOfName

(D5, don't know if lower versions have this one) and

LHashValOfNameSys

(WinAPI).

Ciao, Mike
0
 
simonetCommented:
It would help if the user said what he wants with a little more precision and details.
0
 
LischkeCommented:
Ah Alex, there's an open question for you at:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10216665 

Ciao, Mike

PS: bogieman_ is well known being someone who asks questions with very low points and does seldom reply :-/
0
 
bogieman_Author Commented:
Very well done! I changed it to a function where you just input a string, but that was helpful anyway
0
 
MoondancerCommented:
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0
All Courses

From novice to tech pro — start learning today.