gpt4 book ai didi

php - 如何在 php 中将 base64 转换为十六进制?

转载 作者:可可西里 更新时间:2023-11-01 00:30:03 28 4
gpt4 key购买 nike

我想用这个:

<?php
$string1 = 'V2h5IEkgY2FuJ3QgZG8gdGhpcyEhISEh';
echo base64_decode($string1);
?>

此示例的输出应始终为 18 个字符!但有时这个输出小于 18。

24(base64 字符)乘以 6(每个 base64 字符的位数)等于 144(位)除以 8(每个 ASCII 字符的位数)等于 18 个 ASCII 字符。

问题是输出以纯文本显示;有些字符甚至没有“文本表示”,数据将会丢失。下一个测试将显示有 41 个不同的 ASCII 字符,没有可见的输出。

<?php
for ($i = 0; $i <= 255; $i++) {
$string2 = chr($i);
echo $i . " = " . $string2 . "<br>";
}
?>

我的计划是解码 base64 字符串并从 ASCII 格式的输出中将其重新转换为十六进制。由于这 41 个字符,现在这是不可能的。

我也试过 base_convert 但没有 base64 支持。

最佳答案

您可以使用 bin2hex() 执行此操作:

Returns an ASCII string containing the hexadecimal representation of str. The conversion is done byte-wise with the high-nibble first.

php > $string1 = 'V2h5IEkgY2FuJ3QgZG8gdGhpcyEhISEh';
php > echo base64_decode($string1);
Why I can't do this!!!!!
php > echo bin2hex(base64_decode($string1));
57687920492063616e277420646f20746869732121212121
php >

关于php - 如何在 php 中将 base64 转换为十六进制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37648273/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com