STARGÅTE's example is very close to the C use of a character array.
Incidentally, the first reply showing If *Buffer + i = 13 doesn't take into account the buffer as a character structure that is necessary in order to reference the byte values. Therefore it is looking for memory location of 13.
I suppose ordinarily, in PureBasic, assuming speed is important, we would tend to reference the bytes as CHARACTER type in a loop. The search character is parameterised in the below, as a function. Given that you're reading serial interface data, I'm not certain, but it might be that your data is ASCII, in which case the below would need to be changed — the character length therefore 1, the type ASCII and the structure element \a instead of \c
Code: Select all
EnableExplicit
; **
; ** Returns buffer position (0 to length - 1) of specified character
; ** -1 signifies not found
; **
Procedure Locate_Char(*StrPtr.Character, length.i, character.i)
Protected index.i ; Byte offset, commences 0
While index.i < length
If *StrPtr\c = character.i ; Character no. specified
ProcedureReturn index.i ; Return offset
EndIf
*StrPtr + SizeOf(character) ; Advance byte pos. by character size
index.i + 2 ; ... or just 2
Wend
ProcedureReturn -1 ; Not found
EndProcedure
; **
; ** Main
; **
Define *Buffer
*Buffer = AllocateMemory(100, #PB_Memory_NoClear)
; Result = ReadSerialPortData(1, *Buffer, Len)
Debug Locate_Char(*Buffer, 100, 0) ; 100 bytes, search for char 0
ShowMemoryViewer(*buffer, 100)