0
I need to take each value of the index of the array and convert it to return the same thing that returns in C# when we use the function (uint).
<?php
$arr = [];
$codigo = intval(123456);
$v = intval(89012);
$arr[0] = ($codigo >> 23);
$arr[1] = (($codigo >> 15) & 0xFF);
$arr[2] = (($codigo >> 7) & 0xFF);
$arr[3] = (($v >> 7) & 0xFF);
$arr[4] = ($v & 0xFF);
$arr[0] = ($arr[0] == 0) ? '' : mb_chr($arr[0], 'UTF-8');
$arr[1] = ($arr[1] == 0) ? '' : mb_chr($arr[1], 'UTF-8');
$arr[2] = ($arr[2] == 0) ? '' : mb_chr($arr[2], 'UTF-8');
$arr[3] = ($arr[3] == 0) ? '' : mb_chr($arr[3], 'UTF-8');
$arr[4] = ($arr[4] == 0) ? '' : mb_chr($arr[4], 'UTF-8');
//converter para uint cada índice do array $arr
var_dump($arr[0]);
var_dump($arr[1]);
var_dump($arr[2]);
var_dump($arr[3]);
var_dump($arr[4]);
die;
?>
In C# the code would look like this:
using System;
public class Program {
public static void Main(){
gerar(89012, 123456);
}
public static void gerar(int valor, int codigo) {
char[] arr = new char[8];
int v = Convert.ToInt32(valor);
arr[0] = (char)(codigo >> 23);
arr[1] = (char)((codigo >> 15) & 0xFF);
arr[2] = (char)((codigo >> 7) & 0xFF);
arr[3] = (char)((v >> 7) & 0xFF);
arr[4] = (char)(v & 0xFF);
Console.WriteLine("Valor a ser convertido: ");
Console.WriteLine("arr[0]: " + arr[0]);
Console.WriteLine("arr[1]: " + arr[1]);
Console.WriteLine("arr[2]: " + arr[2]);
Console.WriteLine("arr[3]: " + arr[3]);
Console.WriteLine("arr[4]: " + arr[4]);
Console.WriteLine("\n\nConvertido: ");
Console.WriteLine("arr[0]: " + (uint) arr[0]);
Console.WriteLine("arr[1]: " + (uint) arr[1]);
Console.WriteLine("arr[2]: " + (uint) arr[2]);
Console.WriteLine("arr[3]: " + (uint) arr[3]);
Console.WriteLine("arr[4]: " + (uint) arr[4]);
}
}
The output of C# (and I need it to be PHP too) is:
Valor:
arr[0]:
arr[1]:
Arr[2]: Ä
arr[3]:
arr[4]:
Converted:
Arr[0]: 0
Arr[1]: 3
Arr[2]: 196
Arr[3]: 183
Arr[4]: 180
PHP doesn’t support this, we can’t do these algorithm translations this way so yesterday I said the problem was XY, the solution is to know what should do and build otherwise. I don’t even know if this is really necessary, but I’m just seeing a bit of the question.
– Maniero
@Maniero, yesterday, in the other question you said that the code was not working (and it was not, I answered you there), but that problem was solved and is working. If this I will manage to do, here is another case. I believe I have a way to solve, just not found yet :)
– Isa
From the conversation between you and @Maniero we understand that there is another question related to this already solved. Reference her in this question so that other people who access this question have more context.
– DNick
@Dnick doesn’t think it makes much difference, but he follows the link. https://answall.com/questions/423776/qual-a-fun%C3%A7%C3%a3o-em-php-que-volte-a-mesma-coisa-que-char-inteiro
– Isa