Android Question [Solved] Convert Delphi function to B4A

asales

Expert
Licensed User
Longtime User
I tried to convert the function below from Delphi to B4A, but I'm stuck in ths part of the code:
B4X:
 NewText := NewText + Chr((Ord(Key(x)) xor Ord(Text(y))));
What is an how to use the equivalent commands (chr, xor, ord) in B4A?
B4X:
function ShuffleText(Text, Key: String): String;
var
x, y: Integer;
NewText: String;
begin
For x := 1 To Length(Key) Do
begin
  NewText := '';
   For y := 1 To Length(Text) Do
    NewText := NewText + Chr((Ord(Key[x]) xor Ord(Text[y])));
   Text := NewText;
end;
Result := Text;
end;
B4X:
Sub ShuffleText(Text As String, Key As String) As String
    Dim x, y As  Int
    Dim NewText As String

    For x := 0 To Key.Length - 1
        NewText = ""
        For y = 0 To Text.Length - 1
            NewText = NewText + Chr((Ord(Key(x)) xor Ord(Text(y))));
        Next
     
        Text = NewText
    Next

    Return Text
End Sub
This is what the Delphi commands do:
The chr function converts an IntValue integer into either an AnsiChar or WideChar as appropriate.
The ord function returns an integer value for any ordinal type Arg.
The xor keyword is used to perform a logical or boolean 'Exclusive-or' of two logical values. If they are different, then the result is true.

Any help are welcome. Thanks.
 
Last edited:

asales

Expert
Licensed User
Longtime User
Solved!
B4X:
Sub ShuffleText(Text As String, Key As String) As String
    Dim x, y As  Int
    Dim NewText As String

    For x = 0 To Key.Length - 1
        NewText = ""
        For y = 0 To Text.Length - 1
            NewText = NewText & Chr( Bit.Xor(Asc(Key.CharAt(x)), Asc(Text.CharAt(y))))
        Next
        
        Text = NewText
    Next

     Return Text
End Sub
 
Upvote 0
Top