pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Converting integer to binary
5+ messages / 3 participants
[nested] [flat]

* Converting integer to binary
@ 2004-06-10 13:52 Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
  2004-06-10 18:27 ` Re: Converting integer to binary Bruno Wolff III <bruno@wolff.to>
  0 siblings, 1 reply; 5+ messages in thread

From: Stephen Quinney @ 2004-06-10 13:52 UTC (permalink / raw)
  To: pgsql-sql


I have searched around but I cannot see any standard way in PostgreSQL
to convert from an integer into a binary representation.

i.e. I want to do:

16 ==> 10000
32 ==> 100000
64 ==> 1000000
96 ==> 1100000

etc..

Now I have an algorithm to do it so I could write an SQL function, I
guess. If there's a standard way to do it though that would be quite nice.

Thanks in advance,

Stephen Quinney




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Converting integer to binary
  2004-06-10 13:52 Converting integer to binary Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
@ 2004-06-10 18:27 ` Bruno Wolff III <bruno@wolff.to>
  2004-06-10 20:24   ` Re: Converting integer to binary Chris Gamache <cgg007@yahoo.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Bruno Wolff III @ 2004-06-10 18:27 UTC (permalink / raw)
  To: Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>; +Cc: pgsql-sql

On Thu, Jun 10, 2004 at 14:52:41 +0100,
  Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk> wrote:
> 
> I have searched around but I cannot see any standard way in PostgreSQL
> to convert from an integer into a binary representation.
> 
> Now I have an algorithm to do it so I could write an SQL function, I
> guess. If there's a standard way to do it though that would be quite nice.

There doesn't seem to currently be a function that does this. to_char
would be the logical place since that is what is used to convert various
numeric types to strings with a decimal representation.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Converting integer to binary
  2004-06-10 13:52 Converting integer to binary Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
  2004-06-10 18:27 ` Re: Converting integer to binary Bruno Wolff III <bruno@wolff.to>
@ 2004-06-10 20:24   ` Chris Gamache <cgg007@yahoo.com>
  2004-06-10 22:27     ` Re: Converting integer to binary Bruno Wolff III <bruno@wolff.to>
  0 siblings, 1 reply; 5+ messages in thread

From: Chris Gamache @ 2004-06-10 20:24 UTC (permalink / raw)
  To: Bruno Wolff III <bruno@wolff.to>; Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>; +Cc: pgsql-sql


Once upon a time in PostgreSQL there was a function : bitfromint4 ... Any idea
where it has disappeared to?

You can do

# select B'10101101'::int4;
 int4
------
  173
(1 row)

but you want to go

# select 173::varbit;

which is what bitfromint4 used to do.

CG

--- Bruno Wolff III <bruno@wolff.to> wrote:
> On Thu, Jun 10, 2004 at 14:52:41 +0100,
>   Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk> wrote:
> > 
> > I have searched around but I cannot see any standard way in PostgreSQL
> > to convert from an integer into a binary representation.
> > 
> > Now I have an algorithm to do it so I could write an SQL function, I
> > guess. If there's a standard way to do it though that would be quite nice.
> 
> There doesn't seem to currently be a function that does this. to_char
> would be the logical place since that is what is used to convert various
> numeric types to strings with a decimal representation.
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster



	
		
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Converting integer to binary
  2004-06-10 13:52 Converting integer to binary Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
  2004-06-10 18:27 ` Re: Converting integer to binary Bruno Wolff III <bruno@wolff.to>
  2004-06-10 20:24   ` Re: Converting integer to binary Chris Gamache <cgg007@yahoo.com>
@ 2004-06-10 22:27     ` Bruno Wolff III <bruno@wolff.to>
  2004-06-11 07:54       ` Re: Converting integer to binary Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
  0 siblings, 1 reply; 5+ messages in thread

From: Bruno Wolff III @ 2004-06-10 22:27 UTC (permalink / raw)
  To: Chris Gamache <cgg007@yahoo.com>; +Cc: Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>; pgsql-sql

On Thu, Jun 10, 2004 at 13:24:15 -0700,
  Chris Gamache <cgg007@yahoo.com> wrote:

Following up on the cast to bit idea, he could do something like
casting to bit(32). I don't think there is an easy way to get this
cast to string, so it may not completely solve his problem, depending
on what he was going to do with the binary representation. On teh other
hand, for some things a bit field might be more useful than a string.

area=> select 6::bit(32);
               bit
----------------------------------
 00000000000000000000000000000110
(1 row)




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Converting integer to binary
  2004-06-10 13:52 Converting integer to binary Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
  2004-06-10 18:27 ` Re: Converting integer to binary Bruno Wolff III <bruno@wolff.to>
  2004-06-10 20:24   ` Re: Converting integer to binary Chris Gamache <cgg007@yahoo.com>
  2004-06-10 22:27     ` Re: Converting integer to binary Bruno Wolff III <bruno@wolff.to>
@ 2004-06-11 07:54       ` Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
  0 siblings, 0 replies; 5+ messages in thread

From: Stephen Quinney @ 2004-06-11 07:54 UTC (permalink / raw)
  To: Chris Gamache <cgg007@yahoo.com>; +Cc: pgsql-sql

On Thu, Jun 10, 2004 at 05:27:31PM -0500, Bruno Wolff III wrote:
> On Thu, Jun 10, 2004 at 13:24:15 -0700,
>   Chris Gamache <cgg007@yahoo.com> wrote:
> 
> Following up on the cast to bit idea, he could do something like
> casting to bit(32). I don't think there is an easy way to get this
> cast to string, so it may not completely solve his problem, depending
> on what he was going to do with the binary representation. On teh other
> hand, for some things a bit field might be more useful than a string.
> 
> area=> select 6::bit(32);
>                bit
> ----------------------------------
>  00000000000000000000000000000110
> (1 row)

That would probably work for me in this case as I am interfacing to
postgresql via the Perl DBI layer. With perl reading out that field
would give me a string representation.

Thanks for the tips,

Stephen Quinney

 



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2004-06-11 07:54 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-06-10 13:52 Converting integer to binary Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
2004-06-10 18:27 ` Bruno Wolff III <bruno@wolff.to>
2004-06-10 20:24   ` Chris Gamache <cgg007@yahoo.com>
2004-06-10 22:27     ` Bruno Wolff III <bruno@wolff.to>
2004-06-11 07:54       ` Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox