Home Page for the TeradataForum
 

Archives of the TeradataForum

Message Posted: Tue, 27 May 2008 @ 09:32:36 GMT


     
  <Prev Next>   <<First <Prev Next> Last>>  


Subj:   Re: Hexadecimal to binary conversion
 
From:   Stieger, Etienne E

Hi Shihas,

You wrote:

  Appreciate if someone has teradata code to convert hexadecimal to binary representation  


Solution:

Here is a shell script that can do the required "numeric base conversion" for you. If required to use within Teradata, you could possibly covert the logic to a stored procedure amd/or macro. The shell script defines some functions, then runs it's main logic to make use of the functions. It basically converts any input base value to decimal (if not already specified as decimal), before converting to the final output base number system.

If you do manage to convert this to a macro or stored procedure, please share the solution back with the forum as a courtesy (to save others from also having to do the same work).

     #!/bin/ksh
     ###########################################################
     ###                                                       #
     ###                   baseconvert.ksh                     #
     ###      Convert a value from one base to another         #
     ###                                                       #
     ### Created  : Etienne Stieger          : 2008-05-13      #
     ### Modified : Etienne Stieger          : 2008-05-13      #
     ###########################################################

     ### For the input value, input base and output base this script
     ### converts the input value to an output value of specified
     ### base.

     ### INPUT ARGUMENTS:
     ### $1 is input value to convert, such as 1011 (binary), 10 (decimal),
     1F (hexadecimal) or 10 (Octal)
     ### $2 is input numeric base - such as 2(binary), 8(octal), 10(decimal),
     16(hex), etc
     ### $3 is output numeric base - such as 2(binary), 8(octal),
     10(decimal), 16(hex), etc
     ### (max is base 36)

     toChar() {
          ### converts a single decimal value to a base character
          set -A array_01 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P
     Q R S T U V W X Y Z
          echo "${array_01[${1}]}\c"
     }

     fromChar() {
          ### converts a base character back to a decimal value
          var_lookp_chr='echo "${1}"|tr [:lower:] [:upper:]'
          var_array_01='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
          var_pos='/usr/ucb/expr index "${var_array_01}" "${1}"'
          ret_pos='expr ${var_pos} - 1'
          echo "${ret_pos}"
     }

     fromBase10() {
          ### converts a complete decimal value to any other base value string
          var_d=${1}
          var_r='expr ${var_d} % ${3}'
          var_result=''
          var_chk='expr ${var_d} - ${var_r}'
          if [ ${var_chk} -eq 0 ]
            then
              var_result='toChar ${var_r}'
          else
              var_d_next='expr \( ${var_d} - ${var_r} \) / ${3}'
              var_result='fromBase10 ${var_d_next} ${2} ${3}''toChar ${var_r}'
          fi
          echo "${var_result}"
     }

     toBase10() {
          ### converts any base value string to decimal (base 10)
          var_inp_len='/usr/ucb/expr length "${1}"'
          const_inp_len='/usr/ucb/expr length "${1}"'
          var_factor=1
          var_cum_dec=0
          while [ ${var_inp_len} -gt 0 ]
            do
              if [ ${var_inp_len} -eq ${const_inp_len} ]
                then
                  var_base_chr='echo "${1}"|cut
     -c${const_inp_len}-${const_inp_len}'
                  var_dec_val='fromChar "${var_base_chr}"'
                  var_cum_dec='expr ${var_cum_dec} + ${var_dec_val}'
              else
                  var_base_chr='echo "${1}"|cut
     -c${var_inp_len}-${var_inp_len}'
                  var_tmp_val='fromChar "${var_base_chr}"'
                  var_dec_val='expr ${var_tmp_val} \* ${var_factor}'
                  var_cum_dec='expr ${var_cum_dec} + ${var_dec_val}'
              fi
              var_factor='expr ${var_factor} \* ${2}'
              var_inp_len='expr ${var_inp_len} - 1'
          done
          echo "${var_cum_dec}"
     }

     ### Main controlling logic (entry-point for this shell script)
     if [ "@${2}" = "@10" ]
        then
          if [ "@${3}" = "@10" ]
            then
              echo "${1}"
          else
              fromBase10 ${1} ${2} ${3}
          fi
     else
          var_dec_val='toBase10 ${1} ${2} ${3}'
          if [ "@${2}" = "@${3}" ]
            then
              echo "${1}"
          else
              fromBase10 ${var_dec_val} ${2} ${3}
          fi
     fi

Kind regards

Etienne Stieger
Analytics and Information Management (AIM/EIW)
Standard Bank of South Africa Ltd



     
  <Prev Next>   <<First <Prev Next> Last>>  
 
 
 
 
 
 
 
 
  
  Top Home Privacy Feedback  
 
 
Copyright for the TeradataForum (TDATA-L), Manta BlueSky    
Copyright 2016 - All Rights Reserved    
Last Modified: 15 Jun 2023