Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1808704
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:17:43+00:00 2026-05-17T06:17:43+00:00

In Perl, pack and unpack have two templates for converting bytes to/from hex: h

  • 0

In Perl, pack and unpack have two templates for converting bytes to/from hex:

h    A hex string (low nybble first).
H    A hex string (high nybble first).

This is best clarified with an example:

use 5.010; # so I can use say
my $buf = "\x12\x34\x56\x78";

say unpack('H*', $buf); # prints 12345678
say unpack('h*', $buf); # prints 21436587

As you can see, H is what people generally mean when they think about converting bytes to/from hexadecimal. So what’s the purpose of h? Larry must have thought someone might use it, or he wouldn’t have bothered to include it.

Can you give a real-world example where you’d actually want to use h instead of H with pack or unpack? I’m looking for a specific example; if you know of a machine that organized its bytes like that, what was it, and can you link to some documentation on it?

I can think of examples where you could use h, such as serializing some data when you don’t really care what the format is, as long as you can read it back, but H would be just as useful for that. I’m looking for an example where h is more useful than H.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:17:43+00:00Added an answer on May 17, 2026 at 6:17 am

    Recall in the bad ‘ole days of MS-DOS that certain OS functions were controlled by setting high nibble and low nibbles on a register and performing an Interupt xx. For example, Int 21 accessed many file functions. You would set the high nibble as the drive number — who will have more than 15 drives?? The low nibble as the requested function on that drive, etc.

    Here is some old CPAN code that uses pack as you describe to set the registers to perform an MS-DOS system call.

    Blech!!! I don’t miss MS-DOS at all…

    –Edit

    Here is specific source code: Download Perl 5.00402 for DOS HERE, unzip,

    In file Opcode.pm and Opcode.pl you see the use of unpack("h*",$_[0]); here:

    sub opset_to_hex ($) {
        return "(invalid opset)" unless verify_opset($_[0]);
        unpack("h*",$_[0]);
    }
    

    I did not follow the code all the way through, but my suspicion is this is to recover info from an MS-DOS system call…

    In perlport for Perl 5.8-8, you have these suggested tests for endianess of the target:

    Different CPUs store integers and floating point numbers in different
    orders (called endianness) and widths (32-bit and 64-bit being the
    most common today). This affects your programs when they attempt to transfer
    numbers in binary format from one CPU architecture to another,
    usually either “live” via network connection, or by storing the
    numbers to secondary storage such as a disk file or tape.

    Conflicting storage orders make utter mess out of the numbers. If a
    little-endian host (Intel, VAX) stores 0x12345678 (305419896 in
    decimal), a big-endian host (Motorola, Sparc, PA) reads it as
    0x78563412 (2018915346 in decimal). Alpha and MIPS can be either:
    Digital/Compaq used/uses them in little-endian mode; SGI/Cray uses
    them in big-endian mode. To avoid this problem in network (socket)
    connections use the pack and unpack formats n and N, the
    “network” orders. These are guaranteed to be portable.

    As of perl 5.8.5, you can also use the > and < modifiers
    to force big- or little-endian byte-order. This is useful if you want
    to store signed integers or 64-bit integers, for example.

    You can explore the endianness of your platform by unpacking a
    data structure packed in native format such as:

       print unpack("h*", pack("s2", 1, 2)), "\n";
       # '10002000' on e.g. Intel x86 or Alpha 21064 in little-endian mode
       # '00100020' on e.g. Motorola 68040
    

    If you need to distinguish between endian architectures you could use
    either of the variables set like so:

       $is_big_endian    = unpack("h*", pack("s", 1)) =~ /01/;
       $is_little_endian = unpack("h*", pack("s", 1)) =~ /^1/;
    

    Differing widths can cause truncation even between platforms of equal
    endianness. The platform of shorter width loses the upper parts of the
    number. There is no good solution for this problem except to avoid
    transferring or storing raw binary numbers.

    One can circumnavigate both these problems in two ways. Either
    transfer and store numbers always in text format, instead of raw
    binary, or else consider using modules like Data::Dumper (included in
    the standard distribution as of Perl 5.005) and Storable (included as
    of perl 5.8). Keeping all data as text significantly simplifies matters.

    The v-strings are portable only up to v2147483647 (0x7FFFFFFF), that’s
    how far EBCDIC, or more precisely UTF-EBCDIC will go.

    It seems that unpack("h*",...) is used more often than pack("h*",...). I did note that return qq'unpack("F", pack("h*", "$hex"))'; is used in Deparse.pm and IO-Compress uses pack("*h",...) in Perl 5.12

    If you want further examples, here is a Google Code Search list. You can see pack|unpack("h*"...) is fairly rare and mostly to do with determining platform endianess…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Perl program that sends and receives messages from two sockets
How can I do this Perl code in PHP? print unpack (H*, pack (B*,
In Perl, what is a good way to perform a replacement on a string
According to this calculator site ( link text ), when converting 3 from decimal
I am helping a client convert their Perl flat-file bulletin board site from ISO-8859-1
Perl allows you to use the '..' operator to return a slice from an
Perl: How can I sort a complex structure using JSON::PP ? From the JSON
In Perl if I use this regex /(\w+)\.(\w+)/ on the string 1A3.25D , the
#!/usr/bin/env perl use warnings; use 5.012; my $var = 1 << 31; say unpack(
Perl has the \u operator to lowercase a match when using string replacement and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.