News:

;) This forum is the property of Proton software developers

Main Menu

Print Array in Hex

Started by shantanu@india, Aug 13, 2022, 11:10 AM

Previous topic - Next topic

shantanu@india

I think I've begun to sound repetitive... well nothing doing!

I want to print an array directly in hex...
Dim MyArray[16] as Byte
Dim loop as Byte
'Filling array with unprintables
For loop=0 to 15
    MyArray[loop]=200+loop
Next
Print At 2,1, Str MyArray\16

Will this printout the hex values?
Print At 2,1, Hex2 MyArray[0], Hex2 MyArray[1]..... Maybe that's the only way...
Regards
Shantanu

top204

#1
'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate a procedure to transmit an array's contents as ASCII hex
' 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
    Declare HRSOut_Pin = PORTC.6
'
' Create variables for the demo
'
    Dim bIndex As Byte                              ' Holds the index to fill the array
    Dim Global_ByteArray[16] As Byte Heap           ' Holds the data to transmit (Set to sit at the top of other variables in RAM)

'-------------------------------------------------------------------------------------------------------
' The main program starts here
'
Main:
'
' Fill the array with values
'
    For bIndex = 0 To Bound(Global_ByteArray)
        Global_ByteArray[bIndex] = bIndex  
    Next
    Send_ArrayAsHex()                               ' Transmit the contents of Global_ByteArray as ASCII hex  
   
'-------------------------------------------------------------------------------------------------------
' Transmit the contents of the array "Global_ByteArray" as ASCII hex
' Input     : Global_ByteArray holds the array to send
' Output    : None
' Notes     : None
'
Proc Send_ArrayAsHex()
    Dim bIndex As Byte
   
    For bIndex = 0 To Bound(Global_ByteArray)           ' Create a loop for the size of the array to send
        HRSOut Hex2 Global_ByteArray[bIndex]            ' Send the array's element as an ASCII hex value
    Next                                                ' Close the loop
    HRSOut 13                                           ' Send the terminator character
EndProc

On the serial terminal, it will show:

000102030405060708090A0B0C0D0E0F

To make them comma delimited, change the procedure to:

'-------------------------------------------------------------------------------------------------------
' Transmit the contents of the array "Global_ByteArray" as ASCII hex
' Input     : Global_ByteArray holds the array to send
' Output    : None
' Notes     : Comma delimits the hex values
'
Proc Send_ArrayAsHex()
    Dim bIndex As Byte
   
    For bIndex = 0 To Bound(Global_ByteArray)           ' Create a loop for the size of the array to send
        HRSOut Hex2 Global_ByteArray[bIndex]            ' Send the array's element as an ASCII hex value
        If bIndex < Bound(Global_ByteArray) Then        ' Is it the last element in the array?
            HRSOut ","                                  ' No. So add a comma
        EndIf                                          
    Next                                                ' Close the loop
    HRSOut 13                                           ' Send the terminator character
EndProc

The serial terminal will then show:

00,01,02,03,04,05,06,07,08,09,0A,0B,0C,0D,0E,0F

To make the comma delimited text easier to see they are hex values, change the procedure to:

'-------------------------------------------------------------------------------------------------------
' Transmit the contents of the array "Global_ByteArray" as ASCII hex
' Input     : Global_ByteArray holds the array to send
' Output    : None
' Notes     : Comma delimits the hex values
'
Proc Send_ArrayAsHex()
    Dim bIndex As Byte
   
    For bIndex = 0 To Bound(Global_ByteArray)           ' Create a loop for the size of the array to send
        HRSOut IHex2 Global_ByteArray[bIndex]           ' Send the array's element as an ASCII hex value, with a preceding $ character
        If bIndex < Bound(Global_ByteArray) Then        ' Is it the last element in the array?
            HRSOut ","                                  ' No. So add a comma
        EndIf                                          
    Next                                                ' Close the loop
    HRSOut 13                                           ' Send the terminator character
EndProc

The serial terminal will then show:

$00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F

See what I meant in a previous thread about the compiler's commands and modifiers and built-in functions not always being required now? Just use the ones that serve a particular purpose and create your own procedures to perform more involved tasks.


top204

#2
As an alternative to the above procedure, any byte array can be sent with the same procedure using the program listing below. Notice the use of the ByRef directive in the procedure:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate a procedure to transmit an array's contents as ASCII hex, using an indirect address parameter
' 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
    Declare HRSOut_Pin = PORTC.6
'
' Create variables for the demo
'
    Dim bIndex As Byte                              ' Holds the index to fill the array
    Dim Global_ByteArray[16] As Byte Heap           ' Holds the data to transmit (Set to sit at the top of other variables in RAM)

'-------------------------------------------------------------------------------------------------------
' The main program starts here
'
Main:
'
' Fill the array with values
'
    For bIndex = 0 To Bound(Global_ByteArray)
        Global_ByteArray[bIndex] = bIndex + 10 
    Next
    Send_ByteArrayAsHex(Global_ByteArray, SizeOf(Global_ByteArray))  ' Transmit the contents of Global_ByteArray as ASCII hex  
   
'-------------------------------------------------------------------------------------------------------
' Transmit the contents of a byte array as ASCII hex
' Input     : pArrayAddr holds the address of the array to send
'           : pSize holds the size of the byte array to send
' Output    : None
' Notes     : Comma delimits the hex values
'
Proc Send_ByteArrayAsHex(ByRef pArrayAddr As Word, pSize As Byte)
    Dim bIndex As Byte
    Dim bTemp  As Byte
   
    For bIndex = 0 To pSize - 1                     ' Create a loop for the size of the array to send
        bTemp = Ptr8(pArrayAddr++)                  ' Read the byte array element's value
        HRSOut IHex2 bTemp                          ' Send the array's element as an ASCII hex value, with a preceding $ character
        If bIndex < (pSize - 1) Then                ' Is it the last element in the array?
            HRSOut ","                              ' No. So add a comma
        EndIf                                          
    Next                                            ' Close the loop
    HRSOut 13                                       ' Send the terminator character
EndProc

On the serial terminal, it will show:

$0A,$0B,$0C,$0D,$0E,$0F,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19





shantanu@india

Yes Les... I need to improve my code writing!!
Too lazy I suppose, when I know someone else is doing all the hard work.
I regularly write functions in Python, so why shouldn't I write procedures in Positron??
Thanks.
Regards
Shantanu