Paste ASCII from byte array - CSharp System

CSharp examples for System:Byte Array

Description

Paste ASCII from byte array

Demo Code


using System.Text;
using System;/* w  w  w .  jav a  2s. c  o m*/

public class Main{
        public static void PasteASCII(this byte[] data, string s, int offset, int limit, TextAlign align = TextAlign.Left)
        {
            if (s == null || s.Length == 0)
            {
                return;
            }
            if (offset < 0 || offset >= data.Length)
            {
                throw new ArgumentException("offset");
            }
            if (limit < 1)
            {
                throw new ArgumentException("limit");
            }
            if (limit > data.Length - offset)
            {
                limit = data.Length - offset;
            }
            if (s.Length > limit)
            {
                s = s.Substring(0, limit);
            }
            byte alignOffset = 0, len = (byte)s.Length;
            if (align == TextAlign.Center)
            {
                alignOffset = (byte)((limit - len) / 2);
            }
            else if (align == TextAlign.Right)
            {
                alignOffset = (byte)(limit - len);
            }
            char[] chars = s.ToCharArray();
            byte c;
            for (byte i = 0; i < len; i++)
            {
                if (chars[i] > 0xFF)
                {
                    c = 0xFF;
                }
                else
                {
                    c = (byte)chars[i];
                }
                data[i + alignOffset + offset] = c;
            }
        }
}

Related Tutorials