using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CrossProduct.Core { /// /// the binary encoder class /// public class BinaryEncoder { /// /// Binaries to string. /// /// The binary convert. /// public static string BinaryToString(byte[] BinaryConvert) { return BinaryToString(BinaryConvert, BinaryConvert.Length); } /// /// Binaries to string. /// /// The binary convert. /// Length of the l convert. /// public static string BinaryToString(byte[] BinaryConvert, int lConvertLength) { if (BinaryConvert == null) return ""; int nIndex; StringBuilder buildString = new StringBuilder(lConvertLength * 2); for (nIndex = 0; nIndex < lConvertLength; nIndex++) { buildString.AppendFormat("{0:X2}", BinaryConvert[nIndex]); } return buildString.ToString(); } /// /// Strings to binary. /// /// The binary convert. /// if set to true [b assume string]. /// public static byte[] StringToBinary(string BinaryConvert, bool bAssumeString) { if (BinaryConvert == null || BinaryConvert == "") return new byte[0]; int nLength = BinaryConvert.Length / 2; if (bAssumeString) { if (BinaryConvert.Substring(BinaryConvert.Length - 2, 2) == "00") nLength--; } int nIndex; int nPos; byte[] ReturnArray = new byte[nLength]; for (nPos = 0, nIndex = 0; nPos < nLength; nIndex += 2, nPos++) { ReturnArray[nPos] = byte.Parse(BinaryConvert.Substring(nIndex, 2), System.Globalization.NumberStyles.HexNumber); } return ReturnArray; } /// /// Strings to binary. /// /// The binary convert. /// public static byte[] StringToBinary(string BinaryConvert) { return StringToBinary(BinaryConvert, true); } } }