How do I convert the php array values to the "uint" type of C#?

Asked

Viewed 82 times

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, 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 :)

  • 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 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

1 answer

0


You can not convert up because the value Unit do C# is a specific value of the language constructor and compiler itself as are some things in PHP as well (Iterator, Exceptions among others).

When you need or will need in the future again to communicate between 2 different languages it is always advisable to usewhether the JSON standard for passing values that is globally supported by almost all modern languages and that has constant updates nowadays (mainly web-based languages).

Another way of also making this communication between languages and checking the primitive types, for example, regardless of the compiler or engine that the 2 languages you are using to communicate the primitive types are totally inflexible, for example, an Integer in C# is the same thing as an Integer in PHP the only maximum variation you can have between them is the scalar type and when bytes it supports, but for most of the time it works very well strings for example, only floats can be a little complicated because languages like Python that were developed for ambitos totally aimed at exact and pure mathematicatics the numbers have their own scalar types, but in large majority they get along very well.

  • Fábio, really if they had used the primitive types I would be a happy person. But this code is embedded in various equipment. So I don’t have much option to change the calculation... I’ll end up doing in C++ and send for there... I think it will be the best option.

  • 1

    Even if the primitive types are shipped they do not change, you just need to be careful with the type of Float that it is the one that gives more problems in transitions of compilation environments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.