Subject | Re: [firebird-support] UUID (octets) to something readable |
---|---|
Author | Fabiano Bonin |
Post date | 2008-11-09T18:53:52Z |
Ok, for now i will solve my problem using PSQL code, but i would like to use
UDF's for this purpose.
I think my problem could be solved with 2 general purpose UDF's with these
signatures:
octets2hex(blob): blob
hex2octets(blob): blob
Is it possible to code such UDF's?
Is there something ready out there?
I would like to use blobs to be able to work with large binary data also. Is
it a good/possible choice?
Is there some tutorial or guide do develop a multiplataform UDF using
pascal?
Regards,
Fabiano
PS: For those who have the same problem to convert to and from octets uuids
and hex strings, here are 2 stored procedures based on Ivan original wich do
the job:
set term !! ;
create or alter procedure uuid2hex (
uuid char(16) character set octets)
returns(
result varchar(32))
as
declare variable i integer;
declare variable c integer;
begin
result = '';
i = 1;
while (i <= octet_length(uuid)) do
begin
c = ascii_val(substring(uuid from i for 1));
result = result || substring('0123456789ABCDEF' from bin_shr(c, 4) + 1
for 1) || substring('0123456789ABCDEF' from bin_and(c, 15) + 1 for 1);
i = i + 1;
end
suspend;
end !!
create or alter procedure hex2uuid (
hex varchar(32))
returns(
result char(16) character set octets)
as
declare variable i integer;
declare variable temp varchar(16) character set octets;
begin
hex = upper(hex);
temp = '';
i = 1;
while (i <= char_length(hex)) do
begin
temp = temp || ascii_char((position(substring(hex from i for 1),
'0123456789ABCDEF') - 1) * 16 + (position(substring(hex from i + 1 for 1),
'0123456789ABCDEF') - 1));
i = i + 2;
end
result = temp;
suspend;
end !!
set term ; !!
UDF's for this purpose.
I think my problem could be solved with 2 general purpose UDF's with these
signatures:
octets2hex(blob): blob
hex2octets(blob): blob
Is it possible to code such UDF's?
Is there something ready out there?
I would like to use blobs to be able to work with large binary data also. Is
it a good/possible choice?
Is there some tutorial or guide do develop a multiplataform UDF using
pascal?
Regards,
Fabiano
PS: For those who have the same problem to convert to and from octets uuids
and hex strings, here are 2 stored procedures based on Ivan original wich do
the job:
set term !! ;
create or alter procedure uuid2hex (
uuid char(16) character set octets)
returns(
result varchar(32))
as
declare variable i integer;
declare variable c integer;
begin
result = '';
i = 1;
while (i <= octet_length(uuid)) do
begin
c = ascii_val(substring(uuid from i for 1));
result = result || substring('0123456789ABCDEF' from bin_shr(c, 4) + 1
for 1) || substring('0123456789ABCDEF' from bin_and(c, 15) + 1 for 1);
i = i + 1;
end
suspend;
end !!
create or alter procedure hex2uuid (
hex varchar(32))
returns(
result char(16) character set octets)
as
declare variable i integer;
declare variable temp varchar(16) character set octets;
begin
hex = upper(hex);
temp = '';
i = 1;
while (i <= char_length(hex)) do
begin
temp = temp || ascii_char((position(substring(hex from i for 1),
'0123456789ABCDEF') - 1) * 16 + (position(substring(hex from i + 1 for 1),
'0123456789ABCDEF') - 1));
i = i + 2;
end
result = temp;
suspend;
end !!
set term ; !!
On Sun, Nov 9, 2008 at 10:29 AM, Ivan Prenosil <Ivan.Prenosil@...>wrote:
> > Is there some native way in FB 2.1 to convert from a UUID or any octet
> > field to a human readable or url friendly format (i.e. hexadecimal or
> > base64)?
>
> No. But you can
> - format value on client, or
> - write UDF to do it, or
> - convert using PSQL code, something like
>
> EXECUTE BLOCK RETURNS(UUID VARCHAR(32)) AS
> DECLARE VARIABLE U CHAR(16) CHARACTER SET OCTETS;
> DECLARE VARIABLE C INTEGER;
> DECLARE VARIABLE I INTEGER;
> BEGIN
> U = GEN_UUID();
> UUID = '';
> I = 1;
> WHILE (I <= OCTET_LENGTH(U)) DO BEGIN
> C = ASCII_VAL(SUBSTRING(U FROM I FOR 1));
> UUID = UUID || SUBSTRING('0123456789ABCDEF' FROM BIN_SHR(C, 4)+1 FOR 1)
> || SUBSTRING('0123456789ABCDEF' FROM BIN_AND(C, 15)+1 FOR
> 1);
> I = I + 1;
> END
> SUSPEND;
> END
>
> Ivan
> http://www.volny.cz/iprenosil/interbase/
>
>
> ------------------------------------
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> Visit http://www.firebirdsql.org and click the Resources item
> on the main (top) menu. Try Knowledgebase and FAQ links !
>
> Also search the knowledgebases at http://www.ibphoenix.com
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> Yahoo! Groups Links
>
>
>
>
[Non-text portions of this message have been removed]