C # Implementation of code _c# between 16 and string conversions Tutorial

Source: Internet
Author: User
Tags numeric value

converting between hexadecimal strings and numeric types (C # Programming Guide)
The following example shows how to perform the following tasks:
Gets the hexadecimal value of each character in the string.
Gets the character that corresponds to each value in the hexadecimal string.
Converts a hexadecimal string to an integral type.
Converts a hexadecimal string to a floating-point type.
Converts a byte array to a hexadecimal string.
Example
This example prints the hexadecimal value of each character in string. First, it analyzes string as a character array, and then calls ToInt32 (Char) on each character to get the corresponding numeric value. Finally, the format of the number in string is formatted as a hexadecimal representation.
C#
string input = "Hello world!";
Char[] values = input. ToCharArray ();
foreach (char letter in values)
{
Get the integral value of the character.
int value = Convert.ToInt32 (letter);
Convert the decimal value to a hexadecimal value in string form.
String hexoutput = String.Format ("{0:x}", value);
Console.WriteLine ("Hexadecimal value of {0} is {1}", letter, Hexoutput);
}
/* Output:
Hexadecimal value of H is 48
Hexadecimal value of E is 65
Hexadecimal value of L is 6C
Hexadecimal value of L is 6C
Hexadecimal value of O is 6F
Hexadecimal value is 20
Hexadecimal value of W is 57
Hexadecimal value of O is 6F
Hexadecimal value of R is 72
Hexadecimal value of L is 6C
Hexadecimal value of D is 64
Hexadecimal value of! is 21
*/
This example analyzes a string of hexadecimal values and outputs a character corresponding to each hexadecimal value. First, it calls the Split (array<char>[] []) method to get each hexadecimal value as a single string in the array. The ToInt32 (String, Int32) is then invoked to convert the 16 binary to a decimal value represented as int. The example shows two different ways to get the characters that correspond to the character code. The first method is to use CONVERTFROMUTF32 (INT32), which returns the character corresponding to the integer parameter as a string. The second method is to explicitly convert int to char.
C#
String hexvalues = "6C 6C 6F 6C 6F 64 21";
string[] Hexvaluessplit = Hexvalues.split (");
foreach (String hex in Hexvaluessplit)
{
Convert the number expressed in base-16 to an integer.
int value = Convert.ToInt32 (hex, 16);
Get the character corresponding to the integral value.
String stringvalue = Char.convertfromutf32 (value);
Char charvalue = (char) value;
Console.WriteLine ("Hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
Hex, value, StringValue, charvalue);
}
/* Output:
Hexadecimal value = +, int value =, char value = h or H
Hexadecimal value = +, int value = d, char value = e or E
Hexadecimal value = 6C, int value = 108, char value = L or L
Hexadecimal value = 6C, int value = 108, char value = L or L
Hexadecimal value = 6F, int value = A, char value = O or O
hexadecimal value =, int value =, char value = or
Hexadecimal value = m, int value =, char value = W or W
Hexadecimal value = 6F, int value = A, char value = O or O
Hexadecimal value = A, int value = 114, char value = R or R
Hexadecimal value = 6C, int value = 108, char value = L or L
Hexadecimal value = $, int value = m, char value = d or D
hexadecimal value =, int value =, char value =! or!
*/
This example demonstrates another way to convert a hexadecimal string to an integer by calling the Parse (string, NumberStyles) method.
C#
String hexstring = "8E2";
int num = Int32.Parse (hexstring, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine (num);
output:2274
The following example shows how to use System..::. Bitconverter class and Int32..::. The Parse method converts a hexadecimal string to a floating-point type.
C#
String hexstring = "43480170";
UINT num = uint. Parse (hexstring, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] floatvals = bitconverter.getbytes (num);
float f = bitconverter.tosingle (floatvals, 0);
Console.WriteLine ("Float convert = {0}", f);
output:200.0056
The following example shows how to use System..::. The Bitconverter class converts a byte array to a hexadecimal string.
C#
Byte[] Vals = {0x01, 0xAA, 0xb1, 0xDC, 0x10, 0xDD};
String str = bitconverter.tostring (vals);
Console.WriteLine (str);
str = bitconverter.tostring (vals). Replace ("-", "");
Console.WriteLine (str);
/*output:
01-aa-b1-dc-10-dd
01aab1dc10dd
*/
Just the piracy on MSDN!

Copy Code code as follows:

public string Strtohex (String mstr)//Returns the processed hexadecimal string
{
Return bitconverter.tostring (
ASCIIEncoding.Default.GetBytes (MSTR)). Replace ("-", "");
}/* Strtohex * *
public string Hextostr (string mhex)//Returns a hexadecimal-represented string
{
Mhex = Mhex.replace ("", "");
if (mhex.length <= 0) return "";
byte[] vbytes = new BYTE[MHEX.LENGTH/2];
for (int i = 0; i < mhex.length i + + 2)
if (!byte. TryParse (Mhex.substring (i, 2), Numberstyles.hexnumber, NULL, out VBYTES[I/2))
VBYTES[I/2] = 0;
Return ASCIIEncoding.Default.GetString (vbytes);
}/* HEXTOSTR * *

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.