Search code examples
javasqldatabasepostgresqluuid

What is the algorithm used for converting uuid::text in Postgresql?


A table in postgresql where one of columns is uuid as BYTEA is being exported to tsv.

For example uuid was: 625c3430-34d0-43eb-b85d-e3bd1d9f82e1 and exported value is: b\\404\320C\353\270]\343\275\035\237\202\341.

What is the algorithm behind converting uuid::text and for the reverse functionality also, from text to uuid. I have to read this tsv, decode uuid from its text representation and "do additional stuff" depending on decoded uuid.

Are there some libraries for this in Java or do I need to write my own decoder for it? If I have to write my own decoder any hints on algorithm for it are welcome.


Solution

  • Convert your initial uuid to byte array. Check every element of byte array and if the value is:

    0 => "\000"

    39 => "\047"

    or '''' (single quote symbol)

    92 => "\" or "\134" (backslash)

    31 < value < 127 => character from value

    any other is "\xxx" octal value.

    And you have your "text representation" uuid.

    To get uuid from "text representation" just go trough the steps in reverse.