CopyPastehas never been so tasty!

Convert string into byte and char array in C#

by anonymous

  • 0
  • 0
  • 0
112 views

Convert string into byte and char array in C#

String into Byte ArrayByte arrays store only byte values which are in the form of integers. These integers range from 0-255 in base 10 or 0-FF in Hex.Following code sample shows that how to convert string into a byte array in C#

Code 

 class Program

    {

static void Main(string[] args)

        {

stringStringMessage = "Hello World How Are you? Pi \u03C0 Yen \uFFE5";

            UTF8Encoding UTF8 = new UTF8Encoding();

Byte[] BytesMessage = UTF8.GetBytes(StringMessage);

Console.WriteLine("{0}", StringMessage);

Console.ReadLine();

         }

    }

 String into Char Array

 A char array stores string data in sequence. For example if we take an array that can store up to 10 char type elements. It will be represented as

String s = “hello”

h

e

l

l

o

 

 

 

 

 

 Following code sample shows that how to convert string into a char array in C#

 Code

 using System;

usingSystem.Collections.Generic;

 class Program

    {

static void Main(string[] args)

        {

string s = "Hello World";

 for (int index = 0; index <s.Length; index++)

Console.WriteLine("Char: {0}", s[index]);

Console.ReadLine();

        }

    }

Add A Comment: