Is there a function for Converting a Decimal into BINARY ?

Started by Ow Mun Hengover 16 years ago3 messagesgeneral
Jump to latest
#1Ow Mun Heng
Ow.Mun.Heng@wdc.com

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

#2Harvey, Allan AC
HarveyA@OneSteel.com
In reply to: Ow Mun Heng (#1)
Re: Is there a function for Converting a Decimal into BINARY ?

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, please advise the sender and delete the document. Neither OneSteel nor the sender accept responsibility for any viruses contained in this email or any attachments.

#3John R Pierce
pierce@hogranch.com
In reply to: Ow Mun Heng (#1)
Re: Is there a function for Converting a Decimal into BINARY ?

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.