Skip to content

Latest commit

 

History

History
57 lines (36 loc) · 4.41 KB

how-to-convert-between-hexadecimal-strings-and-numeric-types.md

File metadata and controls

57 lines (36 loc) · 4.41 KB
title description ms.date helpviewer_keywords ms.topic ms.assetid
How to convert between hexadecimal strings and numeric types
Learn how to convert between hexadecimal strings and numeric types. See code examples and view additional available resources.
07/20/2015
hexadecimal strings [C#], converting to numeric type
conversions [C#], hexadecimal strings
strings [C#], converting hexadecimal strings
hexadecimal strings [C#]
how-to
7115c49f-7d1d-40c3-8bd9-aae0cc1d46b6

How to convert between hexadecimal strings and numeric types (C# Programming Guide)

These examples show you how to perform the following tasks:

  • Obtain the hexadecimal value of each character in a string.

  • Obtain the char that corresponds to each value in a hexadecimal string.

  • Convert a hexadecimal string to an int.

  • Convert a hexadecimal string to a float.

  • Convert a byte array to a hexadecimal string.

Examples

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 xref:System.Convert.ToInt32%28System.Char%29 on each character to obtain its numeric value. Finally, it formats the number as its hexadecimal representation in a string.

[!code-csharpcsProgGuideTypes#30]

This example parses a string of hexadecimal values and outputs the character corresponding to 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 xref:System.Convert.ToInt32%28System.String%2CSystem.Int32%29 to convert the 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 xref:System.Char.ConvertFromUtf32%28System.Int32%29, which returns the character corresponding to the integer argument as a string. The second technique explicitly casts the int to a char.

[!code-csharpcsProgGuideTypes#31]

This example shows another way to convert a hexadecimal string to an integer, by calling the xref:System.Int32.Parse%28System.String%2CSystem.Globalization.NumberStyles%29 method.

[!code-csharpcsProgGuideTypes#32]

The following example shows how to convert a hexadecimal string to a float by using the xref:System.BitConverter?displayProperty=nameWithType class and the xref:System.UInt32.Parse%2A?displayProperty=nameWithType method.

[!code-csharpcsProgGuideTypes#39]

The following example shows how to convert a byte array to a hexadecimal string by using the xref:System.BitConverter?displayProperty=nameWithType class.

[!code-csharpcsProgGuideTypes#38]

The following example shows how to convert a byte array to a hexadecimal string by calling the xref:System.Convert.ToHexString%2A?displayProperty=nameWithType method introduced in .NET 5.0.

[!code-csharpcsProgGuideTypes#48]

See also