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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:15:18+00:00 2026-06-07T06:15:18+00:00

I am planning to use unpack in the file. First i tested with a

  • 0

I am planning to use unpack in the file. First i tested with a string. When i have a embedded space in a string the below script is showing it as null. When i tested with a file space is getting read properly. Not sure why it’s changing it to null when i do with string. Can i use unpack while reading a fixed length file? Do i need to consider trailing space or anything else?

#!/usr/local/bin/perl

use strict;
use warnings;
my $str="hek kaaa";
print "<$str>\n";
foreach(unpack("(A1)*", $str)) {
    print sprintf("%x", ord), " ";
}

Output:
<hek kaaa>
68 65 6b 0 6b 61 61 61

Thanks a lot for the response. Pasted my query below

When the perl program reads from a text file and unpacks using the format “A” is working properly. First line i have a embedded space in the first field between A and D. LABEL Variable gets printed as “A D3”. But when i had a variable $str=”A D3″ and unpacked the below way it’s having a null after A. How is it working differently when reading from a file and variable?

foreach(unpack("(A1)*", $str)) {
    print sprintf("%x", ord), " ";
}

it displays as the hex output as

41 0 44 33

cat test.txt
A D37845566974923342XYZ24023984
QRW49327408234028434ERD24448009

 my $file = 'test.txt';

open(my $data, '<', $file) or die "Could not open '$file'\n";
while (my $line = <$data>) {
    print $line;
    chomp $line;
    my ($label, $source, $dest, $type, $value) = unpack ("A4 A8 A8 A4 A8", $line);
    print "LABEL: $label SOURCE: $source DEST: $dest TYPE: $type VALUE: $value\n";
    print "length of a string:" . length($line) . "\n";
    foreach(unpack("(a1)*", $label)) {
       print sprintf("%x", ord), " ";
    }
    print "\n";

}
  • 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-06-07T06:15:19+00:00Added an answer on June 7, 2026 at 6:15 am

    This little nugget in the pack/unpack documentation is easy to miss:

    The “a”, “A”, and “Z” types gobble just one value, but
    pack it as a string of length count, padding with nulls
    or spaces as necessary. When unpacking, “A” strips
    trailing whitespace and nulls
    , “Z” strips everything
    after the first null, and “a” returns data verbatim.

    As a workaround, you can use a a or Z in the template instead of A:

    $ perl -e 'print ord unpack("A", " ")'
    0
    
    $ perl -e 'print ord unpack("a", " ")'
    32
    
    $ perl -e 'print ord unpack("Z", " ")'
    32
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning to use SCTP for a embedded Linux project. Anybody has some
I am planning to use serialization to do clone. I have to make my
im planning to use java/jsp for web application installed on virtual web hosting space
i'm planning to use a .swf file as a header. this won't work. <img
I am planning to use XPath to query an XML file. Can you please
I am planning to use php in an embedded environment. Our current web server
Planning to use Object tag to display pdf file inline in HTML file. Is
I am planning to use jQuery in my new website. I have some questions
I'm planning to use this piece of code in my Asp.net app string strUserInputtedHashedPassword
I am planning to use Hadoop on EC2. Since we have to pay per

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.