Hello,
I am seeking some advice on the best way to work with binary data held in a file. The data is structured in terms of pages and data words to allow a human to write a description of the data. My application will have to work at bit level operating on 1 or more bits of a two byte (16 bit) data word. I have used the byte data type when working with other lower level programming languages and would like to know the "best" method of processing binary data in RevTalk.
An example of what I need to do may be of help:
Extract the value from Word n, MSB=5, LSB=3, then decode and display decode.
and then encode the display and write the data back to MSB=5, LSB=3.
Should I work with hex, binary strings or decimal numbers or is there a better way?
Working with Binary Data Files
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 919
- Joined: Wed Nov 04, 2009 11:41 am
Working with Binary Data Files
best wishes
Skids
Skids
Hi Simon,
I'll skip the part of your program that will have to deal with the reading in of the binary data from the file / seeking word n in the file and getting the value of word n into a variable.
Once you have the value in word n, you might use the operators RunRev provides for processing binary data, like bitAnd, bitOr, and bitXOR. For instance, to extract the value of word n (stored in tWordN) of the bits 3 to 5, you could use
if you count the least significant bit as bit #0. 56 because 8 + 16 + 32 (bits 3, 4, and 5 set) yields 56 in decimal, as a mask.
Does this help?
Regards
I'll skip the part of your program that will have to deal with the reading in of the binary data from the file / seeking word n in the file and getting the value of word n into a variable.
Once you have the value in word n, you might use the operators RunRev provides for processing binary data, like bitAnd, bitOr, and bitXOR. For instance, to extract the value of word n (stored in tWordN) of the bits 3 to 5, you could use
Code: Select all
answer tWordN bitAnd 56
Does this help?
Regards
-
- Posts: 919
- Joined: Wed Nov 04, 2009 11:41 am
Thanks for your post. I think the problem I am having is with the high levelness of RevTalk. Am I correct in thinking that tWordN in your example is storing the two bytes of data and that these may be extracted by RevTalk to be used as required say as a string, a decimal number or possible as a string representation of a binary number. I have not got the concept straight in my head yet.
For example I don't find the following copied from the dictionary that clear, what do they mean by numericdatatype and stringdataType ? I have no idea what they are getting at with the example; the data a3 could be a hex number describing 16 bits or it could be a string which again could be represented in 16 bits or 4 hex characters. (a=97dec Ascii, 3=63dec Ascii)=> 0x613F. As you can see I'm confused
Quoted from the dictionary:
Important! If you specify an amount with a stringdataType (a or A), the binaryDecodefunction places amountbytes in the next variable of the variablesList. If you specify an amount with a numericdataType, the function places amount chunks of data in the next amountvariables of the variablesList. For example, the dataType "a3" requires only one variable, which will hold a 3-characterstring, but the dataType "h3" requires three variables, each of which will hold a single hex digit.
For example I don't find the following copied from the dictionary that clear, what do they mean by numericdatatype and stringdataType ? I have no idea what they are getting at with the example; the data a3 could be a hex number describing 16 bits or it could be a string which again could be represented in 16 bits or 4 hex characters. (a=97dec Ascii, 3=63dec Ascii)=> 0x613F. As you can see I'm confused

Quoted from the dictionary:
Important! If you specify an amount with a stringdataType (a or A), the binaryDecodefunction places amountbytes in the next variable of the variablesList. If you specify an amount with a numericdataType, the function places amount chunks of data in the next amountvariables of the variablesList. For example, the dataType "a3" requires only one variable, which will hold a 3-characterstring, but the dataType "h3" requires three variables, each of which will hold a single hex digit.
best wishes
Skids
Skids
The high-levelness is something that requires very flexible handling on the side of RunRev, and is certainly slower than specialized data types as found in many other programming languages, like C, Pascal, etc. On the other hand, for the programmer, this flexibility saves time when writing code.
In my example, yes, tWordN is holding a certain value. I am not sure though, how many bits it does actually contain, since in my example, it is not clear how the data got into tWordN in the first place. If you use a "read" function for data access from a binary file, it depends on what "read" does when it fetches data and places them into a variable. So you can't declare tWordN as a two-byte variable, but it will, within its limit, hold as many bits as some other command puts into it. (or a string, or a movieplayer or a control or...).
on the stringData function, I cannot help you, sorry. I found the explanations just as confusing and obfuscated as you did, and I have never used that function. If it came to dealing with binary values, I would probably roll my own function - which may be much slower, of course, than some inbuilt code.
Regards,
In my example, yes, tWordN is holding a certain value. I am not sure though, how many bits it does actually contain, since in my example, it is not clear how the data got into tWordN in the first place. If you use a "read" function for data access from a binary file, it depends on what "read" does when it fetches data and places them into a variable. So you can't declare tWordN as a two-byte variable, but it will, within its limit, hold as many bits as some other command puts into it. (or a string, or a movieplayer or a control or...).
on the stringData function, I cannot help you, sorry. I found the explanations just as confusing and obfuscated as you did, and I have never used that function. If it came to dealing with binary values, I would probably roll my own function - which may be much slower, of course, than some inbuilt code.
Regards,
-
- Posts: 919
- Joined: Wed Nov 04, 2009 11:41 am