2

I have a C# .NET 3.5 application using the ADO.NET Driver for MySQL (Connector/NET). It stores a Guid.NewGuid() in the MySQL database as a BINARY(16).

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.

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?

PaulH
  • 7,759
  • 8
  • 66
  • 143
  • try tu use [unpack](http://www.php.net/manual/pl/function.unpack.php) function – Kuba Feb 29 '12 at 22:01
  • possible duplicate of [String to byte array in php](http://stackoverflow.com/questions/885597/string-to-byte-array-in-php) - check out karim79 suggestion there. – Alexei Levenkov Feb 29 '12 at 22:04
  • unpack gets me to a binary representation of the guid in a string, but is not the same as the guid string. – PaulH Feb 29 '12 at 22:10
  • @alexi Levenkov - karim79's suggestion does not yield the result I'm looking for – PaulH Feb 29 '12 at 22:17

1 Answers1

0

The problem was that the field was BINARY(16), but the My SQL connector was trying to store a CHAR(36) in there. (They changed the way GUIDs were formatted in v6.1.1) So, the last 20 bytes of my GUID were being silently truncated. (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. Alternatively, I could have use the oldguids=true element in my connection string.

PaulH
  • 7,759
  • 8
  • 66
  • 143