What is the PHP function that returns the same thing as "(char) integer"?

Asked

Viewed 41 times

0

I have a code to convert from C# to PHP. This code has an integer to char conversion, and I’m not getting the same result.

Note: I know that PHP is not strongly typed, but in version 7.1 (I am using) it already works with typing.

Code in C# (link to simulate):

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("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]);
    }
}

I’ve used this code converted to PHP ...

<?php
$arr = [];
$codigo = intval(123456);
$v = intval(89012);

$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);

var_dump($arr[0]);
var_dump($arr[1]);
var_dump($arr[2]);
var_dump($arr[3]);
var_dump($arr[4]);
die;

... the functions:

  • Chr()
  • mb_chr()
  • (string)
  • strval()
  • mb_convert_encoding() (both for UTF-16, UTF-8...)

And so far no one has made the same return....

  • What is the expected return and what your tests have returned?

  • Writeline of C#: arr[0]: arr[1]: arr[2]: Ä arr[3]: arr[4]: var_dump of PHP depends on the function it uses but none of them returns the same thing.

  • @bfavaretto if you use http://phptester.net/ and https://dotnetfiddle.net/ to simulate the above codes to get the returns right.

  • 1

    If I’m not mistaken, C# will use UTF-16 on this cast. Try mb_chr ($valor, 'UTF-16')

  • I’m thinking about what might be, the problem is not the conversion, it’s the bit manipulation. The original already looks bad and I have my doubts if it does what it should do, can be an XY problem.

  • @bfavaretto TOP! It worked using UTF-8. Comments there that I put as a response! Thanks!

  • Isabela: If/when I understand why it worked with UTF-8 I put an answer :)

  • I don’t know how it can be working, without the conversion it is already wrong: https://ideone.com/1UEwbU

  • I ran some more tests (PHP, C#). I think these tools output in UTF-16 (in the case of C# and one of my PHP tests), but the browser will always interpret as UTF-8. @Maniero

  • @Mom, the code has syntax error. I already corrected the question but should not have updated yet. After this correction mb_chr() works.

  • @bfvaretto I haven’t gone deep enough to understand yet, but as I’m not going to leave the output to the user is quiet. I’m gonna use it for more calculations that weren’t part of the posting. Even the error in the syntax I told Maniero happened exactly by the c# xD Ctrl+c

Show 6 more comments
No answers

Browser other questions tagged

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