Converting between a hexadecimal string and a numeric type

Source: Internet
Author: User

1. Gets the hexadecimal value of each character in the ' string '.

2. Gets the character that corresponds to each value in the hexadecimal string. 、

3. Convert the hexadecimal string to shaping.

4. Converts a hexadecimal string to a floating-point type.

5. Convert the byte array to a hexadecimal string.

Each character in this case input is a hexadecimal value. The string is parsed into a character array, and Inter32 (char) is called for each character to obtain the corresponding numeric value.

Static voidMain (string[] args) {            stringinput ="Hello world!"; Char[] values =input.            ToCharArray (); foreach(CharLetterinchvalues) {                intValue =Convert.ToInt32 (letter); stringHexoutput =string. Format ("{0:x}", value); 0 means that the placeholder X or x represents the hexadecimal 12345.ToString ("x"); Build 3039 (16 binary) Console.WriteLine ("hexadecimal value Of{0} is {1}", letter, Hexoutput); }        }

Extended:
12345.ToString ("n"); Generate 12,345.00
12345.ToString ("C"); Generate ¥12,345.00
12345.ToString ("E"); Build 1.234500e+004
12345.ToString ("F4"); Build 12345.0000
12345.ToString ("X"); Build 3039 (16 binary)
12345.ToString ("P"); Generate 1,234,500.00%

Case analysis The hexadecimal value of the string corresponds to the output corresponding to the character. The split (char[]) method is first used to get each hexadecimal as a single string in the array. Then call ToInt32 (String,int32) to convert the 16 binary to a decimal value that is represented as an int. The first method uses ConvertFromUtf32 (Int32), which returns the character corresponding to the integer as a string. The second method is to convert the int display to Char

stringhexvalues="6C 6C 6F, 6F, 6C"; string[] Hexvaluesplit=hexvalues.split (' '); foreach(stringHexinchhexvaluesplit) {                intValue=convert.toint32 (Hex, -); stringStringValue =Char.                CONVERTFROMUTF32 (value); CharCharvalue = (Char) value; Console.WriteLine ("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}", Hex, Value, StringValue, charvalue); }

The case demonstrates another way to convert a hexadecimal String to an integer, called the Int32.Parse (String,numberstyles) method

string "  - " ;             int num = int32.parse (hexstring,system.globalization.numberstyles.hexnumber);            Console.WriteLine (num);

Converting between a hexadecimal string and a numeric type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.