Overblog Suivre ce blog
Editer l'article 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

Repost 0
Francois Degrelle - dans Oracle PL-SQL
commenter cet article

commentaires

david 08/04/2010 09:21



if this can help, here is an hex to decimal converter to convert your value
right away :)


David



Robert Pang 10/01/2006 19:36

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;

Francois Degrelle 11/01/2006 09:25

Well, i simply ignored this format ! Thanks for the info ;-)