将PHP中的十进制数转换为256个ASCII字符,并在复制时保留十进制值。

huangapple go评论78阅读模式
英文:

PHP Dec to 256 ASCII char and keep the Dec value on copy

问题

Sure, here is the translated content:

需要帮助,我有一个字符串传递给加密,返回一个包含一组十进制值的数组,例如:

[0, 1, 22, 33, 44, 55, 66, 77, 88, 99, 100]

为了使它看起来像一个不可读的字符,我想使用 chr() 函数将它转换为字符。问题出现在当它转换为不可见字符时:

i £nEi5uNtVsd-vo

其中的空白可以是空格、制表符或任何其他字符。£ 可以是任何东西,当我复制它时。它不会保持从加密中返回时的正确十进制值。PHP 能够显示所有 256 个 ASCII 字符并保持已转换字符的十进制值吗?

编辑:
实际情况是我有这个:

[105, 26, 32, 163, 110, 69, 105, 53, 117, 78, 116, 86, 115, 6, 100, 45, 118, 12, 0, 111]

不幸的是,具有“十进制 26”的字符在这里无法正确显示,在任何转换器中都显示为“?”。

英文:

Need help, I have a string that goes to an encryption that returning an array of a set dec values like :

[0, 1, 22, 33, 44, 55, 66, 77, 88, 99, 100]

To make it seem like a non readable I want to convert it to char with chr() func. The problem comes when it converting to an invisible char

i £nEi5uNtVsd-vo

the blank one can be space, tab, or anything. the can be anything too when I copy it. It doesn't keep the right dec value like when it returning from the encryption. Can php show all the 256 ascii char and still keep the dec value of every char that has been converted?

Edit:
real case I have this

[105, 26, 32, 163, 110, 69, 105, 53, 117, 78, 116, 86, 115, 6, 100, 45, 118, 12, 0, 111]

unfortunately char with dec 26 doesn't show properly here, in any of converter out there it shown as ?

答案1

得分: 1

没有。

不是每个从0到256的整数值都具有可打印的表示与语言无关。这是人们所称的“二进制字符串”、“字节数组”等。

不应将二进制字符串呈现为文本,而且绝对不应该复制/粘贴,因为正如您所经历的,不可打印的部分往往无法保留,更不用说对于大于127的序数值,也就是“非ASCII”或“非7位安全”的编码不匹配问题的高概率。

如果您出于任何原因希望将这些值表示为文本,您将需要应用一个字符串安全编码方案,就像Topaco建议的那样。例如:

$in_arr = [0, 1, 22, 33, 44, 55, 66, 77, 88, 99, 100];
$str = implode('', array_map('chr', $in_arr));

var_dump(
    bin2hex($str),
    base64_encode($str)
);

输出:

string(22) "000116212c37424d586364"
string(16) "AAEWISw3Qk1YY2Q="
英文:

No.

The fact that not every integer value from 0 to 256 has a printable representation is nothing to do with the language. This is what people refer to as a "binary string", "byte array", etc.

Binary strings should not be presented as though they are text, and they certainly should not be copy/pasted because, as you've experienced, the non-printable portions tend not to survive, not to mention the high likelihood of encoding mismatch issues for ordinal values above 127, aka "non-ASCII" or "non-7bit-safe".

If you want to represent these values as text for whatever reason you will need to apply a string-safe encoding scheme as Topaco has suggested. Eg:

$in_arr = [0, 1, 22, 33, 44, 55, 66, 77, 88, 99, 100];
$str = implode('', array_map('chr', $in_arr));

var_dump(
    bin2hex($str),
    base64_encode($str)
);

Output:

string(22) "000116212c37424d586364"
string(16) "AAEWISw3Qk1YY2Q="

huangapple
  • 本文由 发表于 2023年6月25日 23:23:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定