Re: BIN() - Mailing list pgsql-hackers

From Tino Wildenhain
Subject Re: BIN()
Date
Msg-id 1133332956.5734.34.camel@Andrea.peacock.de
Whole thread Raw
In response to BIN()  (Christopher Kings-Lynne <chriskl@familyhealth.com.au>)
Responses Re: BIN()
List pgsql-hackers
Am Mittwoch, den 30.11.2005, 10:15 +0800 schrieb Christopher
Kings-Lynne:
> Hi guys,
> 
> How would I go about implementing MySQL's BIN() function easily in PL/SQL.
> 
> mysql> SELECT BIN(12);
>          -> '1100'
> 
> Basically it converts a bigint to a string containing 1's and 0's.
> 
> I've tried messing about with bit() types, but those types lack casts to 
> text, etc.  And they are left padded with many zeros.

In python, I usually go like this:

def trans(value,base="01"):   value,r=divmod(value,len(base))   if value: return trans(value,base)+base[r]   return
base[r]

While base above has a default of "01" which
let it render binary:

trans(10)
-> '1010'

you can use any base you want:

trans(10,"0123456789abcdef")
-> 'a'

and so on.

If you want it easy, just put above code
into a pl/python function.

Or rewrite it in C or pl/pgsql or something.





pgsql-hackers by date:

Previous
From: Simon Riggs
Date:
Subject: Re: Using multi-row technique with COPY
Next
From: Michael Fuhr
Date:
Subject: Re: BIN()