Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / A problem with null strings, null characters, CHR$(0), ASC(0)

Author
Message
Mage
11
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 24th Mar 2018 04:17
Since I am working with a lot of file I/O in a rather surgical way, I have noticed that I cannot store a HEX 00 character in a string. They are completely ignored.



What is the best way around this problem? Thanks.
Mage
11
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 24th Mar 2018 18:15
Thought about it some more. Might just store string characters as bytes in a redim'd array. Unless performance becomes an issue.

This conversation from 2005 asserts this is a limitation of DBP.
https://forum.thegamecreators.com/thread/67452
Phaelax
DBPro Master
15
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 26th Mar 2018 18:24
Probably better off using an array of bytes, or storing what you need in a memblock.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Mage
11
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 26th Mar 2018 19:47
Phaelax wrote: "Probably better off using an array of bytes, or storing what you need in a memblock."

I am having problems writing a generic function to read characters into an array since it would need to be a different array each time. You can't pass an array back from a function. Memblocks seem like a good idea but I don't like that there is a maximum number. So they are only good if the number of strings in a situation is low. Storing 256 separate strings with memblocks is not possible.

My quick fix to the problem was to read any 00 character and change it to char 0xFF (decimal 255), and then when writing it back to a file changing all 0xFF to 0x00. This works until you end up needing a legit 0xFF character. Since these strings only store readable characters, that won't happen in my case. If I started storing bytes in strings as some sort of trick method then this would become a problem.
Starshyne Emir
1
Years of Service
User Offline
Joined: 27th Nov 2016
Location: The Darkness Beyond Time
Posted: 27th Mar 2018 12:38
Null characters were made to be ignored. DBPro is doing exactly what it's expected from it: pretending that's not there.

I started programming in the old QBASIC and it treated null characters the same way.

Frankly you should think on another way to do what you are intending to do, because null characters will be counted as nothing into any string, no matter how you try to assemble them.

An array of bytes is a great idea, but you will need to provide the ASCII values manually as numbers or the issue will persist.



In this example, you can see that the string lenght shown on screen is 1 instead of the expected 2.

Now, let's see another:



As you can see, no matter what you do, null strings are null. They are not counted or read whatever the means used.

Better think on something else. I suggest redesigning your application and thinking on another solution to achieve your result.





[size=+2]Forever and one[/size]
Hotline
9
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 27th Mar 2018 13:50 Edited at: 27th Mar 2018 13:51
Quote: "Null characters were made to be ignored"

Null character is not ignored , in fact it is a very important meaningful character.Null character is a special character that signals the end of the string.Any character after '\0' is ignored.That's true only for null terminated strings hovewer.

Quote: " because null characters will be counted as nothing into any string"

That's true only for null terminated strings.For an example std:;string is NOT null terminated so you can use null as a character.The only problem with this if you convert it to a null terminated c string it will ignore everything after the first \0.
[href=forum.thegamecreators.com/?m=forum_view&t=191567&b=5]Spark Particle engine[/href]
[href=forum.thegamecreators.com/?m=forum_view&t=199163&b=5]Transform gizmo plugin[/href]
Mage
11
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 28th Mar 2018 02:11
Starshyne Emir wrote: "Null characters were made to be ignored. DBPro is doing exactly what it's expected from it: pretending that's not there.

I started programming in the old QBASIC and it treated null characters the same way. "

it's still a limitation of DBP.

Starshyne Emir wrote: "Frankly you should think on another way to do what you are intending to do, because null characters will be counted as nothing into any string, no matter how you try to assemble them."

I am implementing someone else's file format, which includes strings that have 0x00 characters in various parts. I don't have a choice in the matter. My quick and easy solution was just to change those characters to 0xFF when reading and change them back when writing. Workable but not a perfect solution. It could cause issues in the future if I need to do more complicated things.

Starshyne Emir wrote: "An array of bytes is a great idea, but you will need to provide the ASCII values manually as numbers or the issue will persist."

That's what I meant when I said array of bytes.

Starshyne Emir wrote: "An array of bytes is a great idea, but you will need to provide the ASCII values manually as numbers or the issue will persist.
In this example, you can see that the string lenght shown on screen is 1 instead of the expected 2. "

The example I provided demonstrated this.

Starshyne Emir wrote: "Better think on something else. I suggest redesigning your application and thinking on another solution to achieve your result."

Like I said that's not really an option here, I have to provide support for these characters.

I am mentioning it since people might have better methods that I didn't think of. Bored of the rings mentioned using Memory Banks (as a faster alternative to using memblocks) and memory pointers, which is part of a plugin. This might be needed in the long term if things get really complicated, but I was hoping for a solution that didn't require needing an additional plugin.

Login to post a reply

Server time is: 2018-08-15 02:38:32
Your offset time is: 2018-08-15 02:38:32