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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:20:53+00:00 2026-06-14T21:20:53+00:00

I need to extract certain bits of a byte and covert the extract bits

  • 0

I need to extract certain bits of a byte and covert the extract bits back to a hex value.

Example (the value of the byte is 0xD2) :

76543210 bit position
11010010 is 0xD2
  • Bit 0-3 defines the channel which is 0010b is 0x2
  • Bit 4-5 defines the controller which is 01b is 0x1
  • Bit 6-7 defines the port which is 11b is 0x3

I somehow need to get from the byte is 0xD2 to channel is 0x2, controller is 0x1, port is 0x3

I googled allot and found the functions pack/unpack, vec and sprintf. But I’m scratching by head how to use the functions to achieve this. Any idea how to achieve this 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-06-14T21:20:55+00:00Added an answer on June 14, 2026 at 9:20 pm

    What’s the initial format?

    my $chr = chr(0b11010010);  # A character  e.g. from read()
    my $bin = '11010010';       # Binary
    my $hex = 'D2';             # Hexadecimal
    my $num = 0b11010010;       # A number.
    my $num = 0xD2;             # A number.
    

    You want to start by converting it to a number

    my $num = ord($chr);
    my $num = unpack('C', $chr);  # Alternative
    my $num = oct("0b$bin");
    my $num = hex($hex);
    

    Then you use shifts and masks.

    my $channel    = ($num >> 0) & 0xF;   # Or just: $num & 0xF
    my $controller = ($num >> 4) & 0x3;
    my $port       = ($num >> 6) & 0x3;   # Or just: $num >> 6
    

    (You could use 0b1111, 0b11 and 0b11 for the masks. Most people work in hex.)

    Or let vec figure out the masks for you.

    my $channel    = vec $num, 0, 4;
    my $controller = vec $num, 4, 2;
    my $port       = vec $num, 6, 2;
    

    Here’s an example for $controller:

      11010010
          >> 4
      --------
          11010010
    &       11
      --------
            01
    

    (Some zeroes omitted for clarity.)

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

Sidebar

Related Questions

I need some help with twitter hashtag, I need to extract a certain hashtag
I need to extract a value from a hidden HTML field, have somewhat figured
I need to extract certain data from a file, but this file is formatted
I need to get a SQL Query that I can execute to extract certain
I've a long template from which I need to extract certain strings based on
I hope someone can help me. I need to extract certain details (Full Name
i am no RegEx expert. I need to extract a certain number out of
I need to extract frames in certain interval of the video, (eg from 40
I am using opencv for a certain image processing code. I need to extract
Okay so I have database and I need to extract only certain rows that

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.