Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi. I am a fairly experienced Perl amateur, or so I thought. I've been whipping things up in Perl for some 10+ years, but I've never gotten very deep down into the nuts and bolts until fairly recently. Despite being a onetime computer science major as an undergrad (before turning the dark side and becoming a historian), I struggle with computer-sciencey concepts, so that might be part of my problem here.

Anyway, I am trying to learn about pack and unpack and bitwise operations and such. And I have run into some behavior that seems very odd and I can't explain it. I am using the latest release of ActivePerl on a Win32 (WinXP) system.

use strict; use warnings 'all'; my $i = 0; # This should be zero! I want 0b00000000! my $b = unpack("B*", $i); print $b;

The output I get is:

00110000

In other words, what I am getting instead of zero (i.e. 0b00000000, 0x00, NUL, the real zero) is 0b00110000, or 0x48 -- the ASCII character 0. This isn't what I want. It doesn't matter how I write the number. Whether I code $i = 0 or 0x00 or 0b00000000 -- I keep getting the character 0.

If, on the other hand, I code:

printf("%08b", 0);

I properly get:

00000000

Why is this happening? Why is unpack interpreting my 0 as a character or string? Is this the proper behavior for Perl, or is it some quirk or bug of this distribution or system? Or am I just doing something wrong, or misunderstanding what is happening here? Is there any way to stop it?


In reply to ActivePerl on Win32 storing integers as strings? by LonelyPilgrim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found