cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Writing Binary Strings

astevenson-2
1-Visitor

Writing Binary Strings

The help file for WRITEBIN states:

WRITEBIN("file", "type", endian, [M]) writes an array of scalars to the binary data file named file.

Am I correct in concluding that an array of strings cannot be written to a binary file? How would I write string data to a binary file? How could I read string data from a binary file?

ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:astevenson-2)

Use str2vec() function to convert the string(s) to (an) array(s) of ASCII values before applying WRITEBIN(). Be sure to use 'byte' as the type.

Example:

Success!

Luc

View solution in original post

3 REPLIES 3
LucMeekes
23-Emerald III
(To:astevenson-2)

Use str2vec() function to convert the string(s) to (an) array(s) of ASCII values before applying WRITEBIN(). Be sure to use 'byte' as the type.

Example:

Success!

Luc

OK I see that and indeed it works. But a couple of questions come to mind. I would have thought the written file (in this case WriteStrings.txt) would not be human readable. What I mean is when I open the file with text editor I expected to see gibberish but yet the text is perfectly readable. Clearly I don't understand what WRITEBIN does.

The next question is what if my output file needs to have text and numbers. For instance I have a title row, followed by the number of data points in the file followed by the data.

Capture.JPG

How do I build a vector of mixed data types to write to a binary file?

LucMeekes
23-Emerald III
(To:astevenson-2)

Well, Andy, the file isn't human readable, because it contains binary numbers.

See here:

If you open it with NotePad however, that application takes the binary numbers, interprets them as ASCII codes and translates those codes to the characters according the ASCII table.

And applying vec2str() to the result of READBIN  as type 'byte', basically does the same.

If you want to mix text and numbers you first need to decide who (or what application) is going to read/interpret the file, then how you want/need to insert those numbers.

If you want the file to be readable by NotePad, you might want to use the num2str() conversion in Mathcad, followed by the WriteStrings function.

If another application or self written program is to read, AND interpret, the file, you might consider mixing the stuff.

In Mathcad 11 there isn't an APPENDBIN() function. I tried to write one, and it partially works as expected. That function should allow to first write out binary data as 'bytes', then a chunk of data as 32 bit floating point values etcetera.

Luc

Announcements

Top Tags