473,797 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary handling

Hello all,
I have a file of binary data. I want to read this file and parse the data in
it. The format of the file is:
8 bits number of data pakets
6 bits offset
20 bits time
8 states

How can I read this fields in binary? I see the struct module and the
binascii, but I can't split the diferent fiels.

Thanks in advance
Regards
--
Defel
Jul 18 '05 #1
3 1811
Derfel wrote:
Hello all,
I have a file of binary data. I want to read this file and parse the data
in it. The format of the file is:
8 bits number of data pakets
6 bits offset
20 bits time
8 states

How can I read this fields in binary? I see the struct module and the
binascii, but I can't split the diferent fiels.


You can use struct to create a long from your data and then work on that
with the usual bitwise operators for shift and boolean and so that you
extract the actual data. If the above is the entire spec, I see some
problems to get the alignment proper, but with a bit of shifting you should
be able to work around that.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Hello Derfel,
I have a file of binary data. I want to read this file and parse the data in
it. The format of the file is:
8 bits number of data pakets
6 bits offset
20 bits time
8 states

How can I read this fields in binary? I see the struct module and the
binascii, but I can't split the diferent fiels.

--- bitter.py ---
# Testing
'''
b = BitStream(chr(i nt("10111010", 2)))
b.get_bits(3) 5 b.get_bits(2) 3 b.get_bits(3) 2 b.get_bits(0) 0 b.get_bits(1)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/tmp/bitter.py", line 27, in get_bits
raise EOFError
EOFError
'''

from array import array

class BitStream:
'''Bit stream implementation' ''
def __init__(self, data):
self.arr = array("c", data)
self.bit = 0 # Current output bit

def get_bits(self, count):
'''Get `count' bits from stream'''
if count == 0: # Nothing to get
return 0

if not self.arr: # EOF
raise EOFError

value = 0
for c in range(count):
if not self.arr: # EOF
raise EOFError
value <<= 1
shifted = ord(self.arr[0]) >> (8 - self.bit - 1)
value |= (shifted & 1)
self.bit += 1
if self.bit == 8: # Get new byte
self.arr.pop(0)
self.bit = 0

return value

def _test():
from doctest import testmod
import bitter

testmod(bitter)

if __name__ == "__main__":
_test()
--- bitter.py ---

HTH.
Miki
Jul 18 '05 #3
Miki Tebeka escribió en news:40******** ******@zoran.co m:
Hello Derfel,

--- bitter.py ---


Thanks for the module, it works well and is the solution to my problem.
Thanks.
Regards
--
Derfel
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6999
by: herc777 | last post by:
I want to manipulate binary files. this was just a test to concatinate 2 text files but it said file_put_contents was undefined. the second parameter is suppoed to be 'mixed data', no idea what that is. <?php $t1 = file_get_contents('test1.txt'); $t2 = file_get_contents('test2.txt'); $result = file_put_contents('test3.txt', $t1 . $t2);
5
7572
by: Bill Loren | last post by:
Hello ppl, I'm having difficulties to accomplish some simple chores with binary data. I'm having a string (?) which I received via an HTTP transactions which is a binary file. Problem is the webserver I'm communicating with injected a \x0D before every \x0A, and I need to remove those 0x0D characters from my buffer before saving it to disk.
6
481
by: someone | last post by:
Suppose that I have a class in an assembly that is delivered to the user, what can I do to change the class so that it doesn't break the binary compatibility? That is, user application can run with recompiling and relinking. I know that if I define an interface, and only expose the interface but not the class which implments the interface, I can add a data member to the class without breaking the binary compatibility. If the class...
103
48769
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
4
4042
by: Holger Marzen | last post by:
Hi all, AFAIK it is possible for columns to be very large, up to about 2 GB. Are there any hints or experiences about storing binary data (jpg-images, pdf-documents) in PostgrreSQL with or without the complicated lo-stuff? Of course it's in many cases a good approach to store those files simply in the file system but there's always a risk of running out of sync (filesystem and tables), e.g. by deleting files and not deleting the table...
4
2467
by: jesuraj | last post by:
Hi, how can i read input from a data file which contains binary or hex values. I have to use the exact binary data for further processing.only limited number of bits are taken form the file(64 bits) for current processing.Once the processing is done the next 64 bits must be applied.Can this be done?
6
10040
by: SandyMan | last post by:
Hi, I am able to open a binary file for reading but can someone tell me as how to go about converting a Binary file to ASCII file using C. Thanks In Advance SandyMan
7
7029
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap file, I essentially have a char buffer in my streambuf class, that I'm registering with setp(). on an overflow() call, I simply copy the contents of the buffer into the mmap'd file via memcpy().
2
2743
by: CharlesL | last post by:
I am trying to handle binary strings in php. I get a binary output initialization vector from mcrypt as such: from mcrypt: $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); This output may have padded nulls at the end which are significant. I would like to convert all characters including all padding nulls at end and convert it in a string to be saved into a database in such a way
9
3246
by: lokeshrajoria | last post by:
hello everybody, i need some help in binary file handling in perl. can anybody give me some information about binary file. actully i am reading data from some text file and extracting some usefull information from there and want store in my own binary file with .vbf extension ( not like .dat file.) i am putting code here for reading text file and extract some information from there but how can i store that information in my own binary file. ...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10468
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.