comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Unsigned Integers - How?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Unsigned Integers - How? [message #8115] Fri, 07 February 1997 00:00
Peter Berdeklis is currently offline  Peter Berdeklis
Messages: 3
Registered: February 1997
Junior Member
On Fri, 7 Feb 1997, David Fanning wrote:
> Peter Berdeklis <peter@atmosp.physics.utoronto.ca> writes:
>
>> I'm trying to read a binary data file into IDL. Included in the data
>> headers are several unsigned integers for dates and times. Some of the
>> unsinged integers are overflowing, giving me negative integers. How do I
>> specify a variable as an unsinged integer in IDL?
>
> Read the unsinged integers into *signed* 16-bit integers in IDL.
> (You are already doing this, apparently).
>
> date = 0L
> time = 0L
> READU, lun, datafile, date, time
>
> Convert them to the correct *unsigned* values, like this:
>
> date = LONG(date) AND 'FFFF'x
> time = LONG(time) AND 'FFFF'x


Thank you for your response.

Unfortunately, I'm not reading 16-bit integers but 32-bit integers.
Sorry I forgot to mention that - too much time spent programming with
DJGPP (GNU C for DOS). So how would I pull the same trick with 32-bit
integers?


> Seeing the same question twice in the same week means it
> has to be tip material! I've put this answer on my web page
> for future reference.

In fact, it should be part of the IDL manuals. :)

Sorry for asking a question 2ce. I'm not a regular participant to the
newsgroup, just a desperate newbie.

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?

---------------
Peter Berdeklis
Dept. of Physics, Univ. of Toronto
Re: Unsigned Integers - How? [message #8118 is a reply to message #8115] Fri, 07 February 1997 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Folks,

I have the distinct feeling that it is Friday and a lot of
us are not taking enough exercise and getting the ol'
oxygen going to the brain.

Let me see if I can summerize this discussion without
causing any more head scratching and muttering.

If you have unsigned 16-bit integers in a file, you first
read them into *signed* 16-bit integers.

data = INTARR(100)
READU, lun, datafile, data

If the unsigned *value* is important to you, you will have
to convert this data to LONG integers with a command like
this:

data = LONG(data) AND 'FFFF'x

(Yes, those of you uncomfortable with hexadecimal numbers
may use 65535L.)

If memory is important, you probably want to throw a
TEMPORARY in there, like this:

data = LONG(TEMPORARY(data)) AND 'FFFF'x

If the unsigned *value* is not terribly important to you,
but the *relative position of the value in relation to
other values in the data* is important (e.g. maybe you
want to display the data as an image and don't care
what the *real* values are), then you can keep the
data as 16-bit integers, but you have to, as they say,
"twiddle" or change the top-most bit. This in effect
means you subtract an "offset" of -32768 from each
member of the data set.

The unsigned value 0 becomes the signed value -32768.
The unsigned value 32768 becomes the signed value 0.
The unsigned value 65535 becomes the signed value 32768.
And so forth.

According to a wonderful post by Struan Gray earlier (and
explained to me in a private e-mail posting by Mitchell
Grunes, to which I am very appreciative), this is most
easily done by a command like this:

data = TEMPORARY(data) XOR (-32768)

If you are going to use this 16-bit data set for some kind
of real-world purpose, you will have to remember that
the *real* values are offset by this -32768 amount.

There. I hope this clarifies rather than further muddles
the issue. :-)

I'm going to go play basketball with the boys and see if
I can't get a few more brain cells in gear!

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
-----------------------------------------------------------
Re: Unsigned Integers - How? [message #8120 is a reply to message #8115] Fri, 07 February 1997 00:00 Go to previous message
David Foster is currently offline  David Foster
Messages: 341
Registered: January 1996
Senior Member
David Foster wrote:
>> David Fanning wrote:
>>
>> Read the unsinged integers into *signed* 16-bit integers in IDL.
>> (You are already doing this, apparently).
>>
>> date = 0L
>> time = 0L
>> READU, lun, datafile, date, time
>>
>> Convert them to the correct *unsigned* values, like this:
>>
>> date = LONG(date) AND 'FFFF'x
>> time = LONG(time) AND 'FFFF'x
>>
>
> Oops! If you specify LONG then you get 32-bit integers. So:
>
> date = fix(0)
> time = fix(0)
> readu, lun, datafile, date, time
>
> date = date AND 'FFFF'x
> time = time AND 'FFFF'x
>

Double oops! Low voltage day or what!! Sorry about this noise.
I just meant that you need 16-bit integers when you read the
integers from the file (I'm assuming we're talking about shorts).
Of course, the last two lines should read:

date = long(date) AND 'FFFF'x
time = long(time) AND 'FFFF'x

as David Fanning suggested.

Sorry Dave.



--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
David S. Foster Univ. of California, San Diego
Programmer/Analyst Brain Image Analysis Laboratory
foster@bial1.ucsd.edu Department of Psychiatry
(619) 622-5892 8950 Via La Jolla Drive, Suite 2200
La Jolla, CA 92037
[ UCSD Mail Code 0949 ]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
Re: Unsigned Integers - How? [message #8121 is a reply to message #8115] Fri, 07 February 1997 00:00 Go to previous message
David Foster is currently offline  David Foster
Messages: 341
Registered: January 1996
Senior Member
David Fanning wrote:
>
> Peter Berdeklis <peter@atmosp.physics.utoronto.ca> writes:
>
>> I'm trying to read a binary data file into IDL. Included in the data
>> headers are several unsigned integers for dates and times. Some of the
>> unsinged integers are overflowing, giving me negative integers. How do I
>> specify a variable as an unsinged integer in IDL?
>
> It's deja vu all over again, as one of our famous baseball players once
> said. :-)
>
> Read the unsinged integers into *signed* 16-bit integers in IDL.
> (You are already doing this, apparently).
>
> date = 0L
> time = 0L
> READU, lun, datafile, date, time
>
> Convert them to the correct *unsigned* values, like this:
>
> date = LONG(date) AND 'FFFF'x
> time = LONG(time) AND 'FFFF'x
>

Oops! If you specify LONG then you get 32-bit integers. So:

date = fix(0)
time = fix(0)
readu, lun, datafile, date, time

date = date AND 'FFFF'x
time = time AND 'FFFF'x

Of course, if you want 32-bit integers, use LONG like Dave suggested.

Dave
--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
David S. Foster Univ. of California, San Diego
Programmer/Analyst Brain Image Analysis Laboratory
foster@bial1.ucsd.edu Department of Psychiatry
(619) 622-5892 8950 Via La Jolla Drive, Suite 2200
La Jolla, CA 92037
[ UCSD Mail Code 0949 ]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
Re: Unsigned Integers - How? [message #8125 is a reply to message #8115] Fri, 07 February 1997 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Peter Berdeklis <peter@atmosp.physics.utoronto.ca> writes:

> I'm trying to read a binary data file into IDL. Included in the data
> headers are several unsigned integers for dates and times. Some of the
> unsinged integers are overflowing, giving me negative integers. How do I
> specify a variable as an unsinged integer in IDL?

It's deja vu all over again, as one of our famous baseball players once
said. :-)

Read the unsinged integers into *signed* 16-bit integers in IDL.
(You are already doing this, apparently).

date = 0L
time = 0L
READU, lun, datafile, date, time

Convert them to the correct *unsigned* values, like this:

date = LONG(date) AND 'FFFF'x
time = LONG(time) AND 'FFFF'x

Seeing the same question twice in the same week means it
has to be tip material! I've put this answer on my web page
for future reference.

Cheers!

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
-----------------------------------------------------------
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: UNsigned Integer Data
Next Topic: Re: IDL 5.0 news at RSI website *correction*

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:46:47 PDT 2025

Total time taken to generate the page: 0.00571 seconds