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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:47:38+00:00 2026-06-06T04:47:38+00:00

I’m working on some code that takes a hexadecimal string input and produces the

  • 0

I’m working on some code that takes a hexadecimal string input and produces the binary value of that input. Once I have the value of the hexadecimal string in binary, I need to access individual bits of that byte.

I can’t say exactly what I’m working on, but I can say this: The hex characters represent byte value of a given selection of registers on some hardware – which gives us the hex string as it’s output after it has finished crunching some numbers.

As a made up example, the output value “A2” (10100010) will mean that the registers (again made up) will have the following values:

RegA  RegB  RegC  RegD
 101     0   001     0

I need to access N number of bits within the byte values that are returned. Except I’ve hit a bump in the road.

So far, I’ve tried the following:

string inputString = "F";
byte[] byteValues = new byte[inputString.Length * sizeof(char)];
System.Buffer.BlockCopy(inputString.ToCharArray(), 0, byteValues,
                        0, byteValues.Length);
return byteValues;

However, when given the input string “F”, this code returns a byte array with 4 elements. The 1st element has the value 70 in it and the rest have 0. Which is the ASCII key for capital F – not what I want.

I have also tried:

int tempInt = Convert.ToInt32("F", 16);
byte[] value = BitConverter.GetBytes(tempInt);

When given the input string “F”, this code returns a byte array with 4 elements. The 1st element has the value 15 in it and the rest have 0. This is more like it, but I now need to access individual bits within this 1st element of the byte array.

Is there a programmatic way to get individual bit information, other than providing a method that will take a hex character in, switch on it and return a 4 element byte[] with the correct bit values for that hex character?

This is what I mean:

public byte[] getByteValueForString (string inputString)
{
  /* is there a better way than this? */
  switch(inputString)
  {
     case "0":
       return new byte[] {0, 0, 0, 0};
     //....
     case "E":
       return new byte[] {1, 1, 1, 0};
     case "F":
       return new byte[] {1, 1, 1, 1};
  }
}

//or a similar method that switches on the output
//of Convert.ToInt32(string, 16) and returns the a
//byte[] in the same manner as getByteValueForString

public bool bar ()
{
  /* check the value for some representation
   * of a register */
  if (fooByteArray[2] == 0 & fooByteArray[3] == 0)
  {
    //some register, on the remote machine, is set to false
    return false;
  }
}

Any help with this would be greatly appreciated.

  • 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-06T04:47:39+00:00Added an answer on June 6, 2026 at 4:47 am

    I’d personally just use a single byte for this, rather than splitting 4 bits across 4 bytes. At that point you can just use:

    byte b = Convert.ToByte(text, 16);
    

    If you really want 4 bytes, you could use:

    // Note: name changed to comply with .NET conventions
    static byte[] GetByteValuesForString(string text)
    {
        // TODO: Consider what you want to happen for invalid input.
        // You can easily write your own equivalent, ideally for a
        // single char
        byte value = Convert.ToByte(text, 16);
        return new byte[] {
            (byte) (value >> 3) & 1, 
            (byte) (value >> 2) & 1,
            (byte) (value >> 1) & 1,
            (byte) (value >> 0) & 1
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT

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.