sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    String.unpack - signed 4byte little-endian integer?

    Scheduled Pinned Locked Moved Developers' Forum
    17 Posts 3 Posters 2.4k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      @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.

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        Can one from ruby find out what the native endian is?

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @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

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @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.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              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?

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                @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'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  I need to revisit that. Make some le2be and be2le methods.

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    MartinRinehart
                    last edited by

                    @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.

                    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      @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).

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        @thomthom said:

                        I need to revisit that. Make some le2be and be2le 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

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • thomthomT Offline
                          thomthom
                          last edited by

                          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)

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • Dan RathbunD Offline
                            Dan Rathbun
                            last edited by

                            @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
                            
                            

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • Dan RathbunD Offline
                              Dan Rathbun
                              last edited by

                              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.

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by

                                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

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • thomthomT Offline
                                  thomthom
                                  last edited by

                                  That sounds good. 👍
                                  I still haven't been able to look at this. Trying to crunch out Vertex Tools.

                                  Thomas Thomassen — SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 Reply Last reply Reply Quote 0
                                  • 1 / 1
                                  • First post
                                    Last post
                                  Buy SketchPlus
                                  Buy SUbD
                                  Buy WrapR
                                  Buy eBook
                                  Buy Modelur
                                  Buy Vertex Tools
                                  Buy SketchCuisine
                                  Buy FormFonts

                                  Advertisement