• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problem with reading 4-byte floats from file on Intel
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with reading 4-byte floats from file on Intel


  • Subject: Re: Problem with reading 4-byte floats from file on Intel
  • From: jj <email@hidden>
  • Date: Thu, 06 Dec 2007 01:06:02 +0100
  • Thread-topic: Problem with reading 4-byte floats from file on Intel

> Hello jj,
>
> As far as I can tell, "dd" can only swap consecutive bytes.  When you
> are working with multi-byte quantities such as "small real", ABCD
> becomes DCBA, not BADC.  Hope that is clear.

It wasn't till now... I thought the only difference between little and big
endian was based on couples of bytes (that means I was expecting BADC from
ABCD, instead of DCBA) ;-)


jj

>          - Rick
>
> On 12/5/2007 8:33 AM, jj wrote:
>> Well, there are various routines to swap bytes if you are interested in a
>> semi-all-applescript solution. I use this one:
>>
>> to reverseByteOrder(f) --> f = file spec or alias
>>     do shell script "dd conv=swab < " & ¬
>>         quoted form of POSIX path of f & " > /tmp/reversed"
>>     set x to (read ("/tmp/reversed" as POSIX file) as string)
>>     set f to (open for access f with write permission)
>>     set eof of f to 0
>>     write x to f as string
>>     close access f
>> end reverseByteOrder
>>
>> You can first swap bytes in the entire file, then read by 4, 512 or whatever
>> you wish.
>>
>> If you don't want alther the original file, just adapt the code in
>> reverseByteOrder(): use only the first line (the one with the command-line
>> call), then read "as small real" the file "/tmp/reversed" (or whatever
>> output destination you wish).
>>
>>
>> jj
>>
>>
>>> Hi jj,
>>>
>>> The purpose of my script was to test a binary file that contains 4-byte
>>> floats (aka "small real") in a known format to see if the file was
>>> big-endian or little-endian.
>>>
>>> The fact that the script ran fine on your Intel Mac but not on mine
>>> tells that might be a problem with reading lots of objects from a binary
>>> file. When I reduced the number of bytes I tried to read from 512 bytes
>>> (128 floats) to (for example) 128 bytes, everything was fine even on my
>>> Intel Mac.  So I could certainly read and process one float at a time,
>>> if I could figure out how to do it.  In AppleScript I'm not sure how to
>>> read 4 bytes, swap them (but only on Intel Mac to simulate a little
>>> endian file read), coerce to "small real", and then test.  Or maybe
>>> there's a better way.
>>>
>>> Thanks again for all of your time,
>>>
>>>         - Rick
>>>
>>> On 12/5/2007 7:57 AM, jj wrote:
>>>> Well, you could swap bytes before attempting to read (if that is the real
>>>> issue)... But noone of the methods I know may be faster than your C tool...
>>>>
>>>>
>>>> jj
>>>>
>>>> * BTW, I never read a specific binary file known to contain numbers. I used
>>>> in my test a random text file, but I guess it's the same: it will just read
>>>> double-bytes and interpret them as small reals.
>>>>
>>>>> Hello jj,
>>>>>
>>>>> I was reporting my experiences with Mac OS X 10.4, not 10.5.  But it is
>>>>> good to know that my future testing with 10.5 would also fail.
>>>>>
>>>>> Having found out on a non-Apple web page that (apparently) AppleScript
>>>>> is always reading binary files in big-endian mode, I have abandoned this
>>>>> script.  I wrote a C command line tool in just a few minutes that did
>>>>> exactly what I wanted.
>>>>>
>>>>> Thanks for your help, everybody.  If there is more information on how to
>>>>> do this in AppleScript, I am still interested!
>>>>>
>>>>>             - Rick
>>>>>
>>>>> On 12/4/2007 7:35 PM, jj wrote:
>>>>>>> First, running Leopard I do reiterate I get an error.
>>>>>> Ooops! I didn't read before this issue was Leopard-related. I thot it was
>>>>>> only Intel-related.
>>>>>>
>>>>>> I can only confirm that reading small reals works fine in my machine, OS
>>>>>> X
>>>>>> 10.4.11 ;-)
>>>>>>
>>>>>>
>>>>>> jj
>>>>>>
>>

--
http://www.macscripter.net/
http://www.osaxen.com/


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Problem with reading 4-byte floats from file on Intel
      • From: Doug McNutt <email@hidden>
References: 
 >Re: Problem with reading 4-byte floats from file on Intel (From: email@hidden)

  • Prev by Date: Re: Renaming files
  • Next by Date: AppleScript
  • Previous by thread: Re: Problem with reading 4-byte floats from file on Intel
  • Next by thread: Re: Problem with reading 4-byte floats from file on Intel
  • Index(es):
    • Date
    • Thread