News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Array to ASCII

Started by evoortman, Jul 05, 2023, 02:38 PM

Previous topic - Next topic

evoortman

Hi,

Can anyone show me how can i can convert a byte array to ascii values?
e.g. 6 element array 01 02 03 AB CD EF  =>  12 ascii characters "0" "1" "0" "2" "0" "3" "A" "B" "C" "D" "E" "F"

Thanks!

TimB


You want something like this


    Dim bIndex as Byte
    Dim aArray1 * 6 as byte


    for bIndex = 0 to 5

        Print Hex2 aArray1[bIndex]

    Next

If you want to place the output in a String look up str$ in the Manual



Gamboa

#2
evoortman,

Sorry for comments in Spanish.  You can use google translate to translate the comments.

'*************************************************************************
'** SUBRUTINA: ByteADosBytes
'** Convierte un byte hex a 2 bytes de su valor hex
'** Por ejemplo el  byte %00111010 ($3A) lo convierte en el ASCII 3 y el ASCII A
'** Parámetros de entrada: Tempo que es el byte a convertir
'** Parámetros de salida: Tempo1 y Tempo2 que son los bytes convertidos
'*************************************************************************

    ByteADosBytes:

    Tempo1 = Tempo & $0F  'separo cada nible en un byte
    Tempo2 = Tempo & $F0
    Tempo2 = Tempo2 >> 4

    SELECT Tempo1
    CASE 0 TO 9
      Tempo1 = Tempo1 + 48
    CASE ELSE
      Tempo1 = Tempo1 + 55
    ENDSELECT

    SELECT Tempo2
    CASE 0 TO 9
      Tempo2 = Tempo2 + 48
    CASE ELSE
      Tempo2 = Tempo2 + 55
    ENDSELECT
    RETURN
'** FIN SUBRUTINA: ByteADosBytes

'*************************************************************************
'** SUBRUTINA: DosBytesAByte
'** Convierte dos bytes ASCII a su valor hex de un byte
'** Por ejemplo el  byte %00110001  (ASCII 1) y %00110001  (ASCII 1) lo convierte en
'** el %00010001 $11
'** Parámetros de entrada: Tempo y Tempo1 (tempo el de menor peso)
'** Parámetros de salida: Tempo2 que es el byte convertido CON VALOR HEX.
'*************************************************************************

    DosBytesAByte:

    SELECT Tempo
    CASE 48 TO 57
      Tempo = Tempo - 48
    CASE 65 TO 70
      Tempo = Tempo - 55
    ENDSELECT

    SELECT Tempo1
    CASE 48 TO 57
      Tempo1 = Tempo1 - 48
    CASE 65 TO 70
      Tempo1 = Tempo1 - 55
    ENDSELECT

    Tempo2 = Tempo
    Tempo1 = Tempo1 << 4
    Tempo2 = Tempo2 | Tempo1

    RETURN

'** FIN SUBRUTINA: DosBytesAByte

Regards,
Gamboa
Long live for you

top204

#3
Below is a demonstration of another way of converting two hexadecimal ASCII characters into an 8-bit integer value, using a procedure and the compiler's Val function:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate converting two hexadecimal ASCII characters into a single 8-bit value
'
' 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 (in MHz)
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600                    ' Set the Baud rate to 9600
    Declare HRSOut1_Pin = PORTC.6                   ' Set the TX pin
'
' Create any variables for the demo here
'
    Dim bValue As Byte
    Dim bIndex As Byte
    Dim bSourceArray[12] As Byte Heap = "0", "1", "0", "2", "0", "3", "A", "B", "C", "D", "E", "F"

'-------------------------------------------------------------------------
' The main program starts here
'
Main:
    For bIndex = 0 To Bound(bSourceArray) Step 2                                ' Create a loop for the size of the source array
        bValue = ConvertToHex2(bSourceArray[bIndex], bSourceArray[bIndex + 1])  ' Convert the two hex nibbles
        HRSOutLn IHex2 bValue                                                   ' Display the value (in hex) on a serial terminal
    Next                                                                        ' Close the loop

'-------------------------------------------------------------------------------------------------------------
' Convert two Hex ASCII characters into an 8-bit integer value
' Input     : pHighNib holds the high nibble ASCII character of the hex value
'           : pLowNib holds the low nibble ASCII character of the hex value
' Output    : Returns the converted 8-bit integer value
' Notes     : None
'
Proc ConvertToHex2(pHighNib As Byte, pLowNib As Byte), Byte
    Dim bTemp[3] As Byte   

    bTemp[0] = pHighNib         ' Place the high nibble character into the temporary array
    bTemp[1] = pLowNib          ' Place the low nibble character into the temporary array
    bTemp[2] = 0                ' Add a null to the array
    Result = Val(bTemp, Hex)    ' Convert the hex characters into an integer value and return it
EndProc

The program will display the following texts on a serial terminal set for 9600 Baud:

$01
$02
$03
$AB
$CD
$EF

evoortman

Thanks everyone for the quick contribution.
Gamboa's code was the solution for me.