简体   繁体   English

用PHP编码和解码二进制GUID

[英]encoding and decoding a binary guid in PHP

I have a C# .NET 3.5 application using the ADO.NET Driver for MySQL (Connector/NET). 我有一个使用ADO.NET MySQL驱动程序(连接器/ NET)的C#.NET 3.5应用程序。 It stores a Guid.NewGuid() in the MySQL database as a BINARY(16) . 它将Guid.NewGuid()作为BINARY(16)存储在MySQL数据库中。

I have another application using PHP 5.3.4 that needs to be able to read that binary value as a GUID string and encode a GUID string in the same 16-byte binary value. 我有另一个使用PHP 5.3.4的应用程序,该应用程序需要能够将该二进制值读取为GUID字符串,并以相同的16字节二进制值编码GUID字符串。

As an example I want to be able to convert between these two things: 举例来说,我希望能够在这两件事之间进行转换:

GUID string: E241346C-504F-4BE5-BDF3-1B8274815597
BINARY(16): 65 32 34 31 33 34 36 63 2d 35 30 34 66 2d 34 62

How can I do this in PHP? 如何在PHP中做到这一点?

The problem was that the field was BINARY(16), but the My SQL connector was trying to store a CHAR(36) in there. 问题在于该字段是BINARY(16),但是My SQL连接器试图在其中存储CHAR(36)。 (They changed the way GUIDs were formatted in v6.1.1) So, the last 20 bytes of my GUID were being silently truncated. (它们改变了v6.1.1中GUID格式化的方式)。因此,我的GUID的最后20个字节被无声地截断了。 (I do not know why the connector didn't throw an exception.) (我不知道为什么连接器没有引发异常。)

Once I changed the database to store GUIDs as a CHAR(36), it worked fine. 一旦更改数据库以将GUID存储为CHAR(36),它就可以正常工作。 Alternatively, I could have use the oldguids=true element in my connection string. 或者,我可以在连接字符串中使用oldguids=true元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM