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

时间:2016-07-12 15:54:09

标签: c# php bytearray

我有一个Base64编码的字符串,我使用以下c#代码对其进行解码。 Inside应该是以16bit存储的一系列有符号整数。

现在我需要在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;      
    }

这是base64字符串....

AgACAAAAAgAAAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAUABQAFAAUABQAF
AAUAAgACAAAA/v8AAAIABwAHAAUABQAFAAUAAAAAAAAAAgAAAAAAAgACAAUA
BQAFAAIAAgAFAAIAAAAAAAAAAAACAAAAAAACAAIAAAAAAAIABQAFAAUAAgAC
AAIAAgACAAIAAgACAAUAAgACAAIAAgACAAUABQAFAAIAAAAAAAUA/v8CAAIA
AgACAAAAAgAFAAUABQAFAAAAAgACAAIAAAACAAIABQACAAIABQAFAAcAAgAF
AAUAAgAAAAAAAgACAAUAAgACAAIAAAACAAUABQACAAUABQACAAIAAAACAAIA
AgACAAUABQAFAAAA/v/+/wAAAgACAAIAAAAAAAIAAgAAAAAAAgAFAAUABQAC
AAAAAAACAAIAAgACAAIAAAAAAAIAAAACAAIAAgACAAUAAgAFAAUABQACAAUA
BQAFAAcABQAFAAUABQAFAAUABQAHAAcABwAFAAUABwAHAAUABwAHAAUABwAH
AAUABwAHAAcABQAFAAUAAgAAAP7/AAACAAIAAgACAAUABQAFAAUAAgAFAAIA
BQACAAUABwAFAAIABQAFAAIABQACAAAABwAHAAUAAgAFAAUAAgAAAAAAAgAF
AAIABQAHAAcABQACAAIAAgACAAIAAgAFAAcABQAHAAcABwAHAAcABQAFAAUA
AgACAAUABQAFAAUABQAHAAoACgAKAAoABwAHAAoACgAKAAcACgAKAAwADAAM
AAwADAAPABEAEQAPAA8AFAARABQAFAAUABQAFgAWABEAEQAUABQAFAAWABYA
FgAZABsAGwAbABsAGwAbACAAIAAgACMAJQAoACUAKAAoACgAKAAoACgAKAAl
ACUAJQAlACUAJQAlACgAKgAqACgAJQAjACMAIAAeACAAIwAgABkAGQAZABkA
FAARAA8ADAAKAAcABwAFAAIABQAHAAcABQACAAIAAgACAAIAAAAAAAAA/v/+
/wAAAAAAAAIAAAD+//7/AAACAAUAAgACAAIAAAACAAUAAAAAAAAAAAAAAAAA
/v/+//7/AAAAAP7//v8AAAAA/v/+//7/+//7//v//v8AAAAA/v/7//7//v8A
AP7//v/7//7//v8AAP7/AAAAAAAAAAACAAIAAAAAAAAAAAAAAAIABQAHAAUA
BwAKAAoADwAUABsAIwAqAC8AOQBBAE0AWgBhAGkAcwB/AI4AmwClALEAuwDF
ANIA3gDrAPUABgEaASkBOAFKAV4BdAGGAZIBnwGuAb8BxwHRAdsB6gH2AQAC
BQIAAvkB6gHdAccBrgGVAYMBbQFUATgBGAH1ANwA1wDPAMoAyADAALQApwCb
AJMAiQCCAH0AcwBrAGYAXwBaAFIASwBGAEEAOQAvACUAHgAWABQAEQAMAAwA
DAAMAAwABwAFAAUAAgACAAAA/v8AAAAAAAD+//7/+//7//n/+f/2//b/+f/7
//n/+f/5//n/+//2//T/+//5//n/9v/5//n/9v/2//T/9v/0//n/+f/5//b/
9v/2//n/9v/2//T/9v/5//b/+f/5//n/+f/5//b/9P/2//T/7//x//T/9P/0
//b/9v/0//b/9P/2//b/9v/2//b/9P/x//H/8f/v//H/9P/x//T/9P/0//H/
8f/0//H/8f/x//H/8f/0//H/8f/x//H/8f/s/+z/7//v//H/8f/s/+z/7P/s
/+z/7P/s/+r/6v/s/+r/5//s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AAAACAAIA
AgACAAIAAgACAAIAAAAAAAAAAgACAAIAAgACAAIA/v8AAAIAAgACAAIABQAF
AAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIA
AgACAAIAAgACAAIAAgACAAUAAgACAAIAAgACAAIAAgACAAIAAgAFAAUAAgAA
AAAAAgACAAIAAgACAAIAAgACAAIAAgAFAAUAAgACAAIABQAFAAIAAgACAAAA
AgAFAAUABwAFAAIAAAACAAAA/v/7//7/AAAAAAIABwAHAAUAAgAAAAIABwAF
AAIAAgACAAIAAgACAAIA

它应解码为......

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

1 个答案:

答案 0 :(得分:2)

好的

PHP使得在base64数据中读取并将其转换为有符号整数数组非常容易。请务必阅读unpack功能,这很棒。

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

这不是您的C#代码正在做什么。在将base64转换为原始字节之后,它将以4个字节的块处理该数据,将前2个解析为int16 并丢弃后两个字节。一半的数据耗尽了。

丑陋的

如果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));