Web Design that taps into the haromny and vision of your dreams.

C# Convert String to Byte Array and Byte Array to String

Converting between Strings and Byte Array

Written By on in C#

44 words, estimated reading time 1 minutes.

These two methods allow conversion between a C# byte array and string, and vice versa. Handy in dealing with COM objects and some .Net Providers (IBM CWBX, EventLog and so on).

String to byte array

public static byte[] StrToByteArray(string str)
    System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
    return encoding.GetBytes(str);

Byte array to string

public static string ByteArrayToStr(byte[] byteArray)
  System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  return encoding.GetString(byteArray);

Last updated on: Thursday 22nd June 2017



There are no comments for this post. Be the first!


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me