On Sat, 10 Jan 2004, Tim Reilly wrote:

> Below are some transact SQL (MS-SQL Server) statements that I ran in Query
> Analyzer. I'm trying to figure out how it is that a UUID in character format
> translates to the hexidecimal format SQLServer uses.
>
> An example is:
> String format is: B4F00409-CEF8-4822-802C-DEB20704C365
> Hexidecimal format is: 0x0904F0B4F8CE2248802CDEB20704C365
> I see a pattern, but I can't explain it. My first thought was that it's an
> endian problem, but that doesn't seem to pan out. Anyone have any ideas?

It looks odd, doesn't it? But it all makes sense when you see the C/C++
type definition, which looks something like this:

typedef struct _GUID {
    DWORD   Data1;
    WORD    Data2;
    WORD    Data3;
    BYTE    Data4[8];
} GUID;

A DWORD is a 32-bit unsigned value, and a WORD is a 16-bit unsigned value.

The pattern you see is exactly right, and due to the fact that the Data1,
Data2 and Data3 fields have their bytes organised according to the types
specified in the typedef above.

--
Martin Cooper


>
> The pattern I see is the last 2 fields are the same. But the high order
> bytes / half are swapped
> B4F00409-CEF8-4822-802C-DEB20704C365
>   \ /     \/   \/  |||| ||||||||||||
>   / \     /\   /\  |||| ||||||||||||
> 0904F0B4 F8CE 2248 802C DEB20704C365
>
>
> //T-SQL Statements
> DECLARE @tmpuuid uniqueidentifier
> DECLARE @myuuid uniqueidentifier
>
> SET @myuuid = newid()
>
> print 'MyUUID String Format: '
> print Convert(char(36), @myuuid)
> print ''
> print 'MyUUID Binary Format: '
> print Convert(binary(16), @myuuid)
> print ''
> SET @tmpuuid = Convert(binary(16), @myuuid)
> print 'tmpuuid converted back: '
> print @tmpuuid
>
> ==OUTPUT==
> MyUUID String Format:
> B4F00409-CEF8-4822-802C-DEB20704C365
>
> MyUUID Binary Format:
> 0x0904F0B4F8CE2248 802CDEB20704C365
>
> tmpuuid converted back:
> B4F00409-CEF8-4822-802C-DEB20704C365
>
> == Reformatted ==
> MyUUID String Format:
> B4 F0 04 09 - CE F8- 48 22- 802C-DEB20704C365
>
> MyUUID Binary Format:
> 0x 09 04 F0 B4 F8 CE 22 48  802C DEB20704C365
>
> tmpuuid converted back:
> B4F00409-CEF8-4822-802C-DEB20704C365
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to