Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: (tye)Re: Detecting machine Endian-ness

by LunaticLeo (Scribe)
on Aug 07, 2001 at 02:58 UTC ( #102643=note: print w/replies, xml ) Need Help??


in reply to (tye)Re: Detecting machine Endian-ness
in thread Detecting machine Endian-ness

Is substr() offset always in bytes?

Note that the "I" format can result in more than 4 bytes on some platforms.

Yeah, I suppose you could use 'L' then.

I suppose you could figure out the size of integers with code like:

@b = unpack('C*', pack('I', 0)); return scalar(@b);

Then you could make the endian code more generic with:

@b = unpack('C*', pack('I', 0)); $sizeof_long = scalar(@b); @c = (1..$sizeof_long); $i = pack('I', hex('0x0'.join('0',reverse @c))); $big = pack('C'.$sizeof_long, reverse @c); $lit = pack('C'.$sizeof_long, @); if ( substr($i, 0, $sizeof_long) eq $big ) { return 'big'; } elsif ( substr($i, 0, $sizeof_long) eq $lit ) { return 'little'; } else { return "strange"; }

Replies are listed 'Best First'.
(tye)Re2: Detecting machine Endian-ness
by tye (Sage) on Aug 07, 2001 at 07:48 UTC

    Good point. I don't do anything with "wide" strings in Perl yet so I usually forget to take that into consideration. But here we are making the strings so we know they'll be byte strings and we can use what I wrote or: length pack "I", 0
    to find the number of bytes in an IV.

    I thought I had used a "native 32-bit integer" before for endianness testing, but for some reason couldn't think of "L". That certainly makes more sense than resorting to substr. (:

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://102643]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2017-09-25 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (277 votes). Check out past polls.

    Notices?