Convert Byte Array To String In C# - C# Corner

文章推薦指數: 80 %
投票人數:10人

Convert C# Byte Array To String. This code snippet is an example of how to convert a byte array into a string. String conversion includes ... ConvertByteArrayToStringInC# MaheshChand UpdateddateSep17,2019 737.1k 0 6 LearntoconvertaC#bytearraytostringinC#and.NETCore.Codeexampleincluded. facebook twitter linkedIn Reddit WhatsApp Email Bookmark Print OtherArtcile Expand ConvertC#ByteArrayToString.Thiscodesnippetisanexampleofhowtoconvertabytearrayintoastring.Stringconversionincludestwotypes.First,conversionanddisplayofC#bytearrayintoastringformat,andsecond,conversionofC#bytesintoactualcharactersofstring.   TheBitConverterclassin.NETFrameworkisprovidesfunctionalitytoconvertbasedatatypestoanarrayofbytes,andanarrayofbytestobasedatatypes. TheBitConverterclasshasastaticoverloadedGetBytesmethodthattakesaninteger,doubleorotherbasetypevalueandconvertthattoaarrayofbytes.TheBitConverterclassalsohaveotherstaticmethodstoreversethisconversion.SomeofthesemethodsareToDouble,ToChart,ToBoolean,ToInt16,andToSingle. Thefollowingcodesnippetconvertsabytearrayintoastring. string bitString = BitConverter.ToString(bytes);  Thefollowingcodesnippetconvertsabytearrayintoactualcharacterrepresentationofbytesinastring. string utfString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);  Listing1isthecompletesourcecode.Thecodeistestedin.NETCore2.2andC#.using System;  using System.Text;  using System.Security.Cryptography;    class Program  {      static void Main(string[] args)      {          string plainData = "Mahesh Chand is the founder of C# Corner and an author and speaker.";          Console.WriteLine("Raw data: {0}", plainData);              // Sha1          Console.WriteLine("============SHA1 Hash=============");            // Create SHA1Managed          using (SHA1Managed sha1 = new SHA1Managed())          {              // ComputeHash - returns byte array              byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(plainData));                // Merge all bytes into a string of bytes              StringBuilder builder = new StringBuilder();              for (int i = 0; i 



請為這篇文章評分?