using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Neo.Afx.Utilities { public static class StringUtilities { #region HexStringToByteArray /// /// Converts a file hex encoded string to a file byte array /// /// The s. /// public static byte[] HexStringToByteArray(string s) { var buffer = new byte[s.Length / 2]; for (var i = 0; i < buffer.Length; i++) { buffer[i] = (byte)Int32.Parse(s.Substring(2 * i, 2), System.Globalization.NumberStyles.HexNumber); } return buffer; } #endregion #region ByteArrayToHexString /// /// Converts a file byte array to a Hex encoded string. /// /// The buffer. /// public static string ByteArrayToHexString(byte[] buffer) { if (buffer == null || buffer.Length <= 0) { return null; } const string digits = "0123456789ABCDEF"; byte ch = 0x00; var i = 0; var sb = new StringBuilder(buffer.Length * 2); while (i < buffer.Length) { ch = (byte)(buffer[i] & 0xF0); // strip off high nibble ch = (byte)(ch >> 4); // shift the bits down ch = (byte)(ch & 0x0F); // must do this if high order bit is on! sb.Append(digits[(int)ch]); // convert the nibble to a String Character ch = (byte)(buffer[i] & 0x0F); // strip off low nibble sb.Append(digits[(int)ch]); // convert the nibble to a String Character i++; } return sb.ToString(); } #endregion #region String-Byte, Byte-String /// /// Converts the given string to a byte array using Unicode encoding format. /// /// The string to be converted. /// A byte array. public static byte[] StringToByteArray(string s) { return UnicodeEncoding.Unicode.GetBytes(s); } /// /// Converts a byte array to a string using Unicode encoding format. /// /// The byte array to be queried. /// A string. public static string ByteArrayToString(byte[] buffer) { return UnicodeEncoding.Unicode.GetString(buffer); } #endregion #region Apply Data Mask /// /// Converts the given string to a byte array using Unicode encoding format. /// /// The string to be converted. /// First character position to be changed. /// Number of characters to be masked. /// string. public static string ApplyDataMask(string originalValue, int? partStartIndexParameter, int? partLengthParameter) { int partStartIndex = 0; int partLength = 0; if (partStartIndexParameter != null) { partStartIndex = int.Parse(partStartIndexParameter.ToString()); } if (partLengthParameter != null) { partLength = int.Parse(partLengthParameter.ToString()); } if (String.IsNullOrEmpty(originalValue)) { return originalValue; } var newValue = ""; var origLen = originalValue.Length; var loopCt = 1; var part = ""; if (partLength > origLen) { partLength = origLen; } if (origLen == 1) { return "X"; } if (partStartIndex > origLen) { return originalValue; } if (partStartIndex != 0 && partLength != 0) { if (partStartIndex > originalValue.Length) { partStartIndex = originalValue.Length - 1; } else { partStartIndex = partStartIndex - 1; } if ((partStartIndex + partLength) > originalValue.Length)//Start Index + Length Cannot exceed the last character in the { partLength = partLength - ((partStartIndex + partLength) - originalValue.Length); } part = originalValue.Substring(partStartIndex, partLength); } else if (origLen < 6) { partStartIndex = (origLen / 2) - 1; partLength = origLen - partStartIndex - 1; part = originalValue.Substring(partStartIndex, partLength); } else if (origLen < 8) { partStartIndex = 2; partLength = origLen - partStartIndex - 2; part = originalValue.Substring(partStartIndex, partLength); } else { partStartIndex = 3; partLength = origLen - partStartIndex - 3; part = originalValue.Substring(partStartIndex, partLength); } while (loopCt <= partLength) { newValue = newValue + "X"; loopCt = loopCt + 1; } return originalValue.Replace(part, newValue); } #endregion } }