//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Confidential Trade Secret.
// Copyright 1987-2010 Data Access Corporation, Miami FL, USA
// All Rights reserved
// DataFlex is a registered trademark of Data Access Corporation.
//
// Module:
//  cHexHandler.Pkg
//
// Purpose:
//  Defines functions for hexadecimal operations
//
// Author:
//  Vincent Oorsprong
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Use VDFbase.pkg

Class cHexHandler is a cObject
    Function IsHex String sHex Returns Boolean
        Integer iCounter iLength
        String sChar

        Move (Uppercase (sHex)) to sHex
        Move (Length (sHex)) to iLength
        
        For iCounter from 1 to iLength
            Move (Mid (sHex, 1, iCounter)) to sChar
            If (Pos (sChar, "0123456789ABCDEF") = 0) Begin
                Function_Return False
            End
        Loop

        Function_Return True
    End_Function

    Function CharToHex Integer iChar Returns String
        String sHex

        Move (Uppercase (iChar)) to iChar
        Move (Mid ("0123456789ABCDEF", 1, Integer (iChar / 16 + 1))) to sHex
        Move (sHex - Mid ("0123456789ABCDEF", 1, Mod (iChar, 16) + 1)) to sHex
        
        Function_Return sHex
    End_Function

    Function HexToChar String sHex Returns Integer
        Integer iCounter iChar iValue iLength iMax
        String sChar
        
        Move (Uppercase (sHex)) to sHex
        Move (Length (sHex)) to iLength
        Move (iLength - 1) to iMax

        For iCounter from 0 to iMax
            Move (Mid (sHex, 1, iLength - iCounter)) to sChar
            If (Pos (sChar, "ABCDEF") <> 0) Begin
                Move (Ascii (sChar) - 55) to iChar
            End
            Else Begin
                Move (Integer (sChar)) to iChar
            End
            If (iCounter = 0) Begin
                Move iChar to iValue
            End
            Else Begin
                Move (iValue + (iChar * (iCounter * 16))) to iValue
            End
        Loop

        Function_Return iValue
    End_Function

    Function StrToHex String sString Returns String
        Integer iCounter iLength iChar
        String sHex sChar sHexChar

        Move (Length (sString)) to iLength

        For iCounter from 1 to iLength
            Move (Mid (sString, 1, iCounter)) to sChar
            Move (Ascii (sChar)) to iChar
            Get CharToHex iChar to sHexChar
            Move (sHex + sHexChar) to sHex
        Loop

        Function_Return sHex
    End_Function

    Function HexToStr String sHex Returns String
        Integer iCounter iChar iLength
        String sString sHexValue sChar

        Move (Length (sHex) / 2) to iLength

        For iCounter from 1 to iLength
            Move (Mid (sHex, 2, iCounter * 2 - 1)) to sHexValue
            Get HexToChar sHexValue to iChar
            Move (Character (iChar)) to sChar
            Move (sString + sChar) to sString
        Loop

        Function_Return sString
    End_Function

    Function DecToHex Integer iDec Returns String
        String sHex

        Move "" to sHex
        Repeat
            Move (Mid ("0123456789ABCDEF", 1, ((iDec iand |CI$0F) + 1)) + sHex) to sHex
            Move (iDec / |CI$10) to iDec
        Until (iDec = 0)

        Function_Return sHex
    End_Function

    Function HexToDec String sHex Returns Integer
        Integer iValue

        Move ('$' + Trim (sHex)) to iValue

        Function_Return iValue
    End_Function
End_Class