News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Convert binary to Hex string

Started by Trikkitt, Feb 28, 2022, 10:22 PM

Previous topic - Next topic

Trikkitt

Many of the commands have modifiers such as HEX.  However I'm unsure of the best way to convert a binary array in to a HEX string.  I'm thinking something like:

dim WiFiCMD as String * 100
dim RC522_token[10] as byte

    WiFiCMD[0]="T"
    WiFiCMD[1]=0
    WiFiCMD=WiFiCMD + Hex RC522_Token

Would this result in WiFiCMD containing something like "T31AF11003F0000000000".  If not what would be the most efficient way to performing the conversion?

Thanks

Mike



top204

#1
There are a few methods of performing what you require, but below is a simple method using the Str$ function:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demo to show how to load a String variable with Hex digits, read as integer values from a Byte array
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F25K20
    Declare Xtal = 16
'
' Setup USART1
'   
    Declare Hserial_Baud = 9600
    Declare HRSOut1_Pin = PORTC.6
'
' Create some variables
'  
    Dim bIndex As Byte                              ' Create an index for the filling of the String
    Dim MyByteArray[16] As Byte = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ' Create an array and load it with values
    Dim MyString As String * 40                     ' Create a String variable, large enough to hold the Hex characters
 
'--------------------------------------------------------------------
Main:   
    Clear MyString                                  ' Clear the String before continuing
    '
    ' Fill the String with the values read from the array, as Hex characters
    '
    For bIndex = 0 To Bound(MyByteArray)
        MyString = MyString + Str$(Hex2, MyByteArray[bIndex])    
    Next
  
    HRSOutLn MyString                               ' Transmit the String to a serial terminal   

On the serial terminal, you will see:

000102030405060708090A0B0C0D0E0F

Which are the values from MyArray, converted into Hex characters and added to MyString.

Trikkitt

Thanks - hoped there would be a super efficient way (from a code size and code complexity pov), but this looks like a solid way to do it.  I missed the modifier within the Str$ function.