dongsi1954
2016-12-13 20:41
浏览 544
已采纳

PHP:Base64解码将字符串转换为其他格式,如十进制或二进制

I'm trying to use a service which "base64 converts" their data which they then push to my server. The data is divided into three data types: strings, integers and booleans. Why they package it this way I don't understand, but it's up to me to decipher it.

I have a string Qjo0MDk2 which should convert to B:4096 and PHP's native base64_decode function works!

However, if I try to convert AAATmg== to a base-10 (decimal) integer value, I want to get 5018, but base64_decode just gives me nothing. (I assume because it's trying to convert to a string, rather than a base-10 integer.)

Likewise, AA== should convert to 0 in base-2 (binary) boolean values, while AQ== should convert to 1 in the same.

Is there a set of functions that does this already somewhere? I can't imagine this is new. Here is a website that does it today, but the code is not exposed: https://conv.darkbyte.ru/

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用“base64转换”其数据的服务,然后将这些数据推送到我的服务器。 数据分为三种数据类型:字符串,整数和布尔值。 为什么他们以这种方式打包它我不明白,但是由我来解读它。

我有一个字符串 Qjo0MDk2 应转换为 B:4096 和PHP的原生 base64_decode 函数有效!

但是,如果我尝试转换 AAATmg == 对于一个基数为10(十进制)的整数值,我想得到 5018 ,但 base64_decode 只是没有给我任何东西。 (我假设因为它试图转换为字符串,而不是基数为10的整数。)

同样, AA == 应转换为 基数为2(二进制)布尔值时为0 ,而 AQ == 应转换为 1 < p>是否有一组函数可以在某处执行此操作? 我无法想象这是新的。 这是一个今天就做的网站,但代码没有公开: https://conv.darkbyte.ru/ < / a>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhiyi2006 2016-12-13 21:07
    已采纳

    Binary data can't be simply printed, that's why you don't see something from base64_decode, but the data is there. If you want to actually see something, you need to convert the data to hexadecimal (technically into a hexadecimal representation). Since it looks like that the third party application is doing that (though in the other direction), you will have to do that for all data.

    The data representation for AA== and AQ== is the same when you use them in an if statement, even though they are 00 and 01 hexadecimal-wise. They're true-ish to PHP, thus executing the if part. If you actually want to check their state, you will have to convert them to int (from the hexadecimal representation).

    (int) bin2hex(base64_decode("AA==")) // int(0)
    (int) bin2hex(base64_decode("AQ==")) // int(1)
    

    Trying to convert from binary to int directly will result in int(0). So you have to be cautious when you deal with data from the third party application.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题