使用 PHP 解码 16 位的 BASE64 编码的有符号整数
本文关键字:符号 整数 编码 BASE64 PHP 解码 位的 使用 | 更新日期: 2023-09-27 17:56:41
我有一个 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;
}
这是 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/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AAAACAAAAAAACAAAAAAACAAIA
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...
好
PHP 使得读取 base64 数据并将其转换为有符号整数数组变得非常容易。请务必阅读解包功能,这很棒。
$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));