473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Reading binary from a file...

Hi!
In python I'm able to read in binary data from a file.
data = file.read() # Reads in an entire file.

However the data is 16bits per sample and python is storing the
data in a string. How do I convert that 8bit data into a list of 16
bit integers?

Note: I want generic python lists or tupels not numpy or numeric etc
etc...

Mar 8 '06 #1
3 1665
KraftDiner wrote:
Hi!
In python I'm able to read in binary data from a file.
data = file.read() # Reads in an entire file.
Note that you should open the file in binary mode to be
platform-agnostic and as portable as possible. (Just in case you aren't).
However the data is 16bits per sample and python is storing the
data in a string. How do I convert that 8bit data into a list of 16
bit integers?

Note: I want generic python lists or tupels not numpy or numeric etc
etc...

You really want the struct module here.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 8 '06 #2
Steve Holden a écrit :
KraftDiner wrote:
Hi!
In python I'm able to read in binary data from a file.
data = file.read() # Reads in an entire file.

Note that you should open the file in binary mode to be
platform-agnostic and as portable as possible. (Just in case you aren't).
However the data is 16bits per sample and python is storing the
data in a string. How do I convert that 8bit data into a list of 16
bit integers?

Note: I want generic python lists or tupels not numpy or numeric etc
etc...

You really want the struct module here.


If all data are same 16 bits integer values, you can also see the array
module (and the fromfile() method - see docs).

A+

Laurent.
Mar 8 '06 #3
KraftDiner wrote:
[...]
In python I'm able to read in binary data from a file. [...]
However the data is 16bits per sample and python is storing the
data in a string. How do I convert that 8bit data into a list of 16
bit integers?


On the vast majority of systems, files hold sequences of
eight-bit integers. How you convert from those to your 16-bit
type depends on how the the writer of the file converted the
16-bit integer type to a sequence 8-bit integers.
--
--Bryan
Mar 8 '06 #4

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

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.