Skip to content

Commit 32ef5b2

Browse files
Add example of using Convert.ToHexString() (#22185)
* Add example of using Convert.ToHexString() Show an example of converting a byte[] to a hex string using the new .NET 5.0 Convert.ToHexString() method. * Target net5.0 Target net5.0 so that Convert.ToHexString() is available. * Fix snippet numbering Use N+1 snippet number for file. * Apply code review feedback Fix URL and clarify versioning. Co-authored-by: Youssef Victor <[email protected]> Co-authored-by: Youssef Victor <[email protected]>
1 parent 7738c39 commit 32ef5b2

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ These examples show you how to perform the following tasks:
5555

5656
[!code-csharp[csProgGuideTypes#38](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#38)]
5757

58+
## Example
59+
60+
The following example shows how to convert a [byte](../../language-reference/builtin-types/integral-numeric-types.md) array to a hexadecimal string by calling the <xref:System.Convert.ToHexString%2A?displayProperty=nameWithType> method introduced in .NET 5.0.
61+
62+
[!code-csharp[csProgGuideTypes#47](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#47)]
63+
5864
## See also
5965

6066
- [Standard Numeric Format Strings](../../../standard/base-types/standard-numeric-format-strings.md)

samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/CS.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,17 @@ static void Main()
555555

556556
// Output: 200.0056
557557
//</snippet39>
558+
559+
//<snippet47>
560+
byte[] array = { 0x64, 0x6f, 0x74, 0x63, 0x65, 0x74 };
561+
562+
string hexValue = Convert.ToHexString(array);
563+
Console.WriteLine(hexValue);
564+
565+
/*Output:
566+
646F74636574
567+
*/
568+
//</snippet47>
558569
// Keep the console window open in debug mode.
559570
System.Console.WriteLine("Press any key to exit.");
560571
System.Console.ReadKey();

0 commit comments

Comments
 (0)