Exemplo n.º 1
0
        public static string ByteArrayToString(byte[] thearray, HashStringStyle style)
        {
            StringBuilder sbBuffer = new StringBuilder();

            if (style == HashStringStyle.BigHex)
            {
                sbBuffer.Append("0x");
            }

            foreach (byte bSingle in thearray)
            {
                if (style == HashStringStyle.BigHex)
                {
                    sbBuffer.AppendFormat("{0:X}", bSingle);
                }
                else if (style == HashStringStyle.SingleHex)
                {
                    sbBuffer.AppendFormat("0x{0:X} ", bSingle);
                }
                else if (style == HashStringStyle.HexWithSeparator)
                {
                    sbBuffer.AppendFormat("{0:X} ", bSingle);
                }
            }

            string toReturn = sbBuffer.ToString().Trim();

            if (style == HashStringStyle.HexWithSeparator)
            {
                toReturn = toReturn.Replace(' ', '-');
            }

            return(toReturn);
        }
Exemplo n.º 2
0
        public static byte[] StringToByteArray(string thearray, HashStringStyle style, int expectedLength)
        {
            byte[] toReturn = null;

            if (expectedLength > 0)
            {
                toReturn = new byte[expectedLength];
            }

            if (style == HashStringStyle.BigHex)
            {
                // sbBuffer.AppendFormat("{0:X}", bSingle);
            }
            else if (style == HashStringStyle.SingleHex)
            {
                // sbBuffer.AppendFormat("0x{0:X} ", bSingle);
            }
            else if (style == HashStringStyle.HexWithSeparator)
            {
                string[] separated = thearray.Split(new char[] { '-' });
                if (expectedLength > 0)
                {
                    if (expectedLength == separated.Length)
                    {
                        for (int i = 0; i < separated.Length; i++)
                        {
                            if (!Byte.TryParse(separated[i], NumberStyles.AllowHexSpecifier, null, out toReturn[i]))
                            {
                                return(null); // error
                            }
                        }
                    }
                }
                else
                {
                    toReturn = new byte[separated.Length];
                    for (int i = 0; i < separated.Length; i++)
                    {
                        if (!Byte.TryParse(separated[i], NumberStyles.AllowHexSpecifier, null, out toReturn[i]))
                        {
                            return(null); // error
                        }
                    }
                }
            }

            return(toReturn);
        }
Exemplo n.º 3
0
        public static string ByteArrayToString(byte[] thearray, HashStringStyle style)
        {
            StringBuilder sbBuffer = new StringBuilder();

            if (style == HashStringStyle.BigHex)
            {
                sbBuffer.Append("0x");
            }

            foreach (byte bSingle in thearray)
            {
                if (style == HashStringStyle.BigHex)
                    sbBuffer.AppendFormat("{0:X}", bSingle);
                else if (style == HashStringStyle.SingleHex)
                    sbBuffer.AppendFormat("0x{0:X} ", bSingle);
                else if (style == HashStringStyle.HexWithSeparator)
                    sbBuffer.AppendFormat("{0:X} ", bSingle);
            }

            string toReturn = sbBuffer.ToString().Trim();

            if (style == HashStringStyle.HexWithSeparator)
            {
                toReturn = toReturn.Replace(' ', '-');
            }

            return toReturn;
        }
Exemplo n.º 4
0
        public static byte[] StringToByteArray(string thearray, HashStringStyle style, int expectedLength)
        {
            byte[] toReturn = null;

            if (expectedLength > 0)
                toReturn = new byte[expectedLength];

            if (style == HashStringStyle.BigHex)
            {
                // sbBuffer.AppendFormat("{0:X}", bSingle);
            }
            else if (style == HashStringStyle.SingleHex)
            {
                // sbBuffer.AppendFormat("0x{0:X} ", bSingle);
            }
            else if (style == HashStringStyle.HexWithSeparator)
            {
                string[] separated = thearray.Split(new char[] { '-' });
                if (expectedLength > 0)
                {
                    if (expectedLength == separated.Length)
                    {
                        for(int i = 0; i < separated.Length; i++)
                        {
                            if (!Byte.TryParse(separated[i], NumberStyles.AllowHexSpecifier, null, out toReturn[i]))
                                return null; // error
                        }
                    }
                }
                else
                {
                    toReturn = new byte[separated.Length];
                    for (int i = 0; i < separated.Length; i++)
                    {
                        if (!Byte.TryParse(separated[i], NumberStyles.AllowHexSpecifier, null, out toReturn[i]))
                            return null; // error
                    }
                }
            }

            return toReturn;
        }