String formatting

I use XOJO 2019r3 for a Windows 10 desktop programma.

When I use code :

eventData = New MemoryBlock(length)
eventData = pointer.Ptr(0)
eventDataString = ""
For nr As Integer = 0 to length -1
  eventDataString = eventDataString + str(eventData.UInt8Value(nr), "000") + " "
Next

This works fine and the result is ‘255 033 001 000’
The values are correct and the formatting (“000”) works fine.

Following code :

eventData = New MemoryBlock(length)
eventData = pointer.Ptr(0)
eventDataString = ""
For nr As Integer = 0 to length -1
  eventDataString = eventDataString + str(eventData.UInt8Value(nr).ToHex) + " "
Next

This works fine too, and the result is ‘FF 21 1 0’
The values are correct (no formatting)

In the following code the problem arises in the result.
I tried to format the hex value with “00” but this didn’t work correctly

eventData = New MemoryBlock(length)
eventData = pointer.Ptr(0)
eventDataString = ""
For nr As Integer = 0 to length -1
   eventDataString = eventDataString + str(eventData.UInt8Value(nr).ToHex, "00") + " "
Next

The result is ‘00 21 01 00’.
Apparently the FF is replaced by 00 ?
I would like to have as result ‘FF 21 01 00’.

What am I doing wrong in the third code ?

Regards
Etienne

ToHex gave you a string and Str get any numeric, i guess str can not understand “FF”

you can also add “0” in front and cut right string with len of 2.

or try with 2 in minimumDigits (at least Integer have this)
.ToHex(Optional minimumDigits As Integer)

Thanks Markus, you put me to the right solution :
following code works fine :

eventData = New MemoryBlock(length)
eventData = pointer.Ptr(0)
eventDataString = ""
For nr As Integer = 0 to length -1
   eventDataString = eventDataString + str(eventData.UInt8Value(nr).ToHex(2)) + " "
Next

…ToHex(2) -> always return a hex value with 2 characters

Regards
Etienne

Why are you calling str on something that is already a string?

You are right Greg. Thanks.
I corrected it -> removed the str.
Before I changed it into Hex it was necessary, because eventData.UInt8Value(nr) was not a string.

Regards