Byte Array to String Pattern

This design pattern converts an array of bytes to an ASCII string.

 

Pattern Syntax

 

 

// required for ASCIIEncoding class

using System.Text;

 

private ScriptHelper helper = new ScriptHelper();

 

public void SendBytes()

{

   // prepare a byte array

   byte[] bytes = new byte[] { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x2E };

   ProcessBytes( bytes );

}

 

private void ProcessBytes( byte[] bytes )

{

   // convert the byte array to a string using ASCII encoding

   string text = System.Text.Encoding.ASCII.GetString( bytes, 0, bytes.Length );

 

   // display the string

   helper.Logger.Debug( text );

}

 

 

Remarks

This design pattern:

 

See Also

Serial Data

Design Patterns Index