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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:46:26+00:00 2026-06-18T23:46:26+00:00

i have a string hex value, and i need to express it in 2’s

  • 0

i have a string hex value, and i need to express it in 2’s complement.

string hx = "FF00";

what i did is, converting it to binary:

 string h = Convert.ToString(Convert.ToInt32(hx, 16), 2 );

then inverting it, but i couldn’t use the NOT operator.

is there any short way to invert the bits and then adding 1 (2’s complement operation)?

  • 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-18T23:46:27+00:00Added an answer on June 18, 2026 at 11:46 pm

    The answer might depend on whether or not the bit width of the value is important to you.

    The short answer is:

    string hx = "FF00";
    uint intVal = Convert.ToUInt32(hx, 16);      // intVal == 65280
    uint twosComp = ~v + 1;                      // twosComp == 4294902016
    string h = string.Format("{0:X}", twosComp); // h == "FFFF0100"
    

    The value of h is then “FFFF0100” which is the 32-bit 2’s complement of hx. If you were expecting ‘100’ then you need to use 16-bit calculations:

    string hx = "FF00";
    ushort intVal = Convert.ToUInt16(hx, 16);    // intVal = 65280
    ushort twosComp = (ushort)(~v + 1);          // twosComp = 256
    string h = string.Format("{0:X}", twosComp); // h = "100"
    

    Bear in mind that uint is an alias for UInt32 and ushort aliases the UInt16 type. For clarity in this type of operation you’d probably be better off using the explicit names.

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

Sidebar

Related Questions

I have a string x that I need to convert into a hex format:
I have the need to convert a hex string into an integer using C#.
Question: I need to convert a string into hex and then format the hex
i have a string, like this: '<indirizzo>Via Universit\E0 4</indirizzo>' Whit HEX char... i need
I have to parse a String that can assume hex values or other non-hex
I have a string representation of a MD5 hex digest for a file, that
Let's say that I have a string 5a . This is the hex representation
I have two strings String s1=426F62; String s2=457665; The strings are in hex representation.
I have string that displays UTF-8 encoded characters, and I want to convert it
I have string as abvd.qweqw.sdfs.a=aqwrwewrwerrew . I need to parse this string and get

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.