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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:41:15+00:00 2026-05-31T06:41:15+00:00

I have a problem understanding and using the ‘vec’ keyword. I am reading a

  • 0

I have a problem understanding and using the ‘vec’ keyword.

I am reading a logpacket in which values are stored in little endian hexadecimal. In my code, I have to unpack the different bytes into scalars using the unpack keyword.

Here’s an example of my problem:

my @hexData1 = qw(50 65);
my $data = pack ('C*', @hexData1);
my $x = unpack("H4",$data);    # At which point the hexadecimal number became a number 
print $x."\n";
#my $foo = sprintf("%x", $foo);

print "$_-> " . vec("\x65\x50", $_, 1) . ", " for (0..15);    # This works.
print "\n";

But I want to use the above statement in the way below. I don’t want to send a string of hexadecimal in quotes. I want to use the scalar array of hex $x. But it won’t work. How do I convert my $x to a hexadecimal string. This is my requirement.

print "$_-> " . vec($x, $_, 1).", " for (0..15);    # This doesn't work.
print "\n";

My final objective is to read the third bit from the right of the two byte hexadecimal number.

How do I use the ‘vec’ command for that?

  • 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-31T06:41:17+00:00Added an answer on May 31, 2026 at 6:41 am

    You are making the mistake of unpacking $data into $x before using it in a call to vec. vec expects a string, so if you supply a number it will be converted to a string before being used. Here’s your code

    my @hexData1 = qw(50 65);
    my $data= pack ('C*', @hexData1);
    

    The C pack format uses each value in the source list as a character code. It is the same as calling chr on each value and concatenating them. Unfortunately your values look like decimal, so you are getting chr(50).chr(65) or "2A". Since your values are little-endian, what you want is chr(0x65).chr(0x50) or "\x65\x50", so you must write

    my $data= pack ('(H2)*', reverse @hexData1);
    

    which reverses the list of data (to account for it being little-endian) and packs it as if it was a list of two-digit hex strings (which, fortunately, it is).

    Now you have done enough. As I say, vec expects a string so you can write

    print join ' ', map vec($data, $_, 1), 0 .. 15;
    print "\n";
    

    and it will show you the bits you expect. To extract the the 3rd bit from the right (assuming you mean bit 13, where the last bit is bit 15) you want

    print vec $data, 13, 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem understanding how to create a child class's contructor, which
I think I have a problem in understanding the proper way of using MVC.
I have a problem understanding why shared_ptr is using atomic cpu instructions... I cant
I am started using MongoDB few days ago, and i have problem in understanding
I have a little trouble understanding scope and lifetime using javaScript and jQuery. I
I seem to have a problem understanding how to conditionally test a boolean value
I have an understanding problem of how the following code works: XMLInputFactory xif =
There are many skills a programmer could have (understanding the problem, asking good questions,
I have a problem understanding what's causes the compilation error in the code below:
Possible Duplicate: Using IoC for Unit Testing I think I do have a Problem

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.