News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Convert STRING ASCII into HEX format

Started by Mapo, Jul 02, 2022, 03:08 PM

Previous topic - Next topic

Mapo

Hi everyone,
from a keyboard I receive the ascii characters of hexadecimal data
to convert a string of ascii characters to an array in hexadecimal with control of error I use this routine, I ask if anyone has a simpler way
Thanks in advance

example
Dati_PC = "45525241"
Ciclo = 8
 
For Counter1 = 0 To Ciclo Step 2
   Var2 = Dati_PC[Counter1]
   Var3 = Dati_PC[counter1+1]
   GoSub Conv_Ascii_Hex1
   InBuffer_Hex [Var1] = ASCII_HEX
   Inc Var1
Next

Conv_Ascii_Hex1:
                If Var2 > $60 Then Var2 = Var2 - $20
                If Var2 < $30 Then Error_Code = 1
                If Var2 > $39 Then
                    If Var2 < $41 Or Var2 > $46 Then Error_Code = 1
                EndIf
                If Var3  > $60 Then Var3 = Var3 - $20
                If Var3 < $30 Then Error_Code = 1
                If Var3 > $39 Then
                    If Var3 < $41 Or Var3 > $46 Then Error_Code = 1
                EndIf

                Var2 = Var2 -48
                Var3 = Var3 -48
                If Var2 >9 Then Var2 = Var2 -7
                If Var3 >9 Then Var3 = Var3 -7
                ASCII_HEX = (Var2 * 16) + Var3
                Return

top204

I'm not quite sure about the error checking you require, or what the program is actually doing fully, but below is a listing that will convert an array holding ASCII hexadecimal characters into an array holding their integer equivalent values:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Convert Hexadecimal characters held inside an array, into their integer values held in another array.
' Written for the  Positron8 compiler by Les Johnson
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at
'
' Setup USART1
'
    Declare Hserial_Baud = 9600
    Declare HRSOut1_Pin  = PORTC.6
    Declare HRSIn1_Pin   = PORTC.7 
'
' Create some variables for the demo
'
    Dim bSourceArray[10] As Byte Heap = "A5B6C7D8E9"        ' Create an array to hold the hex ASCII characters
    Dim bDestArray[10] As Byte Heap                         ' Holds the integer values converted from the ASCII hex characters
    Dim bHexCharsArray[3] As Byte                           ' Used to convert from hex ASCII to integer
    Dim bCharIndex As Byte                                  ' Used to read the source array that holds the hex ASCII characters 
    Dim bDestIndex As Byte                                  ' Used to load the destination array
    Dim bHexValue As Byte                                   ' Holds the converted hex ASCII to integer value
   
'--------------------------------------------------------------------
' The main program starts here
' Convert an array holding hexadecimal ASCII characters
' into a destination array holding the integer conversions
'
Main:
    Clear bHexCharsArray                                    ' Clear bHexCharsArray before the loop
    bDestIndex = 0                                          ' Start at the destination array index of 0
    For bCharIndex = 0 To 9 Step 2                          ' Create a loop to read the Hex characters from the source
        bHexCharsArray[0] = bSourceArray[bCharIndex]        ' \
        bHexCharsArray[1] = bSourceArray[bCharIndex + 1]    ' / Load the first 2 elements of bHexCharsArray with the hex characters
        bHexValue = Val(bHexCharsArray, Hex)                ' Convert the hex characters into an integer value
        bDestArray[bDestIndex] = bHexValue                  ' Load the integer value into the destination array
        Inc bDestIndex                                      ' Move up the element in the integer destination array
    Next
'
' Create a loop to display what is in the destination array on a serial terminal
'
    For bDestIndex = 0 To 4
        HRSOutLn Hex2 bDestArray[bDestIndex]
    Next

The conversion pieces of the above code listing could be placed in a procedure, so each value can be compared with something.


top204

#2
Below is the same principle as the program above, but uses a procedure to do the conversion from hex ASCII to integer, so the values can be individually compared within it.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Convert Hexadecimal characters held inside an array, into their integer values.
' Written for the  Positron8 compiler by Les Johnson.
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at
'
' Setup USART1
'
    Declare Hserial_Baud = 9600
    Declare HRSOut1_Pin  = PORTC.6
    Declare HRSIn1_Pin   = PORTC.7 
'
' Create some variables for the demo
'
    Dim bSourceArray[10] As Byte Heap = "A5B6C7D8E9"        ' Create an array to hold the hex characters
    Dim bDestArray[10] As Byte Heap                         ' Holds the integer values from the ASCII hex characters
    Dim bCharIndex As Byte                                  ' Used to read the source array that holds the hex ASCII characters 
    Dim bDestIndex As Byte                                  ' Used to load the destination array
    Dim bHexValue As Byte                                   ' Holds the converted hex ASCII to integer value
   
'--------------------------------------------------------------------
' The main program starts here
' Convert an array holding hexadecimal ASCII characters
' into a destination array holding the integer conversions
'
Main:
    bDestIndex = 0                                          ' Start at the destination array index of 0
    For bCharIndex = 0 To 9 Step 2                          ' Create a loop to read the Hex characters from the source
        bHexValue = ASCII_Hex(bSourceArray[bCharIndex], bSourceArray[bCharIndex + 1]) ' Convert the hex characters into an integer value
        bDestArray[bDestIndex] = bHexValue                  ' Load the integer value into the destination array
        Inc bDestIndex                                      ' Move up the element in the integer destination array
    Next
'
' Create a loop to display what is in the destination array on a serial terminal
'
    For bDestIndex = 0 To 4
        HRSOutLn Hex2 bDestArray[bDestIndex]
    Next
 
'--------------------------------------------------------------------
' Convert two hexadecimal ASCII characters into an 8-bit integer value
' Input     : pHexCharH holds the most significant hex character
'           : pHexCharL holds the least significant hex character
' Output    : Returns the 8-bit integer conversion result
' Notes     : An example would be using ASCII_Hex("1", "0"), and the integer would be returned as 16 ($10 in hex)
'
Proc ASCII_Hex(pHexCharH As Byte, pHexCharL As Byte), Byte
    Dim bHexChars[3] As Byte                        ' Used to convert from Hex ASCII to integer
    
    bHexChars[0] = pHexCharH                        ' \
    bHexChars[1] = pHexCharL                        ' / Load the two hex characters into "bHexChars"
    bHexChars[2] = 0                                ' Add a null to the last element of "bHexChars"
    Result = Val(bHexChars, Hex)                    ' Convert the hex characters into an integer value
EndProc

In the ASCII_Hex procedure, the Result variable can be used for comparisons, or a temporary variable created to do the comparisons, then Result loaded with a new value or an error value.

Mapo

Hi Les,
thanks for the reply, I see you convert the ascii with the VAL command, I hadn't thought about it, beautiful.
I do the error checking because the data is entered from the keyboard, and can have non-hexadecimal characters.
Is there a dedicated command to check if the entered data falls within the hexadecimal?
Thanks again and sorry for bad english

top204

#4
Since adding true procedures to the compilers, there is no need for new dedicated commands and functions within the compilers themselves, because they can be created by the user and shared. For example, below is a simple procedure to check if a value is a hexadecimal character:

'--------------------------------------------------------------------
' Check if a character is a valid hexadecimal character
' Input     : pValue holds the ASCII value for the character to check
' Output    : Returns 1 if the value is a hexadecimal character, else returns 0
' Notes     : None
'
Proc IsHex(pValue As Byte), Bit   
    Result = 1                  ' Default to the character being valid
    Select pValue
        Case "0" To "9"         ' Is pValue holding the value for character "0" to "9"?
                                ' Yes. So return the default because it is a valid hexadecimal character       
        Case "A" To "F"         ' Is pValue holding the value for character "A" to "F"?
                                ' Yes. So return the default because it is a valid hexadecimal character  
        Case "a" To "f"         ' Is pValue holding the value for character "a" to "f"?
                                ' Yes. So return the default because it is a valid hexadecimal character
        Case Else
            Result = 0          ' Otherwise... Return 0 because it is not a valid hexadecimal character
    EndSelect    
EndProc

Mapo