Re: BIN()

From: Tino Wildenhain <tino(at)wildenhain(dot)de>
To: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
Cc: Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: BIN()
Date: 2005-11-30 06:42:36
Message-ID: 1133332956.5734.34.camel@Andrea.peacock.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

  • BIN() at 2005-11-30 02:15:07 from Christopher Kings-Lynne

Responses

  • Re: BIN() at 2005-11-30 07:03:11 from Michael Fuhr

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Fuhr 2005-11-30 07:03:11 Re: BIN()
Previous Message Simon Riggs 2005-11-30 06:35:23 Re: Using multi-row technique with COPY