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

  • Home
  • SEARCH
  • 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 8848789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:29:30+00:00 2026-06-14T12:29:30+00:00

I have this number in hex string: 002A05. I need to set 7-th bit

  • 0

I have this number in hex string:

002A05.

I need to set 7-th bit of this number to 1, so after conversion I will get

022A05

But it has to work with every 6 chars hex number.

I tried converting hex string to integer via strtol, but that function strip leading zeros.

Please help me how can I solve it.

  • 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-14T12:29:31+00:00Added an answer on June 14, 2026 at 12:29 pm

    In a 24-bit number bit #7 (counting from the left, as you did in your example, not from the right, as is done conventionally) is always going to be in the second byte from the left. You can solve your problem without converting the entire number to integer by taking that second hex digit, converting it to a number 0..15, setting its bit #3 (again counting from the left), and converting the result back to a hex digit.

    int fromHex(char c) {
        c = toupper(c);
        if (c >= '0' && c <= '9') {
            return c-'0';
        } else {
            return c-'A'+10;
        }
    }
    char toHexDigit(int n) {
        return n < 10 ? '0'+n : 'A'+n-10;
    }
    
    char myNum[] = "002A05";
    myNum[1] = toHexDigit(fromHex(myNum[1]) | 2);
    printf("%s\n", myNum);
    

    This prints ‘022A05’ (link to ideone).

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

Sidebar

Related Questions

I have this generic string to number conversion : enum STRING_BASE : signed int
I have a hex string that represents a 2's complement number. Is there an
I have a 128-bit number in hexadecimal stored in a string (from md5, security
I Wrote a small php script to convert string to HEX but i have
Hello I have a string parser who receives as its input a hex number:
Possible Duplicate: Change bit of hex number with leading zeros in C++,(C) I have
I have a hex string which looks like: String hexImage =0xFFD8FFE000104A46494600010200006400640000FFEC00114475636B79000100040000003C... Which I need
I have this code: # Compare phone number phone_pattern = '^\d{3} ?\d{3}-\d{4}$' phoneNumber =
I have this code which adds a x number of views to a Multiview
I have this regex that checks for a 5 digit number ^\d{5}$ How do

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.