String.unpack - signed 4byte little-endian integer?
-
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