简体   繁体   中英

Copy string (hexadecimal format) to ulong in c#

I need to copy elements of a string which are in hexadecimal format to ulong.

Example:

string s = "0x4E45565251554954";     // already in hex format

ulong t;     // this ulong should be t=0x4E45565251554954.

Or:

string s = "0x4E45565251554954";
ulong t = Convert.ToUInt64(s, 16);

Here's some pointers... I'm afraid the conversion isn't as simple as you might hope...

Taken from here.

ie is it a signed or unsigned value?

"When performing binary operations or numeric conversions, it is always the responsibility of the developer to verify that a method or operator is using the appropriate numeric representation to interpret a particular value. The following example illustrates one technique for ensuring that the method does not inappropriately use binary representation to interpret a value that uses two's complement representation when converting a hexadecimal string to a UInt64 value. The example determines whether a value represents a signed or an unsigned integer while it is converting that value to its string representation. When the example converts the value to a UInt64 value, it checks whether the original value was a signed integer. If so, and if its high-order bit is set (which indicates that the original value was negative), the method throws an exception. "

// Create a negative hexadecimal value out of range of the UInt64 type. 
long sourceNumber = Int64.MinValue;
bool isSigned = Math.Sign((long)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1;
string value = Convert.ToString(sourceNumber, 16);
UInt64 targetNumber;
try
{
   targetNumber = Convert.ToUInt64(value, 16);
   if (isSigned && ((targetNumber & 0x8000000000000000) != 0))
      throw new OverflowException();
   else 
      Console.WriteLine("0x{0} converts to {1}.", value, targetNumber);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to an unsigned long integer.", 
                 value);
} 

// Displays the following to the console: 
//    Unable to convert '0x8000000000000000' to an unsigned long integer.   

The following example attempts to interpret each element in an array of numeric strings as a hexadecimal value and to convert it to an unsigned long integer.

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "80000000", "0FFFFFFF", "F0000000", "00A3000", "D", 
                          "-13", "9AC61", "GAD", "FFFFFFFFFF" };

      foreach (string hexString in hexStrings)
      {
         Console.Write("{0,-12}  -->  ", hexString);
         try {
            uint number = Convert.ToUInt32(hexString, 16);
            Console.WriteLine("{0,18:N0}", number);
         }
         catch (FormatException) {
            Console.WriteLine("{0,18}", "Bad Format");
         }   
         catch (OverflowException)
         {
            Console.WriteLine("{0,18}", "Numeric Overflow");
         }   
         catch (ArgumentException) {
            Console.WriteLine("{0,18}", "Invalid in Base 16");
         }
      }                                            
   }
}
// The example displays the following output: 
//       80000000      -->       2,147,483,648 
//       0FFFFFFF      -->         268,435,455 
//       F0000000      -->       4,026,531,840 
//       00A3000       -->             667,648 
//       D             -->                  13 
//       -13           -->  Invalid in Base 16 
//       9AC61         -->             633,953 
//       GAD           -->          Bad Format 
//       FFFFFFFFFF    -->    Numeric Overflow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM