Convert Strings To Byte Array Array - CSharp System

CSharp examples for System:Byte

Description

Convert Strings To Byte Array Array

Demo Code


using System.Text;
using System.IO;//ww  w  . j  ava  2  s  .  com
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        public static byte[][] ConvertStringsToByteArrayArray(IEnumerable strings, int minLength)
        {
            List<byte[]> list = new List<byte[]>();
            foreach (string str in strings)
            {
                if (str.StartsWith("0x"))
                {
                    list.Add(ByteConverter.ToByteArrayFromHexString(str));
                }
                else if (str.Length >= minLength)
                {
                    char[] chars = str.ToCharArray();
                    byte[] bytes = System.Text.Encoding.Default.GetBytes(chars);
                    byte[] destinationArray = System.Text.Encoding.BigEndianUnicode.GetBytes(chars);
                    if ((destinationArray.Length > 0) && (destinationArray[0] == 0))
                    {
                        byte[] sourceArray = destinationArray;
                        destinationArray = new byte[sourceArray.Length - 1];
                        Array.Copy(sourceArray, 1, destinationArray, 0, destinationArray.Length);
                    }
                    byte[] item = System.Text.Encoding.UTF8.GetBytes(chars);
                    list.Add(destinationArray);
                    list.Add(bytes);
                    if (bytes.Length != item.Length)
                    {
                        list.Add(item);
                    }
                }
            }
            return list.ToArray();
        }
        public static byte[][] ConvertStringsToByteArrayArray(IEnumerable strings)
        {
            return ConvertStringsToByteArrayArray(strings, 3);
        }
}

Related Tutorials