K
K
KraymiKrons2022-02-13 14:53:48
PHP
KraymiKrons, 2022-02-13 14:53:48

How to convert bytes?

In C#, the BitConverter function
How to implement it in PHP?
Here is the PHP code

$string = "RustExtended/4.0 (Bootloader; Data Request)";
$buffer = array();
for($i = 0; $i < strlen($string); $i++){
     $buffer[] = ord($string[$i]);
}
//print_r($buffer);

for ($i = sizeof($buffer) - 2; $i >= 0; $i--)
                {
                    $buffer[$i] ^= $buffer[$i + 1];
                }
                $buffer[sizeof($buffer) - 1] ^= $buffer[0];
                $buffer = implode("", $buffer);
                print_r($buffer);


I am trying to translate from C#
Byte[] buffer = Encoding.UTF8.GetBytes(text);
                for (int i = buffer.Length - 2; i >= 0; i--)
                {
                    buffer[i] ^= buffer[i + 1];
                }
                buffer[buffer.Length - 1] ^= buffer[0];
                return BitConverter.ToString(buffer).Replace("-", "");


In C# I get the following
encryption

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
twobomb, 2022-02-13
@KraymiKrons

function myfunc($text){
  $text = utf8_encode($text);

  $buffer = [];
  foreach(mb_str_split($text) as $v)
    $buffer[] = ord($v);

  for ($i = count($buffer)-2; $i >= 0; $i--)
    $buffer[$i] ^= $buffer[$i+1];

  $buffer[count($buffer) - 1] ^= $buffer[0];

  return implode(array_map("dec2hexbyte", $buffer));
}

function dec2hexbyte($number){
  $hexval = dechex($number);
  if(mb_strlen($hexval) == 1)
    $hexval = "0".$hexval;
    return mb_strtoupper($hexval);
}


$text = "my text";
echo myfunc($text);

A
Anton Shamanov, 2022-02-13
@SilenceOfWinter

decbin/bindec pack/unpack base_convert

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question