public static IEnumerable <CharacterInfo> GetCharacters(string s) { var bytes = UTF32.GetBytes(s); for (int i = 0; i < bytes.Length; i += 4) { var cp = new CharacterInfo(); var utf32 = new byte[4]; Array.Copy(bytes, i, utf32, 0, 4); cp.utf32 = utf32; cp.codePoint = (utf32[0] << 0) | (utf32[1] << 8) | (utf32[2] << 16) | (utf32[3] << 24); var decoded = UTF32.GetString(utf32); cp.@char = decoded; cp.utf16 = Unicode.GetBytes(decoded); cp.utf8 = UTF8.GetBytes(decoded); yield return(cp); } }
private static UnicodeCategory GetUnicodeCategory(int codePoint, byte[] buffer) { buffer[0] = unchecked ((byte)(codePoint >> 0)); buffer[1] = unchecked ((byte)(codePoint >> 8)); buffer[2] = unchecked ((byte)(codePoint >> 16)); buffer[3] = unchecked ((byte)(codePoint >> 24)); var s = UTF32.GetString(buffer); var c = char.GetUnicodeCategory(s, 0); return(c); }
/// <summary> /// Converts the string into UTF-32 bytes and passes them to the hashing algorithm. /// </summary> /// <param name="str">the string to add to the signature by passing it to the hashing algorithm.</param> /// <returns>this task signer.</returns> /// <remarks> /// This method uses UTF-32 because it has fixed character width. This way we can use buffers to digest /// the string piecewise. Otherwise we would have to allocate new byte arrays for each string. /// </remarks> public Sha256Signer Digest(string str) { var blockMaxCharCount = buffer.Length >> 2; var strLength = str.Length; for (int charsDigested = 0; charsDigested < strLength; charsDigested += blockMaxCharCount) { var bytes = UTF32.GetBytes(str, charsDigested, Min(blockMaxCharCount, strLength - charsDigested), buffer, 0); hashAlgorithm.TransformBlock(buffer, 0, bytes, null, 0); } return(this); }
///<summary> /// Get with encoding from Bytes ///</summary> public static string FromBytes(this byte[] bytes, string encoding = "Unicode") { if (Comparison(encoding, "UTF-8")) { return(UTF8.GetString(bytes)); } else if (Comparison(encoding, "UTF-32")) { return(UTF32.GetString(bytes)); } else if (Comparison(encoding, "ASCII")) { return(ASCII.GetString(bytes)); } return(Unicode.GetString(bytes)); }
///<summary> /// Specify Encoding for Bytes from a string ///</summary> public static byte[] ToBytes(this string source, string encoding = "Unicode") { if (Comparison(encoding, "UTF-8")) { return(UTF8.GetBytes(source)); } else if (Comparison(encoding, "UTF-32")) { return(UTF32.GetBytes(source)); } else if (Comparison(encoding, "ASCII")) { return(ASCII.GetBytes(source)); } return(Unicode.GetBytes(source)); }
/// <summary> /// Internal text encoder. /// </summary> /// <param name="text">Text to encode.</param> /// <param name="encodedByte">Callback with the byte encoded.</param> void EncodeText(string text, Action <Stream, byte> encodedByte) { using (var stream = new MemoryStream(UTF32.GetBytes(text))) { EncodeText(stream, encodedByte); } }
public override int GetMaxCharCount(int byteCount) { return(UTF32.GetMaxCharCount(byteCount)); }
public override int GetCharCount(byte[] bytes, int index, int count) { return(UTF32.GetCharCount(MapAll(bytes), index << 2, count << 2)); }
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { return(UTF32.GetChars(MapAll(bytes), byteIndex << 2, byteCount << 2, chars, charIndex)); }
static byte GetUtf32HexNumber(Stream stream) { byte[] buffer = new byte[4 * 2]; stream.Read(buffer, 0, buffer.Length); return(System.Convert.ToByte(UTF32.GetString(buffer), 16)); }
byte[] GetControlBytes(Stream stream, byte current) { if (current != 0x2F || stream.Position >= stream.Length) { return new[] { current } } ; byte[] dummy = new byte[4]; stream.Read(dummy, 0, dummy.Length); if (dummy[0] != 0x5B || dummy[1] != 0 || dummy[2] != 0 || dummy[3] != 0x00) { stream.Position -= 4; return(new[] { current }); } byte[] buffer = new byte[5 * 4]; stream.Read(buffer, 0, buffer.Length); string token = UTF32.GetString(buffer); stream.Position += 4; // ( byte[] control = { current }; switch (token) { case "unk03": stream.Position += 8; // 0x control = new byte[] { 0x0D, GetUtf32HexNumber(stream) }; break; case "unk01": stream.Position += 8; // 0x control = new byte[] { 0x15, GetUtf32HexNumber(stream) }; break; case "unk02": stream.Position += 8; // 0x control = new byte[] { 0x04, GetUtf32HexNumber(stream) }; break; case "color": stream.Position += 8; // 0x control = new byte[] { 0x0C, GetUtf32HexNumber(stream), GetUtf32HexNumber(stream), GetUtf32HexNumber(stream) }; break; case "unk04": stream.Position += 8; // 0x control = new byte[] { 0x08, GetUtf32HexNumber(stream), GetUtf32HexNumber(stream), GetUtf32HexNumber(stream) }; break; case "unk05": stream.Position += 8; // 0x control = new byte[] { 0x18, GetUtf32HexNumber(stream) }; break; case "unk06": stream.Position += 8; // 0x control = new byte[] { 0x12, GetUtf32HexNumber(stream), GetUtf32HexNumber(stream) }; break; case "unk07": stream.Position += 8; // 0x control = new byte[] { 0x19, GetUtf32HexNumber(stream) }; break; case "unk08": stream.Position += 8; // 0x control = new byte[] { 0x05, GetUtf32HexNumber(stream) }; break; case "unk09": stream.Position += 8; // 0x control = new byte[] { 0x0F, GetUtf32HexNumber(stream), GetUtf32HexNumber(stream), GetUtf32HexNumber(stream) }; break; case "unk0A": stream.Position += 8; // 0x control = new byte[] { 0x1E, GetUtf32HexNumber(stream) }; break; case "unk0B": stream.Position += 8; // 0x control = new byte[] { 0x1F }; break; } stream.Position += 4; // ) stream.Position += 4; // ] return(control); }