Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert number to binary format

MohammedImranJul 26 2017 — edited Jul 26 2017

Hi,

I have a function that converts number to binary format which looks like

FUNCTION dec2bin (N in number, bits in number) RETURN varchar2 IS

  binval varchar2(64);

  N2 number := N;

BEGIN

  while ( N2 > 0 ) loop

  binval := mod(N2, 2) || binval;

  N2 := trunc( N2 / 2 );

  end loop;

     binval := LPad(binval,bits,'0');

  return binval;

END dec2bin;

I want to alter this so it works correctly for negative values as well. Please suggest.

Thanks,

Imran.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 23 2017
Added on Jul 26 2017
22 comments
7,231 views