32-bit Unsigned Integers, Was: Unsigned Integers - How? [message #8110] |
Sat, 08 February 1997 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Peter Berdeklis <peter@atmosp.physics.utoronto.ca> carries on this
discussion about 16-bit unsigned integers when he writes:
> Unfortunately, I'm not reading 16-bit integers but 32-bit integers.
> Sorry I forgot to mention that. So how would I pull the same trick
> with 32-bit integers?
Somehow, Peter, I just *knew* you didn't have 16-bit integers! :-)
Alright, I know how to answer part of this question. Perhaps
we can get some help with the rest.
Suppose you have 100 32-bit *unsigned* integers in a data file.
Read them into IDL long *signed* integers. Like this:
data = LONARR(100)
READU, lun, datafile, data
First of all, if your data values are all less than 2L^31-1 or
2147483647 you are home free, don't worry about a thing.
If your data has values greater than that, things get a little dicey.
(Note that MAX(data) won't help much here because values of
2L^31 and higher will show as *negative* values. You basically
will have to know this some other way.)
Now, here is where I start to get unsure of myself. I know
how to turn *one* unsigned 32-bit integer into its real value.
You use the BYTE function to individually read the four
bytes of information in the 32-bit integer and you reconstruct
those bytes into a DOUBLE-PRECISION value. The code looks
like this:
number = data(0)
factor = 256.0D
realNumber = BYTE(number, 0)*factor^3 + BYTE(number,1)*factor^2 +$
BYTE(number,2)*factor^1 + BYTE(number,3)*factor^0
This is for a big endian machine, like most UNIX machines. If
you are on a little endian machine (like a PC), you will have to
reverse the order in which the real number is constructed. Your
code will look like this:
number = data(0)
factor = 256.0D
realNumber = BYTE(number, 0)*factor^0 + BYTE(number,1)*factor^1 +$
BYTE(number,2)*factor^2 + BYTE(number,3)*factor^3
What I don't know how to do (perhaps Bill Thompson or Mitchell Grunes
can help us here), is how to do this for the whole array at once in
an "array" type way. I certainly know how to write a loop! :-)
realNumbers = DBLARR(N_ELEMENTS(data))
factor = 256.0D
FOR j=0, N_ELMENTS(data)-1 DO BEGIN
realNumbers(j) = BYTE(data(j), 0)*factor^0 + BYTE(data(j),1)*factor^1 +$
BYTE(data(j),2)*factor^2 + BYTE(data(j),3)*factor^3
ENDFOR
Perhaps that will get us started.
> By the way David, I just looked up your Web page. Thanks for the tips.
> Since you worked at RSI, do you know why IDL doesn't have an
> unsigned data type?
No, I don't know why. Perhaps they believe idle minds are the
devil's workshop. :-)
Goin' dancin'. See you later...
David
-----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
2642 Bradbury Court, Fort Collins, CO 80521
Phone: 970-221-0438 Fax: 970-221-4762
E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com
-----------------------------------------------------------
|
|
|