Buffer with null terminated ascii strings

Just starting out? Need help? Post your questions and find answers here.
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Buffer with null terminated ascii strings

Post by wayne-c »

I have a memory buffer that contains a few ascii strings separated by null chars. What is the best method to get the individual strings from such a buffer?

Buffer:

Code: Select all

Hello World[NULL]Hello Sun[NULL]Hello Moon and Stars[NULL][NULL]
As you walk on by, Will you call my name? Or will you walk away?
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Buffer with null terminated ascii strings

Post by Saki »

Replace the zeros with e.g: # - With Peek.a - Poke.a
Then separate the parts with StringField.
地球上の平和
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Buffer with null terminated ascii strings

Post by NicTheQuick »

I would it do somehow like this:

Code: Select all

EnableExplicit

Define *p.Ascii = ?strings
While *p\a
	Define s.s = PeekS(*p, -1, #PB_Ascii)
	*p + Len(s) + 1
	Debug s
Wend


DataSection
	strings:
		Data.a 'H', 'e', 'l', 'l', 'o' ,' ', 'W', 'o', 'r', 'l', 'd', 0
		Data.a 'H', 'e', 'l', 'l', 'o', ' ' ,'S', 'u', 'n', 0
		Data.a 'H', 'e', 'l', 'l', 'o', ' ', 'M', 'o', 'o', 'n', ' ', 'a', 'n', 'd', ' ', 'S', 't', 'a', 'r', 's', 0
		Data.a 0
EndDataSection
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: Buffer with null terminated ascii strings

Post by wayne-c »

@Saki and @NicTheQuick : Thank you, both work fine. I'll go with Nic's solution because there is only one For..Next loop.
As you walk on by, Will you call my name? Or will you walk away?
User avatar
skywalk
Addict
Addict
Posts: 3996
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Buffer with null terminated ascii strings

Post by skywalk »

Keep in mind when the buffer is large, you suffer string concatenation delays. It is faster to append a byte array and convert to string when you need it.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Buffer with null terminated ascii strings

Post by Saki »

The procedure is to search for a section between two binary separators and to copy the binary section into a suitable empty string.
地球上の平和
Post Reply