Problem solve Get help with specific problems with your technologies, process and projects.

Converting number to HEX value, part 2

Is there a piece of PL/SQL or function or group of functions where I can take a number between 1 and 1234567 and convert it into a HEX value?

Script continued from part 1.

    str := upper(str);  /* a-f ==> A-F */ 
    /* determine valid characters for given radix */ 
    validChars := substr('0123456789ABCDEF', 1, radix); 
    valid := 1; 
    len := length(str); 
    i := 1; 
    while (valid !=0) loop 
      valid := instr(validChars, substr(str, i, 1)); 
      i := i + 1; 
    end loop; 
    if (valid=0) then 
      retval := false; 
      i := to_number('invalid number');  /* Forces ORA-6502. */ 
      retval := true; 
    end if; 
    return retval; 
  end isValidNumStr; 
  /* This function converts a number into a string in given radix. 
     Only non-negative integer should be passed as the argument num, and 
     radix must be a positive integer in [1, 16]. 
     Otherwise, 'ORA-6502: PL/SQL: numeric or value error' is raised. 
  function toRadixString(num in number, radix in number) return varchar2 as 
    dividend number; 
    divisor number; 
    remainder number(2); 
    numStr varchar2(2000); 
    /* NULL NUMBER -> NULL hex string */ 
    if(num is null) then 
      return null; 
    elsif (num=0) then  /* special case */ 
      return '0'; 
    end if; 
    /* invalid number or radix; force ORA-6502: PL/SQL: numeric or value err 
    if (num<0) or (num!=trunc(num)) or 
       (radix<2) or (radix>16) or (radix!=trunc(radix)) then 
      numStr := to_char(to_number('invalid number'));  /* Forces ORA-6502. */ 
      return numStr; 
    end if; 
    dividend := num; 
    numStr := '';  /* start with a null string */ 
    /* the actual conversion loop */ 
    while(dividend != 0) loop 
      remainder := mod(dividend, radix); 
      numStr := digitToString(remainder) || numStr; 
      dividend := trunc(dividend / radix); 
    end loop; 
    return numStr; 
  end toRadixString; 
  function toBinaryString(num in number) return varchar2 as 
    return toRadixString(num, 2); 
  end toBinaryString; 
  function toHexString(num in number) return varchar2 as 
    return toRadixString(num, 16); 
  end toHexString; 
  function toOctalString(num in number) return varchar2 as 
    return toRadixString(num, 8); 
  end toOctalString; 
  /* The parseInt() function is equivalent to TO_NUMBER() when called 
     without a radix argument.  This is consistent with what Java does. 
  function parseInt(s in varchar2) return number as 
    return to_number(s); 
  end parseInt; 
  /* Converts a string in given radix to a number */ 
  function parseInt(s in varchar2, radix in number) return number as 
    str varchar2(2000); 
    len number; 
    decimalNumber number; 
    /* NULL hex string -> NULL NUMBER */ 
    if(s is null) then 
      return null; 
    end if; 
    /* Because isValidNumStr() expects a IN OUT parameter, must use an 
       intermediate variable str.  str will be converted to uppercase 
       inside isValidNumStr(). 
    str := s; 
    if (isValidNumStr(str, radix) = false) then 
      return -1;  /* Never executes because isValidNumStr forced ORA-6502. */ 
    end if; 
    len := length(str); 
    decimalNumber := 0; 
    /* the actual conversion loop */ 
    for i in 1..len loop 
      decimalNumber := decimalNumber*radix + digitToDecimal(substr(str, i, 
    end loop; 
    return decimalNumber; 
  end parseInt; 
end Lang_Integer; 
grant execute on Lang_Integer to public; 

Dig Deeper on Using Oracle PL-SQL

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.