String.unpack - signed 4byte little-endian integer?
-
@thomthom said:
How does one read a signed 4byte little-endian integer in ruby?
I ran into problem when I used "l" - since it uses native format. It worked on Windows and my Intel Mac 10.5, but not on my PPC Mac 10.4.
Is the (-K parameter) / $KCODE set to UTF8 on the PPC Mac?
Did you try using
'l_'
as a format string ? -
@dan rathbun said:
Is the (-K parameter) / $KCODE set to UTF8 on the PPC Mac?
hmm... don't know. But what does UTF8 got to do with this?
@dan rathbun said:
Did you try using 'l_' as a format string ?
No, because from what I read, that might cause it not to read four bytes.
@unknownuser said:
The directives sSiIlL may each be followed by an underscore (``_ββ) to use the underlying platformβs native size for the specified type; otherwise, it uses a platform-independent consistent size.
@unknownuser said:
treat four (modified by _) successive characters as an unsigned native long integer
With just
"l"
, this should always read four bytea, right? So the problems seems that on PC and Intel Mac it read with little endian, which is what I want, but on PPC Mac it is read with big endian. And the"_"
doesn't appear to affect that. -
Can one from ruby find out what the native endian is?
-
@thomthom said:
Can one from ruby find out what the native endian is?
Don't know, but this page:
http://developer.apple.com/mac/library/documentation/DeveloperTools/gcc-4.0.1/gcc/ARM-Options.html
seems to suggest that the processor itself can run in "an endian mode". (I've no idea how you'd change such a setting. Do PPC Macs have BIOS setup like a Intel PC?)Here's a PDF on the Apple Carbon "Core Endian Reference". (C function calls.)
http://developer.apple.com/mac/library/documentation/Carbon/Reference/CoreEndianReference/CoreEndianReference.pdf -
@thomthom said:
Can one from ruby find out what the native endian is?
OK.. here's the answer I think (although you'd need to write a Ruby wrapper in C.)
Byte-Order Utilities Reference
http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFByteOrderUtils/Reference/reference.html@unknownuser said:
A platform stores values either in big-endian or little-endian format. On big-endian machines, such as PowerPC machines, values are stored with the most-significant bytes first in memory; on little-endian machines, such as Pentium machines, values are stored with the least-significant bytes first. A multibyte value transmitted to a platform with a different format will be misinterpreted if it is not converted properly by one of the computers.
You identify the native format of the current platform using the CFByteOrderGetCurrent function. Use functions such as CFSwapInt32BigToHost and CFConvertFloat32HostToSwapped to convert values between different byte order formats.
-
Thanks for that Dan. That confirmed my suspicion.
Can't one rearrange the bits using ruby? If one use the RUBY_PLATFORM constant to determine if the host is a PPC, when swap the bits with ruby avoiding the need to C dependencies?
-
@thomthom said:
Can't one rearrange the bits using ruby?
Sure .. remember that Color Integer Conversion code I did awhile back.
That was just swapping bits around. -
I need to revisit that. Make some
le2be
andbe2le
methods. -
@thomthom said:
Can't one rearrange the bits using ruby? If one use the RUBY_PLATFORM constant to determine if the host is a PPC, when swap the bits with ruby avoiding the need to C dependencies?
Mucking with bits in Ruby is very easy (though performance?). In VisMap I fiddled with bits in JavaScript to encode the string sent to Ruby and in reverse in the Ruby to unencode. Wrote about that in http://www.MartinRinehart.com/models/tutorial/tutorial_bfr.htmlAppendix BFR[/url].
I apologize for the code. It dates back to when I was first writing Ruby, trying to do things the Ruby way.
-
@dan rathbun said:
@thomthom said:
Can one from ruby find out what the native endian is?
... this page:
http://developer.apple.com/mac/library/documentation/DeveloperTools/gcc-4.0.1/gcc/ARM-Options.html
seems to suggest that the processor itself can run in "an endian mode".More info...
@unknownuser said:(http://en.wikipedia.org/wiki/Endian#Bi-endian_hardware)":2fp65ya5]Some architectures (including ARM, PowerPC, Alpha, SPARC V9, MIPS, PA-RISC and IA-64) feature switchable endianness. This feature can improve performance or simplify the logic of networking devices and software. The word bi-endian, said of hardware, denotes the capability to compute or pass data in either of two different endian formats.
...
Note, too, that some nominally bi-endian CPUs may actually employ internal "magic" (as opposed to really switching to a different endianness) in one of their operating modes. For instance, some PowerPC processors in little-endian mode act as little-endian from the point of view of the executing programs but they do not actually store data in memory in little-endian format (multi-byte values are swapped during memory load/store operations). -
@thomthom said:
I need to revisit that. Make some
le2be
andbe2le
methods.Hmmm... looking at Array.pack it says "network byte order" is big-endian.
Would something like this work? (you'd need to apply your platform test beforehand)
# singleton method for String def myStr.swapEndian_to_i( long=true, signed=true ) arr=[] self.each_byte {|byte| arr.unshift(byte) } str=arr.join if signed return ( long ? str.unpack('l') ; str.unpack('s') ) else # unsigned return ( long ? str.unpack('L') ; str.unpack('S') ) end end
Further Reading:
Understanding Big and Little Endian Byte Order
Blog: How to convert an integer to little endian or big endian
Apple Dev: Byte Swapping Integers
Endian FAQ -
Good stuff Dan - thanks for looking into this. I'll look into this as soon as I can. (Just need to get Vertex Tools out the window)
-
@thomthom said:
Can one from ruby find out what the native endian is?
from the source:
@unknownuser said:
- ... use WORDS_BIGENDIAN to detect the platform's endian.
WORDS_BIGENDIAN is set in defines.h (line 163,) IF the compiler had defined BIG_ENDIAN (otherwise LITTLE_ENDIAN would have been defined.)
Have a look at pack.c begining line 182:
There is a C function declared called endian() that returns 0|1
.. you'll see how in that block between 182 and 280 how #ifdef DYNAMIC_ENDIAN and #ifdef WORDS_BIGENDIAN are used with the endian() function to control how values are converted.
The endian() function only gets declared if DYNAMIC_ENDIAN is defined.But you could do something similar in
#include 'ruby.h' #include 'defines.h' #ifdef WORDS_BIGENDIAN /* define a ruby constant that returns true */ #else /* define a ruby constant that returns false */ #endif
-
Another idea, since on the Mac you have a full Ruby install..
is to see what the compiler -archflags were when Ruby was built for the PPC.The
Config::CONFIG
hash contains alot of info:
At console try:
Config::CONFIG['ARCH_FLAG']
or just open the
rbconfig.rb
file in the arch_sub_folder (ppc-darwinX.X) of the ruby lib folder and manually browse the whole hash for possible info. -
TT,
Just posted a Platform module in the SKX forum that has some Pure Ruby Endianess hacks.
http://forums.sketchucation.com/viewtopic.php?f=315&t=29132#p253630 -
That sounds good.
I still haven't been able to look at this. Trying to crunch out Vertex Tools.
Advertisement