String to Buffer

BlitzMax Forums/BlitzMax Programming/String to Buffer

ziggy(Posted 2005) [#1]
Is there any fast way to copy the contents of a String to a buffer?
something like:
local MyString:String = "Hello!"
Local MyBuffer:TBank 
MyBuffer.CreateFromString(MyString)  'This doesn't exists.



ImaginaryHuman(Posted 2005) [#2]
You could use VarPtr(MyString) to get the address of the string and then use CreateStaticBank pointing to the string's, memory, then use CopyBank to copy the contents to MyBuffer?


ziggy(Posted 2005) [#3]
a pointer to a String is not a pointer to its data, but a pointer to an structure (I think) wich has the Length and another pointer to the data, as BlitzMax Strings are not CStrings, it works like this. (I think).
I've tried this, but it doesn't worked.... :(


gman(Posted 2005) [#4]
if getting a C string is what you need to get over the hump, use MyString.ToCString(). if you need wide use MyString.ToWString().


ziggy(Posted 2005) [#5]
Thxs gman,
I don't want a CString, becouse BlitzMAx Strings can have any number of zero bytes, but a CString can't have any. and converting a BlitzMax String to a CString can make a lose of data, unless the data is plain ASCII text(not ANSI or UTF-8 or UTF-16, and neither a collection of concatened bytes).
What I'm thinking now is, what does ToWString() returns? Does it return the direction to the blitzMax String data?


gman(Posted 2005) [#6]
its a wide character string... probably not what you need. wish i had my trusty BMAX manual...


ziggy(Posted 2005) [#7]
he he, anyway, thanks!


gman(Posted 2005) [#8]
here ya go. think this is what you want :)


Framework BRL.Basic

Local test:TBank=New TBank
Local teststr:String="testing"

' create a stream to access the bank
Local tstream:TBankStream=TBankStream.create(test)
' write the string to the bank
tstream.writeBytes(teststr.ToCString(),teststr.length)

' make the string different to test
teststr="different"
DebugLog(teststr)

' get a string back out of the bank
teststr=String.FromCString(test.Buf())
DebugLog(teststr)




ziggy(Posted 2005) [#9]
I'll give it a try. thxs, but I'm not sure what will happens when the string has a chr(0) -> This means end of string in a CString, but not in a BlitzMax string.


gman(Posted 2005) [#10]
if the null character is an issue i think you may be ok. the following does not print "null".

Framework BRL.Basic

Local teststr:String="different"
DebugLog(teststr)

Local test:TBank=New TBank

' create a stream to access the bank
Local tstream:TBankStream=TBankStream.create(test)
Local testptr:Byte Ptr=teststr.ToCString()

For i=0 To teststr.length-1
	If (testptr[i]=0)
		DebugLog("null")
	EndIf
	DebugLog(Chr(testptr[i]))
	' write the char to the bank
	tstream.writeBytes(Chr(testptr[i]),1)
Next

teststr=String.FromCString(test.Buf())

DebugLog(teststr)



or, you can write directly from the string without converting to a pointer first:

Framework BRL.Basic

Local teststr:String="different"
DebugLog(teststr)

Local test:TBank=New TBank

' create a stream to access the bank
Local tstream:TBankStream=TBankStream.create(test)

For i=0 To teststr.length-1
	DebugLog(Chr(teststr[i]))
	' write the char to the bank
	tstream.writeBytes(Chr(teststr[i]),1)
Next

teststr=String.FromCString(test.Buf())

DebugLog(teststr)




Dreamora(Posted 2005) [#11]
how do you exactly get a 0 character into a BM string beside string + chr(0) + other_string? (which makes no sense anyway as chr(0) has no usefull visual representation)


gman(Posted 2005) [#12]
unless you add it in you are correct. i was suggesting use of ToCString which converts the BMAX string to a C string... which should be null terminated. by converting to a C string he was afraid it would introduce a null character into his bank. my code example checking for the null was checking the result of ToCString() for a null value. the second loops directly through the BMAX string, thus avoiding the ToCString altogether.


ziggy(Posted 2005) [#13]
Dreamora: Reading it from a file, for instance.
But the real point is, how can I get the address of the data stored in a BlitzMAx string, without converting it to a CString and running the risc of losing portions of data. I know most of the times I just won't lose data, becouse there will be no null character, but I can not ensure this!


gman(Posted 2005) [#14]
i guess im not positive on what your trying to accomplish? the last example i gave directly writes the contents of the BMAX string to the bank without converting to a C string. if your worried about a null character directly in the BMAX string, you can check each byte in the loop for 0 if you wish and just skip writing it if it is...


ziggy(Posted 2005) [#15]
hehe, sorry, it's a long story, I'm trying to comunicate two BlitzMax applications due StandardIO redirectioning, and I need to ensure I can write any combination of bytes (even null ones) on the standardoutstream. Print doesn't work becouse CString, and a problem writing spetial characters, so I am tring alternatives... and of you writebytes to the standarduiostring, voila!, this bytes are converted by BlitzMAx again to a CString, so any null truncates the data. I can not sent the 2 bytes of an integer to the Standard IO, becouse it's easy the first one is a null character, if the number is less than 255...