Hexadecimais C#
Hexadecimais C#
Page 1 of 4
Example
This example outputs the hexadecimal value of each character in a string. First it parses the
string to an array of characters. Then it calls ToInt32(Char)6 on each character to obtain its
numeric value. Finally, it formats the number as its hexadecimal representation in a string.
C#
This example parses a string of hexadecimal values and outputs the character corresponding to
7
each hexadecimal value. First it calls the Split(Char[]) method to obtain each hexadecimal value
as an individual string in an array. Then it calls ToInt32(String, Int32)8 to convert the
3
hexadecimal value to a decimal value represented as an int . It shows two different ways to
obtain the character corresponding to that character code. The first technique uses
http://msdn.microsoft.com/en-us/library/bb311038(d=printer).aspx 24/3/2011
How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming G... Page 2 of 4
9
ConvertFromUtf32(Int32) , which returns the character corresponding to the integer argument as
2
a string. The second technique explicitly casts the int to a char .
C#
This example shows another way to convert a hexadecimal string to an integer, by calling the
Parse(String, NumberStyles)10 method.
C#
4
The following example shows how to convert a hexadecimal string to a float by using the
11 12
System.BitConverter class and the Int32.Parse method.
C#
http://msdn.microsoft.com/en-us/library/bb311038(d=printer).aspx 24/3/2011
How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming G... Page 3 of 4
// Output: 200.0056
5
The following example shows how to convert a byte array to a hexadecimal string by using the
System.BitConverter11 class.
C#
/*Output:
01-AA-B1-DC-10-DD
01AAB1DC10DD
*/
See Also
Tasks
13
How to: Determine Whether a String Represents a Numeric Value (C# Programming Guide)
Reference
14
Types (C# Programming Guide)
Concepts
15
Standard Numeric Format Strings
Links Table
1
http://msdn.microsoft.com/en-us/library/362314fe.aspx
2
http://msdn.microsoft.com/en-us/library/x9h8tsay.aspx
3
http://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx
4
http://msdn.microsoft.com/en-us/library/b1e65aza.aspx
5
http://msdn.microsoft.com/en-us/library/5bdb6693.aspx
6
http://msdn.microsoft.com/en-us/library/ww9t2871.aspx
7
http://msdn.microsoft.com/en-us/library/b873y76a.aspx
8
http://msdn.microsoft.com/en-us/library/1k20k614.aspx
9
http://msdn.microsoft.com/en-us/library/system.char.convertfromutf32.aspx
http://msdn.microsoft.com/en-us/library/bb311038(d=printer).aspx 24/3/2011
How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming G... Page 4 of 4
10
http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx
11
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx
12
http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx
13
http://msdn.microsoft.com/en-us/library/bb384043.aspx
14
http://msdn.microsoft.com/en-us/library/ms173104.aspx
15
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
Community Content
http://msdn.microsoft.com/en-us/library/bb311038(d=printer).aspx 24/3/2011