LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

convert a binary number to ascii

Hi, does anybody have an algorithm to convert a binary file to ascii.
I have enclosed a simple sine waveform that needs to be converted to ascii.

0 Kudos
Message 1 of 13
(4,661 Views)
Hello Preitano,

you should tell us a little bit more of the file format. All files are "binary" and also "ASCII" in some kind 🙂

Do you want to have a hexadecimal representation, decimal numbers or binary ('10011101') numbers?
Your file seems to contain values consisting either of 5 bytes or 4 bytes separated by a zero. Do you know more about the format of those values?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 13
(4,655 Views)
Hi sorry for the lack of information.

The file is exported from and AWG710B instrument.

According to their manual, the waveform file format is composed of 4-byte Little Endian format specified in IEEE488.2 floating point numbers and 1-byte of marker data.
The waveform file contains waveform data in single precision floating-point numbers and marker data.

It then goes into explaining the 3 parts of the waveform file.

<Waveform File>::<Header><Body>[<Trailer>]
where:
<Header>::=MAGIC<space>1000<CR><LF>
<Body>::=#<Num_digits><Num_bytes><Data(1)>...<Data(n)>

          <Num_digits> is the number of digits in <Num_bytes>
          <Num_bytes> is the byte count of the data that follows.
          <Data(n)>::=<Waveform><Marker>

         <Waveform> is the single precision floating-point number of 4-byte Little Endian format specified in IEEE488.2.  The full scale of the D/A converter of the waveform generator             corresponds to -1.0 to 1.0.

        <Marker> is 1-byte of marker data.  The bit 0 (LSB) and bit 1 represent Markers1 and 2 respectively.

<Trailer>::=CLOCK<space><clock><CR><LF>

Hope this helps.

0 Kudos
Message 3 of 13
(4,645 Views)

Actually before you convert it to ASCII you first need to get LabVIEW to read and understand what's in the file. After you accomplish that, then you can worry about turning the numbers into ASCII (if that is really what you want to do with them).

The attached VI appears to do a pretty good job of understanding what's in the file with the exception of the actual data. Are you sure the file you posted contains a sinusoid?

I went so far as expanding the binary data to individual binary bytes so I could look for the most significant bit of one of them changing in a regular pattern as the values swung from positive to negative & back but no obvious pattern was observed. According to the NI application note #154 (part of the "LabVIEW Bookshelf") "Single-precision floating-point numbers have 32-bit IEEE single-precision format." so it may naturally use the format you have. Although LabVIEW, with its Mac roots, is Big-Endian, I'm not sure that carries over to IEEE number formats. It is however easy enough to try either swapping or not swapping the bytes and in the case of your data, it doesn't seem to make it any more understandable.

 

0 Kudos
Message 4 of 13
(4,621 Views)
Hi

Thanks for you help.

Enclosed is the ascii file for the sine wave.
What I need to do is convert the sine.txt file into 4-bytes and then send this to the instrument.  I then have to take the binary file and convert it back to .txt.

The first file was the binary and the enclosed file is the ascii.
0 Kudos
Message 5 of 13
(4,619 Views)
The attached VI shows how you would convert an array of single precision floating point numbers into a byte stream and (for example) write it to a file.
0 Kudos
Message 6 of 13
(4,614 Views)

I assume you know about the Tek AWG 700 LabVIEW driver found here? (or possibly here)

FWIW, I was able to find it by going here and searching for Tektronix and "waveform".

I just noticed that the library does make use of a "AWG_Make32bitFloat2Str.vi" routine that Typecasts a SGL value to a string and then reverses the bytes of the string like it is converting from Big-to-little endian.

Message Edited by Warren Massey on 11-30-2005 01:08 PM

0 Kudos
Message 7 of 13
(4,608 Views)
OK, one more update and then I've really got to get back to work. The afore-mentioned driver library has this VI in it: "AWG_MakeWaveformData.vi"
 and in frame #1 of the sequence structure is a FOR loop that converts the incoming ASCII file data, line by line into the binary stuff you shove out to the instrument. As a part of this line-by-line conversion the code looks for a floating point number followed by two comma-seperated integer numbers. Note that this does not quite match the ASCII file you provided (which had only lines of floating point numbers, no commas and no integer numbers). Anyway the conversion process mixes the two integers into a one-byte string and concatenates it onto the 4-byte string that it already gotten for the SGL floating point value. Then these FIVE bytes are appended to the data string that is being built up. It was this extra fifth byte that was messing up the interpretation of the data read from the file. Once I modified the attached VI to read the data in chunks of five rather than four it started working.
 
I'm not sure what those extra two comma-delimited integer numbers might be used for, but they don't exist in the data you've provided, they just have to be allowed for in the code. Oh, wait, I see now that it is some sort of "marker" data flag.

 

Message Edited by Warren Massey on 11-30-2005 02:42 PM

0 Kudos
Message 8 of 13
(4,602 Views)
Thanks a lot for all you help.


0 Kudos
Message 9 of 13
(4,570 Views)
Thanks again.

However I do not fully understand the "AWG_MakeWaveformData.vi".  I don't fully comprehend how my sine.txt is converted to binary.

0 Kudos
Message 10 of 13
(4,562 Views)