Overblog
Editer l'article Suivre ce blog Administration + Créer mon blog

Search

Free tool

Look and Feel Project

10 janvier 2006 2 10 /01 /janvier /2006 09:35

I have just read the Jeff Moss article concerning Hex to decimal conversion and vice versa.

In this article we can find some great conversions routines.

This is one that allows to convert hexadecimal numbers greatest than 255 to a decimal format:

CREATE OR REPLACE FUNCTION hex_to_dec ( pc$hex IN VARCHAR2 )
RETURN PLS_INTEGER
IS
  hexch  VARCHAR2(40) := UPPER( pc$hex) ;
  len PLS_INTEGER := LENGTH( hexch) ;
  res PLS_INTEGER := 0 ;
  pos PLS_INTEGER := 0 ;
  val PLS_INTEGER ;
  car VARCHAR2(1) ;
BEGIN

  FOR i IN REVERSE 1..len LOOP

    car := SUBSTR( hexch, i , 1 ) ;

 IF ASCII(car) > 57 THEN
       val := ASCII(car) - 55 ;
    ELSE
       val := ASCII(car) - 48 ;
    END IF ;

    res := res + (val * ( POWER(16,pos) ) ) ;

 pos := pos + 1 ;
 
  END LOOP ;

  RETURN( res ) ;
 
END;
/

SQL> SELECT hex_to_dec('FFFF') FROM dual
  2  /

HEX_TO_DEC('FFFF')
------------------
             65535

SQL>

Francois

Partager cet article
Repost0

commentaires

D
<br /> <br /> if this can help, here is an hex to decimal converter to convert your value<br /> right away :)<br /> <br /> <br /> David<br /> <br /> <br /> <br />
Répondre
R
Why not just the TO_NUMBER built-in function as follows?FUNCTION hex_to_dec(pc$hex IN VARCHAR2) RETURN PLS_INTEGER ISBEGIN  RETURN TO_NUMBER(pc$hex, 'XXXXXXXX');END;
Répondre
F
Well, i simply ignored this format ! Thanks for the info ;-)