/// <summary> /// Parses a byte array and creates and returns one or more strings /// from the byte array. NULL bytes are interpreted as string terminators. /// Strings up to and including maxStrings are created and returned; the remainder /// of the byte array (if any) is passed back using the CreateStringsLeftoverBytes /// delegate. /// </summary> /// <param name="encType">The text encoding of the byte-array string.</param> /// <param name="stringData">A byte-array string.</param> /// <param name="maxStrings">The maximum number of strings to create.</param> /// <param name="leftoversDelegate">A delegate to be used to pass back any bytes /// left over from the string parsing.</param> /// <returns>One or more strings created from the byte array.</returns> public static ReadOnlyCollection <EncodedString> CreateStrings(TextEncodingType encType, byte[] stringData, int maxStrings, CreateStringsLeftoverBytes leftoversDelegate) { List <EncodedString> strings = new List <EncodedString>(); List <int> nullIndices; int incrementSize; if (encType == TextEncodingType.ISO_8859_1 || encType == TextEncodingType.UTF_8) { nullIndices = FindSingleByteNulls(stringData, maxStrings); incrementSize = 1; } else { nullIndices = FindDoubleByteNulls(stringData, maxStrings); incrementSize = 2; } int start = 0; foreach (int nullIndex in nullIndices) { strings.Add(CreateString(start, nullIndex, stringData, encType)); start = nullIndex + incrementSize; } if (strings.Count < maxStrings) { // Make a string from the remaining bytes strings.Add(CreateString(start, stringData.Length, stringData, encType)); } else { // Add the remaining bytes to the end byte[] remainingBytes = new byte[stringData.Length - start]; Array.Copy(stringData, start, remainingBytes, 0, remainingBytes.Length); if (leftoversDelegate != null) { leftoversDelegate(remainingBytes); } } return(new ReadOnlyCollection <EncodedString>(strings)); }
/// <summary> /// Parses a byte array and creates and returns one or more strings /// from the byte array. NULL bytes are interpreted as string terminators. /// Strings up to and including maxStrings are created and returned; the remainder /// of the byte array (if any) is passed back using the CreateStringsLeftoverBytes /// delegate. /// </summary> /// <param name="stringDataWithEncoding">A byte-array string whose first byte /// is a text encoding field.</param> /// <param name="maxStrings">The maximum number of strings to create.</param> /// <param name="leftoversDelegate">A delegate to be used to pass back any bytes /// left over from the string parsing.</param> /// <returns>One or more strings created from the byte array.</returns> public static ReadOnlyCollection <EncodedString> CreateStrings(byte[] stringDataWithEncoding, int maxStrings, CreateStringsLeftoverBytes leftoversDelegate) { TextEncodingType encType = (TextEncodingType)stringDataWithEncoding[0]; byte[] textBytes = new byte[stringDataWithEncoding.Length - 1]; Array.Copy(stringDataWithEncoding, 1, textBytes, 0, textBytes.Length); ReadOnlyCollection <EncodedString> strings; if (textBytes.Length > 0) { strings = CreateStrings(encType, textBytes, maxStrings, leftoversDelegate); } else { List <EncodedString> stringsList = new List <EncodedString>(); stringsList.Add(new EncodedString(encType, "")); strings = new ReadOnlyCollection <EncodedString>(stringsList); } return(strings); }