PHP输出十六进制字符串“按原样”

时间:2022-05-08 15:16:30

I am having a weird problem and have hit a road block.

我有一个奇怪的问题,并遇到了障碍。

Background:

背景:

I am working with MSSQL database.

我正在使用MSSQL数据库。

The table that I am querying has a "TIMESTAMP" field.

我查询的表有一个“TIMESTAMP”字段。

Problem:

问题:

When I try to output the data retrieved from this column, I get weird output.

当我尝试输出从该列检索的数据时,我得到了奇怪的输出。

For example, suppose I have the following data saved in the field: 0x000000000ADDAB2F

例如,假设我在字段中保存了以下数据:0x000000000ADDAB2F

PHP will output it as: ������

PHP将输出为:

Perhaps it is happening because PHP is trying to convert this string to a number or something.

也许它正在发生,因为PHP正在尝试将此字符串转换为数字或其他内容。

What I want to do is output the string "AS IS" (i.e I dont want PHP to process it at all, just output the raw data).

我想要做的是输出字符串“AS IS”(即我根本不希望PHP处理它,只输出原始数据)。

I am unable to find a solution that will stop PHP from processing this data.

我无法找到阻止PHP处理这些数据的解决方案。

I am using mssql_* functions to query the data.

我正在使用mssql_ *函数来查询数据。

What I have tried

我试过的

As I dont even have a clue of why is it happening at all, I tried to implement whatever I could find on the Google. So far I have tried to convert the number back to its original form.

由于我甚至不知道为什么会发生这种情况,我试图实现我在谷歌上找到的任何东西。到目前为止,我已尝试将数字转换回原始形式。

So I have used utf8_decode, mb_encode_numericentity, mb_decode_numericentity, base_convert, unpack and dechex

所以我使用了utf8_decode,mb_encode_numericentity,mb_decode_numericentity,base_convert,unpack和dechex

None of them do what I am trying to do.

他们都不做我想做的事。

If you are going to suggest converting the TIMESTAMP value to NVARCHAR or something in the SQL Query itself, then please note that this is not possible.

如果您打算建议将TIMESTAMP值转换为NVARCHAR或SQL Query本身中的某些内容,请注意这是不可能的。

Because I am creating a simple generic query form, where a user will type MSSQL queries in a textarea and will get the results returned in the browser.

因为我正在创建一个简单的通用查询表单,用户将在textarea中键入MSSQL查询,并将在浏览器中返回结果。

So he can type SELECT * FROM table and he should get all columns in the output.

所以他可以键入SELECT * FROM table,他应该输出输出中的所有列。

So it is not possible to fix the problem in the query itself, I have to do it in PHP itself.

因此无法在查询本身中解决问题,我必须在PHP本身中完成。

Any help?

有帮助吗?

3 个解决方案

#1


2  

Use bin2hex. What PHP is printing is the actual binary value. The 0x.... that you see is a hex representation of the binary data.

使用bin2hex。 PHP打印的是实际的二进制值。您看到的0x ....是二进制数据的十六进制表示。

#2


2  

An SQLServer TIMESTAMP value is a number but, for some reason, PHP stores it as string. A string is just a sequence of bytes. When these bytes are interpreted as characters, some of them might not be printable.

SQLServer TIMESTAMP值是一个数字,但由于某种原因,PHP将其存储为字符串。字符串只是一个字节序列。当这些字节被解释为字符时,其中一些可能无法打印。

There are several ways to convert the sequence of bytes in something you can use further (for printing, computation etc).

有几种方法可以转换您可以进一步使用的字节序列(用于打印,计算等)。

If you need the value as number then you can use unpack().

如果您需要值作为数字,那么您可以使用unpack()。

Assuming the raw value you get from the database is stored in the $rawData variable, the following code should display 0x000000000ADDAB2F:

假设从数据库获得的原始值存储在$ rawData变量中,以下代码应显示0x000000000ADDAB2F:

$values = unpack('J', $rawData);
printf('0x%016X', $value[1]);

#3


0  

If you want to print/show any hex value from a variable, You can use the code below:

如果要打印/显示变量的任何十六进制值,可以使用以下代码:

$hex_value=0x000000000ADDAB2F;
echo sprintf("0x%'016X", $hex_value);

#1


2  

Use bin2hex. What PHP is printing is the actual binary value. The 0x.... that you see is a hex representation of the binary data.

使用bin2hex。 PHP打印的是实际的二进制值。您看到的0x ....是二进制数据的十六进制表示。

#2


2  

An SQLServer TIMESTAMP value is a number but, for some reason, PHP stores it as string. A string is just a sequence of bytes. When these bytes are interpreted as characters, some of them might not be printable.

SQLServer TIMESTAMP值是一个数字,但由于某种原因,PHP将其存储为字符串。字符串只是一个字节序列。当这些字节被解释为字符时,其中一些可能无法打印。

There are several ways to convert the sequence of bytes in something you can use further (for printing, computation etc).

有几种方法可以转换您可以进一步使用的字节序列(用于打印,计算等)。

If you need the value as number then you can use unpack().

如果您需要值作为数字,那么您可以使用unpack()。

Assuming the raw value you get from the database is stored in the $rawData variable, the following code should display 0x000000000ADDAB2F:

假设从数据库获得的原始值存储在$ rawData变量中,以下代码应显示0x000000000ADDAB2F:

$values = unpack('J', $rawData);
printf('0x%016X', $value[1]);

#3


0  

If you want to print/show any hex value from a variable, You can use the code below:

如果要打印/显示变量的任何十六进制值,可以使用以下代码:

$hex_value=0x000000000ADDAB2F;
echo sprintf("0x%'016X", $hex_value);

相关文章