// /*************************************************************************** // The Disc Image Chef // ---------------------------------------------------------------------------- // // Filename : StringHandlers.cs // Author(s) : Natalia Portillo // // Component : Helpers. // // --[ Description ] ---------------------------------------------------------- // // Convert byte arrays to C# strings. // // --[ License ] -------------------------------------------------------------- // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, see . // // ---------------------------------------------------------------------------- // Copyright © 2011-2016 Natalia Portillo // ****************************************************************************/ using System.Text; namespace DiscImageChef { public static class StringHandlers { /// /// Converts a null-terminated (aka C string) ASCII byte array to a C# string /// /// The corresponding C# string /// A null-terminated (aka C string) ASCII byte array public static string CToString(byte[] CString) { return CToString(CString, Encoding.ASCII); } /// /// Converts a null-terminated (aka C string) byte array with the specified encoding to a C# string /// /// The corresponding C# string /// A null-terminated (aka C string) byte array in the specified encoding /// Encoding. public static string CToString(byte[] CString, Encoding encoding) { if(CString == null) return null; StringBuilder sb = new StringBuilder(); for(int i = 0; i < CString.Length; i++) { if(CString[i] == 0) break; sb.Append(encoding.GetString(CString, i, 1)); } return sb.ToString(); } /// /// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string /// /// The corresponding C# string /// A length-prefixed (aka Pascal string) ASCII byte array public static string PascalToString(byte[] PascalString) { if(PascalString == null) return null; StringBuilder sb = new StringBuilder(); byte length = PascalString[0]; for(int i = 1; i < length + 1; i++) { sb.Append(Encoding.ASCII.GetString(PascalString, i, 1)); } return sb.ToString(); } /// /// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string /// /// The corresponding C# string /// A space (' ', 0x20, ASCII SPACE) padded ASCII byte array public static string SpacePaddedToString(byte[] SpacePaddedString) { if(SpacePaddedString == null) return null; int length = 0; for(int i = SpacePaddedString.Length; i >= 0; i--) { if(i == 0) return ""; if(SpacePaddedString[i - 1] != 0x20) { length = i; break; } } return length == 0 ? "" : Encoding.ASCII.GetString(SpacePaddedString, 0, length); } /// /// Converts an OSTA compressed unicode byte array to a C# string /// /// The C# string. /// OSTA compressed unicode byte array. public static string DecompressUnicode(byte[] dstring) { ushort unicode; byte compId = dstring[0]; string temp = ""; if(compId != 8 && compId != 16) return null; for(int byteIndex = 1; byteIndex < dstring.Length;) { if(compId == 16) unicode = (ushort)(dstring[byteIndex++] << 8); else unicode = 0; if(byteIndex < dstring.Length) unicode |= dstring[byteIndex++]; if(unicode == 0) break; temp += Encoding.Unicode.GetString(System.BitConverter.GetBytes(unicode)); } return temp; } } }