Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

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: Wed, 30 Nov 2005 07:42:36 +0100
  • Message-id: <1133332956(dot)5734(dot)34(dot)camel(at)Andrea(dot)peacock(dot)de>

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.





  • References:
    • BIN()
      • From: Christopher Kings-Lynne

Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group