Android Question Help Required in PHP Function

TheRnker

Member
Hi Everyone,

I need some help, I have a function in B4A which does basic ASCII shift on a string input. I use this function to do a basic encryption on my data. My PHP knowledge is very poor and having tried multiple times I am not able to get the below function converted to PHP.

I would appreciate if someone could convert the below into PHP

B4A Function:
Sub AsciiSwitch(InputString As String, ValueToAdd As Int) As String
    Dim OutputString As String
    OutputString=""
    Dim c As Char
    For i = 0 To InputString.Length - 1
        c = InputString.CharAt(i)
        OutputString = OutputString & Chr(Asc(c) + ValueToAdd)
    Next

    Return OutputString
End Sub

Thank you in advance
 

TILogistic

Expert
Licensed User
Longtime User
echo AsciiSwitch("hello", 3);
PHP:
function AsciiSwitch($InputString, $ValueToAdd) {
    $InputArray = str_split($InputString);
    $OutputString = "";
    for($i = 0; $i < count($InputArray); ++$i) {
        $OutputString = $OutputString . Chr(Ord($InputArray[$i]) + $ValueToAdd);
    }
    return $OutputString;
}

note:
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I use this function to do a basic encryption on my data.
Please note that what you're doing is using a Caesar cipher, which Julius Caesar used some time around 50BC. To say it's insecure is an understatement. If you're using this to hide information from small children, it's probably fine, but you might want to read up on other alternatives on encryption here in the forum for all other uses. I believe I've seen some solution where there's also a matching piece of php code to use.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Please note that what you're doing is using a Caesar cipher, which Julius Caesar used some time around 50BC. To say it's insecure is an understatement. If you're using this to hide information from small children, it's probably fine, but you might want to read up on other alternatives on encryption here in the forum for all other uses. I believe I've seen some solution where there's also a matching piece of php code to use.
 
Upvote 0

FrostCodes

Active Member
Licensed User
Longtime User
Another implementation:
<?php
function AsciiSwitch($InputString, $ValueToAdd){
$OutputString = "";

for ($i = 0; $i <= strlen($InputString) - 1; $i++) {
$c = $InputString[$i];
       $OutputString = $OutputString. chr(ord($c) + $ValueToAdd);
}

return $OutputString;
}


$a= AsciiSwitch('frost hello hi', 12);

echo $a.'<br>';

echo 'ORGINAL is:  '. AsciiSwitch($a, -12);
 
Upvote 0

TheRnker

Member
Another implementation:
<?php
function AsciiSwitch($InputString, $ValueToAdd){
$OutputString = "";

for ($i = 0; $i <= strlen($InputString) - 1; $i++) {
$c = $InputString[$i];
       $OutputString = $OutputString. chr(ord($c) + $ValueToAdd);
}

return $OutputString;
}


$a= AsciiSwitch('frost hello hi', 12);

echo $a.'<br>';

echo 'ORGINAL is:  '. AsciiSwitch($a, -12);
Thanks, really appreciate it
 
Upvote 0
Top