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 7435941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:09:34+00:00 2026-05-29T10:09:34+00:00

I need the Python analog for this Perl string: unpack(nNccH*, string_val) I need the

  • 0

I need the Python analog for this Perl string:

unpack("nNccH*", string_val)

I need the nNccH* – data format in Python format characters.

In Perl it unpack binary data to five variables:

  • 16 bit value in “network” (big-endian)
  • 32 bit value in “network” (big-endian)
  • Signed char (8-bit integer) value
  • Signed char (8-bit integer) value
  • Hexadecimal string, high nibble first

But I can’t do it in Python

More:

bstring = ''
while DataByte = client[0].recv(1):
    bstring += DataByte
print len(bstring)
if len(bstring):
    a, b, c, d, e = unpack("nNccH*", bstring)

I never wrote in Perl or Python, but my current task is to write a multithreading Python server that was written in Perl…

  • 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-29T10:09:35+00:00Added an answer on May 29, 2026 at 10:09 am

    The Perl format "nNcc" is equivalent to the Python format "!HLbb".
    There is no direct equivalent in Python for Perl’s "H*".

    There are two problems.

    • Python’s struct.unpack does not accept the wildcard character, *
    • Python’s struct.unpack does not “hexlify” data strings

    The first problem can be worked-around using a helper function like unpack.

    The second problem can be solved using binascii.hexlify:

    import struct
    import binascii
    
    def unpack(fmt, data):
        """
        Return struct.unpack(fmt, data) with the optional single * in fmt replaced with
        the appropriate number, given the length of data.
        """
        # http://stackoverflow.com/a/7867892/190597
        try:
            return struct.unpack(fmt, data)
        except struct.error:
            flen = struct.calcsize(fmt.replace('*', ''))
            alen = len(data)
            idx = fmt.find('*')
            before_char = fmt[idx-1]
            n = (alen-flen)//struct.calcsize(before_char)+1
            fmt = ''.join((fmt[:idx-1], str(n), before_char, fmt[idx+1:]))
            return struct.unpack(fmt, data)
    
    data = open('data').read()
    x = list(unpack("!HLbbs*", data))
    # x[-1].encode('hex') works in Python 2, but not in Python 3
    x[-1] = binascii.hexlify(x[-1])
    print(x)
    

    When tested on data produced by this Perl script:

    $line = pack("nNccH*", 1, 2, 10, 4, '1fba');
    print "$line";
    

    The Python script yields

    [1, 2, 10, 4, '1fba']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python I need to delete all characters in a multiline string up to
Using Python I need to insert a newline character into a string every 64
I need an IronPython\Python example that would show C#/VB.NET developers how awesome this language
In Python I need to get the version of an external binary I need
[Python] I need to make the first 3 variables (attack, hitpoints, money) global but
I need a Python implementation of this function - I want to use it
I need a Python library that supports PEM files and both RSA signing and
Sometimes (in customer's PCs) I need a python script to execute in the Windows
A python script need to spawn multiple sub-processes via fork(). All of those child
In Python I need to efficiently and generically test whether an attribute of a

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.