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

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://102643]
[Corion]: Hmm. I think overnight I decided on simplifying some code. I have plugin classes that do data import (.csv, .yml, .json) and for that create objects on which then ->load() is called. But YAML::XS doesn't have an object, so I wrote my own wrapper.
[Corion]: This evening, I'll kill that wrapper again, and just call LoadFile() in the plugin class directly instead of creating a go-between object for no real gain.
[Corion]: Writing these import plugins was really nice though - in about 2 hours, I had imports for CSV, YAML and JSON, and adding XLS(X), SQLite (or DBI) data sources is also trivial. I'm idly wondering about separating the plugin into transport+parser, so ...
[Corion]: ... http:// URLs could be retrieved and then parsed, but I think that that would be overkill for a toy static site generator ;)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-05-22 11:01 GMT
Find Nodes?
    Voting Booth?