1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

How to convert decimal to hexadecimal

Discussion in 'Other Development Tools' started by Arju, Nov 7, 2008.

  1. Arju

    Arju Forum Expert

    Messages:
    107
    Likes Received:
    5
    Trophy Points:
    240
    Location:
    Bangladesh
    Convert decimal to hexadecimal

    There are several ways to convert decimal number to hexadecimals. Like using function, using complex sql statements. Let's try from with our ideas. I have shown one simple way, That is using TO_CHAR function.

    TO_CHAR function take argument as decimal value and format is specified to xxxxx so that it represent hexadecimal value. Note that less the number of format xxxx than the output of total number of hexadecimal digit will not bring expected result.

    Example:
    Code (Text):
    SQL> select to_char(1233,'XXXXXX') from dual;

    TO_CHAR
    -------
        4D1

    SQL> select to_char(821681268,'XXXXXX') from dual;

    TO_CHAR
    -------
    #######
    Since here total digits in hexadecimal is more than XXXXXX so we did not get desired result.

    Code (Text):
    SQL> select to_char(821681268,'XXXXXXXXXXX') from dual;

    TO_CHAR(8216
    ------------
        30F9DC74

    SQL> select to_char(8,'XXXXXXXXXXX') from dual;

    TO_CHAR(8,'X
    ------------
               8
     
  2. whathappen

    whathappen Guest

    we might create function converter. as
    Code (SQL):

    CREATE OR REPLACE FUNCTION to_base (p_dec IN NUMBER, p_base IN NUMBER)
       RETURN VARCHAR2
    IS
       l_str   VARCHAR2 (255) DEFAULT NULL;
       l_num   NUMBER         DEFAULT p_dec;
       l_hex   VARCHAR2 (16)  DEFAULT ‘0123456789abcdef’;
    BEGIN
       IF (p_dec IS NULL OR p_base IS NULL)
       THEN
          RETURN NULL;
       END IF;

       IF (TRUNC (p_dec) <> p_dec OR p_dec < 0)
       THEN
          RAISE PROGRAM_ERROR;
       END IF;

       LOOP
          l_str := SUBSTR (l_hex, MOD (l_num, p_base) + 1, 1) || l_str;
          l_num := TRUNC (l_num / p_base);
          EXIT WHEN (l_num = 0);
       END LOOP;

       RETURN l_str;
    END to_base;
    /



    CREATE OR REPLACE FUNCTION to_dec (
       p_str         IN   VARCHAR2,
       p_from_base   IN   NUMBER DEFAULT 16
    )
       RETURN NUMBER
    IS
       l_num   NUMBER        DEFAULT 0;
       l_hex   VARCHAR2 (16) DEFAULT ‘0123456789abcdef’;
    BEGIN
       IF (p_str IS NULL OR p_from_base IS NULL)
       THEN
          RETURN NULL;
       END IF;

       FOR i IN 1 .. LENGTH (p_str)
       LOOP
          l_num :=
             l_num * p_from_base + INSTR (l_hex, UPPER (SUBSTR (p_str, i, 1)))
             - 1;
       END LOOP;

       RETURN l_num;
    END to_dec;
    /

    SHOW errors



    CREATE OR REPLACE FUNCTION to_hex (p_dec IN NUMBER)
       RETURN VARCHAR2
    IS
    BEGIN
       RETURN to_base (p_dec, 16);
    END to_hex;
    /

    CREATE OR REPLACE FUNCTION to_bin (p_dec IN NUMBER)
       RETURN VARCHAR2
    IS
    BEGIN
       RETURN to_base (p_dec, 2);
    END to_bin;
    /

    CREATE OR REPLACE FUNCTION to_oct (p_dec IN NUMBER)
       RETURN VARCHAR2
    IS
    BEGIN
       RETURN to_base (p_dec, 8);
    END to_oct;
    /
    Informat at : blogitknowledge.wordpress.com
     
  3. Sadik

    Sadik Community Moderator Forum Guru

    Messages:
    1,906
    Likes Received:
    252
    Trophy Points:
    1,455
    Nice post whathappen, you might want to keep the link in the signature though.