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); }
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); }
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; }
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; }