sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

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

    Scheduled Pinned Locked Moved Developers' Forum
    17 Posts 3 Posters 2.5k 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

      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