Thread: Is there a function for Converting a Decimal into BINARY ?

Is there a function for Converting a Decimal into BINARY ?

From
"Ow Mun Heng"
Date:
Hi Guys,

Searching the net didn't give me much clues as to how to convert a Decimal
number into BINARY.


Eg:
I have a datatype in the DB which needs to be converted.

DEC = 192
BINARY = 11000000

DEC = 197
BINARY = 11000101

Which I then need to break down into pairs to do calculations on

11 : 00 : 00 : 00
11 : 00 : 01 : 01

Some of the solutions I've seen on the Internet is based on VB and mainly
userland apps, I would like to do the conversion within PG itself.

Thanks

Re: Is there a function for Converting a Decimal into BINARY ?

From
John R Pierce
Date:
Ow Mun Heng wrote:
> Hi Guys,
>
> Searching the net didn't give me much clues as to how to convert a Decimal
> number into BINARY.
>

well, a decimal number is a fixed point number stored in a modified BCD
format, which optionally can contain a decimal fractional component.

you likely would want to convert it to a INTEGER or BIGINT first,  then
cats it to BIT(n) where N is the number of bits you want.

Re: Is there a function for Converting a Decimal into BINARY ?

From
"Harvey, Allan AC"
Date:
> Eg:
> I have a datatype in the DB which needs to be converted.
>
> DEC = 192
> BINARY = 11000000

How about this....

gwmdb=> select 192::bit(16);
       bit
------------------
 0000000011000000
(1 row)

Hope that helps

Allan


The material contained in this email may be confidential, privileged or copyrighted. If you are not the intended
recipient,use, disclosure or copying of this information is prohibited. If you have received this document in error,
pleaseadvise the sender and delete the document. Neither OneSteel nor the sender accept responsibility for any viruses
containedin this email or any attachments.