Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8051

Other programming languages • Re: Basic on Raspberry

$
0
0
In BASIC, binary data is typically stored using the DATA keyword.
No, DATA is for holding constant data, it can't be used for data that changes!
I haven't studied the string structure in PureBasic yet. But I remember for sure that in C++ strings end with a null byte. Then this is a shortcoming of C++ too.
Indeed, C's strings are not suitable for binary data, but BASIC's strings are! That C's strings cannot hold binary data is not really a "shortcoming" because C has other methods of storing and manipulating binary data, for example a memory block allocated with malloc().

BASIC is a very different language from C or C++ and one of the distinguishing features of BASIC is that its strings are binary-data compatible. One key advantage is that the string is managed, you can't accidentally write past the end of a string in BASIC and corrupt memory, whereas in C you can very easily do that.

For example suppose one wants to read 1 Kilobyte of binary data from a file, In BBC BASIC one would do that as follows:

Code:

       bin$ = GET$#file BY 1024
That reads 1024 bytes from the file identified by the handle #file into the string bin$. If you want a pointer to that string (which you may if you want to pass it to an API function, for example) you do that as follows:

Code:

      bin%% = PTR(bin$)
One particularly nice benefit from storing binary data in a string is that you can search it using INSTR, just like any other string. For example suppose we want to search the 1K binary blob that we read from the file to see if contains 0x00 0x00 anywhere:

Code:

      found% = INSTR(bin$, CHR$0 + CHR$0)      IF found% THEN PRINT "Data contains 0x00 0x00 at offset "; found%-1

Statistics: Posted by RichardRussell — Wed Apr 02, 2025 3:14 pm



Viewing all articles
Browse latest Browse all 8051

Trending Articles