Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用PHP解码16位的BASE64编码有符号整数_C#_Php_Bytearray - Fatal编程技术网

C# 使用PHP解码16位的BASE64编码有符号整数

C# 使用PHP解码16位的BASE64编码有符号整数,c#,php,bytearray,C#,Php,Bytearray,我有一个Base64编码的字符串,我正在使用下面的c代码对其进行解码。里面应该是存储在16位的有符号整数的范围 现在我需要用PHP来做,但我遇到了真正的麻烦。有人能帮忙吗 基本上,任何人都可以将此代码转换为php吗 private string decode(string encoded) { Byte[] newBytes = Convert.FromBase64String( encoded.Replace("\n","")

我有一个Base64编码的字符串,我正在使用下面的c代码对其进行解码。里面应该是存储在16位的有符号整数的范围

现在我需要用PHP来做,但我遇到了真正的麻烦。有人能帮忙吗

基本上,任何人都可以将此代码转换为php吗

    private string decode(string encoded) 
    {
        Byte[] newBytes = Convert.FromBase64String(
            encoded.Replace("\n","")
        );

        int[] newArr = new int[newBytes.Length / 4];
        for (int ctr = 0; ctr < newBytes.Length / 4; ctr++)
            newArr[ctr] = BitConverter.ToInt16(newBytes, ctr * 4);

        return DisplayArray(newArr);
    }

    private string DisplayArray(Array arr)
    {
        string output = "";
        for (int ctr = 0; ctr < arr.GetUpperBound(0); ctr++)
        {
            output += arr.GetValue(ctr)  + ",";
        }

        return output;      
    }
它应该解码成

2,0,0,2,2,2,2,2,2,5,5,5,2,0,0,7,5,5,0,0,0,2,5,5,2,2,0,0,0,2,0,2,5,2,2,2,2,5,2,2,5,5,0,5,2,2,0,5,5,0,2,0,2,2,5,7,5,2,0,2,2,2,2,5,5,2,0,2,2,5,0,-2,2,2,0,2,0,5,5,0,2,2,2,0,0,2,2,2,5,2,5,7,5,5,5,7,7,5,7,7,5,7,7,7,5,2,-2,2,2,5,5,2,2,2,7,2,5,5,0,7,2,5,0,2,2,7,5,2,2,2,7,7,7,7,5,2,5,5,5,10,10,7,10,10,10,12,12,12,17,15,20,20,20,22,17,20,20,22,25,27,27,27,32,35,40,40,40,40,40,37,37,37,40,42,37,35,30,35,25,25,20,15,10,7,2,7,5,2,2,0,0,-2,0,2,-2,0,5,2,0,5,0,0,0,-2,0,-2,0,-2,-2,-5,-2,0,-5,-2,-2, etc...
好人 PHP使读取base64数据并将其转换为有符号整数数组变得非常容易。一定要仔细阅读函数,它很棒

$stringValue = base64_decode($base64data, true);
$integers = unpack("s*", $stringValue);

坏的 这不是你的C代码所做的。将base64转换为原始字节后,它将以4个字节的块处理该数据,将前2个字节解析为int16,并丢弃后2个字节。你的数据有一半会流失


丑陋的 如果C#代码真的是您所追求的算法,那么以下是如何在PHP中实现它:

$stringValue = base64_decode($base64data, true);
$integers = unpack("s*", $stringValue);
// Throw away half of our data. Because... why not? :)
$integers = array_map('array_shift', array_chunk($integers, 2));
好人 PHP使读取base64数据并将其转换为有符号整数数组变得非常容易。一定要仔细阅读函数,它很棒

$stringValue = base64_decode($base64data, true);
$integers = unpack("s*", $stringValue);

坏的 这不是你的C代码所做的。将base64转换为原始字节后,它将以4个字节的块处理该数据,将前2个字节解析为int16,并丢弃后2个字节。你的数据有一半会流失


丑陋的 如果C#代码真的是您所追求的算法,那么以下是如何在PHP中实现它:

$stringValue = base64_decode($base64data, true);
$integers = unpack("s*", $stringValue);
// Throw away half of our data. Because... why not? :)
$integers = array_map('array_shift', array_chunk($integers, 2));

试试这个:Int16[]num=newBytes.Select(x=>(Int16)x.ToArray();试试这个:Int16[]num=newBytes.Select(x=>(Int16)x.ToArray();完美——而且全部也只有两行。我只是在早些时候翻开包裹,但我显然遗漏了什么。对不起,我忘了将c#4更改为a 2,在现有的c#项目中,我们需要忽略一半的数据。非常感谢你!完美——而且全部也只有两行。我只是在早些时候翻开包裹,但我显然遗漏了什么。对不起,我忘了将c#4更改为a 2,在现有的c#项目中,我们需要忽略一半的数据。非常感谢你!