473,382 Members | 1,581 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,382 software developers and data experts.

16 bit unsigned little endian reading issue

Hi everyone,

I have been working with Python since this summer and I used to frequent this board for advice and tips. Well...I'm back!

Anywho, I am reading in a file that is around 70 megs. It's a .img extension that contains quite a few CT slides. The way that a java based imaging program (ImageJ) reads it is a little unknown to me, but I know there is a header and the pictures follow.

My question is that when I open the file and read it to a variable, like:
data=open(filename,'r').read()

The variable (I believe is a unicode string) returns some data like this:
x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ etc.

How do I decode this so that I can use the info? As mentioned earlier I know it is in 16 bit unsigned little endian form. I have been reading some unicode stuff but have been coming up empty.

Thank you very much!
John Perry
Sep 24 '07 #1
1 3876
bartonc
6,596 Expert 4TB
Actually, you want to open in binary mode:
Expand|Select|Wrap|Line Numbers
  1. f = open('fName', 'rb')
  2. # then bite of a chunk of a known size
  3. # ie
  4. data = '\x00\x01\x00\x02\x00\x03\x00\x04'
  5. # then unpack it
  6. import struct
  7. print struct.unpack('HHHH', data)
  8. (256, 512, 768, 1024)
  9. print struct.unpack('>HHHH', data)
  10. (1, 2, 3, 4)
Sep 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
4
by: James Roberge | last post by:
I am having a little trouble getting my union/struct to work correctly. I am creating a struct that will contain information about the status of various Z80 cpu registers in an emulator i am...
19
by: Vincent | last post by:
Hi all, I want to convert a char (binary) to an unsigned long. How can I do this? Thanks, Vincent
3
by: nightolo | last post by:
Hi all, after all I have to say that I'm not a native english speaker so I pray you to excuse for my poor english. I got a trouble writing an application in a powerpc enviroment, that's the...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
36
by: Digital Puer | last post by:
Hi, suppose I have an unsigned long long. I would like to extract the front 'n' bits of this value and convert them into an integer. For example, if I extract the first 3 bits, I would get an int...
5
by: mohamed.alam78 | last post by:
Greetings, How does one find the format of a binary file to be in Little Endian or big Endian before we start reading the file. Thanks
14
by: moumita | last post by:
Hi All, I need to convert 4 bytes to an unsigned long. Suppose I have one array like unsigned char buf.I need to convert these 4 bytes into a single unsigned long. Is the following piece of code...
28
by: Fore | last post by:
Hello I am looking for some effecient way to convert a 32 bit unsigned integer to a 16 bit signed integer. All I want is the lower 16 bits of the 32 bit unsigned integer , with bit 15 (0..15) to...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.