public static void Main() { Encoding enc = new UTF32Encoding(false, true, true); string value = "\u00C4 \uD802\u0033 \u00AE"; try { byte[] bytes = enc.GetBytes(value); foreach (var byt in bytes) { Console.Write("{0:X2} ", byt); } Console.WriteLine(); string value2 = enc.GetString(bytes); Console.WriteLine(value2); } catch (EncoderFallbackException e) { Console.WriteLine("Unable to encode {0} at index {1}", e.IsUnknownSurrogate() ? String.Format("U+{0:X4} U+{1:X4}", Convert.ToUInt16(e.CharUnknownHigh), Convert.ToUInt16(e.CharUnknownLow)) : String.Format("U+{0:X4}", Convert.ToUInt16(e.CharUnknown)), e.Index); } }
public static string get_uft32(string unicodeString) { UTF32Encoding utf32 = new UTF32Encoding(); Byte[] encodedBytes = utf32.GetBytes(unicodeString); String decodedString = utf32.GetString(encodedBytes); return(decodedString); }
/// <summary> /// Tries to convert a given memory stream to the UTF32 encoding by adding an UTF32 BOM to the stream. /// </summary> /// <param name="stream">The stream to try to convert to.</param> /// <param name="encoding">The encoding successfully used with the stream data.</param> /// <param name="bigEndian"><c>true</c> to use the big endian byte order (most significant byte first); <c>false</c> to use the little endian byte order (least significant byte first).</param> /// <param name="noBom">A value indicating whether the detected unicode file contains a byte-order-mark.</param> /// <returns>A string converted into the UTF32 encoding if successful; otherwise null.</returns> public static string TryUtf32Encoding(MemoryStream stream, bool bigEndian, out Encoding encoding, out bool noBom) { noBom = false; if (!FormSettings.Settings.SkipUtf32Le && !bigEndian && ByteMatch(GetEncodingComparisonBytes(stream), Utf32LittleEndianBom) || !FormSettings.Settings.SkipUtf32Be && bigEndian && ByteMatch(GetEncodingComparisonBytes(stream), Utf32BigEndianBom)) { try { encoding = new UTF32Encoding(bigEndian, true, true); return(encoding.GetString(stream.ToArray())); } catch (Exception ex) { ExceptionOccurred?.Invoke("TryUtf32Encoding", new EncodingExceptionEventArgs { Exception = ex }); // failed.. encoding = null; return(null); } } // the user doesn't want this detection.. if (FormSettings.Settings.SkipUtf32Le && !bigEndian || FormSettings.Settings.SkipUtf32Be && bigEndian) { // ..so just return.. encoding = null; return(null); } try // there is no BOM.. { // get the contents of the memory stream into a list of bytes.. List <byte> bytes = new List <byte>(stream.ToArray()); // insert the BOM.. //bytes.InsertRange(0, bigEndian ? Utf32BigEndianBom : Utf32LittleEndianBom); // try the UTF32 encoding with the BOM.. encoding = new UTF32Encoding(bigEndian, false, true); noBom = true; return(encoding.GetString(bytes.ToArray())); } catch (Exception ex) { ExceptionOccurred?.Invoke("TryUtf32Encoding", new EncodingExceptionEventArgs { Exception = ex }); // failed.. encoding = null; return(null); } }
/// <summary> /// Provides decoding functionality in UniversalString way for a string. /// </summary> /// <param name="bytes">A BER encoding result of UniversalString</param> /// <returns>The decoding result.</returns> private static string UniversalStringDecode(byte[] bytes) { if (bytes == null || bytes.Length % 4 != 0) { throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData); } UTF32Encoding be = new UTF32Encoding(true, false); return(be.GetString(bytes)); }
public static void Main() { // Create a UTF-32 encoding that supports a BOM. var enc = new UTF32Encoding(); // A Unicode string with two characters outside an 8-bit code range. String s = "This Unicode string has 2 characters " + "outside the ASCII range: \n" + "Pi (\u03A0), and Sigma (\u03A3)."; Console.WriteLine("Original string:"); Console.WriteLine(s); Console.WriteLine(); // Encode the string. Byte[] encodedBytes = enc.GetBytes(s); Console.WriteLine("The encoded string has {0} bytes.\n", encodedBytes.Length); // Write the bytes to a file with a BOM. var fs = new FileStream(@".\UTF32Encoding.txt", FileMode.Create); Byte[] bom = enc.GetPreamble(); fs.Write(bom, 0, bom.Length); fs.Write(encodedBytes, 0, encodedBytes.Length); Console.WriteLine("Wrote {0} bytes to the file.\n", fs.Length); fs.Close(); // Open the file using StreamReader. var sr = new StreamReader(@".\UTF32Encoding.txt"); String newString = sr.ReadToEnd(); sr.Close(); Console.WriteLine("String read using StreamReader:"); Console.WriteLine(newString); Console.WriteLine(); // Open the file as a binary file and decode the bytes back to a string. fs = new FileStream(@".\Utf32Encoding.txt", FileMode.Open); Byte[] bytes = new Byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); fs.Close(); String decodedString = enc.GetString(encodedBytes); Console.WriteLine("Decoded bytes from binary file:"); Console.WriteLine(decodedString); }
public static void Main() { var utf32 = new UTF32Encoding(!BitConverter.IsLittleEndian, true); String s = "It was the best of times, it was the worst of times..."; // We need to dimension the array, since we'll populate it with 2 method calls. Byte[] bytes = new Byte[utf32.GetByteCount(s) + utf32.GetPreamble().Length]; // Encode the string. Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length); utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length); // Decode the byte array. String s2 = utf32.GetString(bytes, 0, bytes.Length); Console.WriteLine(s2); }
private static string FromCodePoints(int[] codepoints, int startIndex, int count) { bool useBigEndian = !BitConverter.IsLittleEndian; Encoding utf32 = new UTF32Encoding(useBigEndian, false, true); byte[] octets = new byte[count * 4]; for (int i = startIndex, j = 0; i < startIndex + count; i++, j += 4) { byte[] bytes = BitConverter.GetBytes(codepoints[i]); octets[j] = bytes[0]; octets[j + 1] = bytes[1]; octets[j + 2] = bytes[2]; octets[j + 3] = bytes[3]; } return(utf32.GetString(octets)); }
/* * GetString * * Syntax: * byte[] GetString( * string Name // Name of file * ) * * Explanation: * Load a text file. * * Return Value: * string of entire file; null otherwise */ public string GetString(Encoding _Encoding, string Name) { byte[] _Buffer = GetBytes(Name); if (_Buffer.Length == 0) { Console.Write("Attempting to read from an uninitialized buffer, aborting...\r\n"); return(null); } try { if (_Encoding == Encoding.ASCII) { ASCIIEncoding enc = new ASCIIEncoding(); return(enc.GetString(_Buffer)); } else if (_Encoding == Encoding.Unicode) { UnicodeEncoding enc = new UnicodeEncoding(); return(enc.GetString(_Buffer)); } else if (_Encoding == Encoding.UTF32) { UTF32Encoding enc = new UTF32Encoding(); return(enc.GetString(_Buffer)); } else if (_Encoding == Encoding.UTF7) { UTF7Encoding enc = new UTF7Encoding(); return(enc.GetString(_Buffer)); } else if (_Encoding == Encoding.UTF8) { UTF8Encoding enc = new UTF8Encoding(); return(enc.GetString(_Buffer)); } } catch (Exception Error) { Console.Write("{0}\r\n", Error.Message); return(null); } return(null); }
public static void Main() { // The encoding. var enc = new UTF32Encoding(); // Create a string. String s = "This string contains two characters " + "with codes outside the ASCII code range: " + "Pi (\u03A0) and Sigma (\u03A3)."; Console.WriteLine("Original string:"); Console.WriteLine(" {0}", s); // Encode the string. Byte[] encodedBytes = enc.GetBytes(s); Console.WriteLine(); Console.WriteLine("Encoded bytes:"); for (int ctr = 0; ctr < encodedBytes.Length; ctr++) { Console.Write("[{0:X2}]{1}", encodedBytes[ctr], (ctr + 1) % 4 == 0 ? " " : ""); if ((ctr + 1) % 16 == 0) { Console.WriteLine(); } } Console.WriteLine(); // Decode bytes back to string. // Notice Pi and Sigma characters are still present. String decodedString = enc.GetString(encodedBytes); Console.WriteLine(); Console.WriteLine("Decoded string:"); Console.WriteLine(" {0}", decodedString); }
public clsForecast[] getForecast(clsForecast[] forecast) { // API Key, Lat, Long, Unit string url = "https://api.forecast.io/forecast/" + API_KEY + "/" + flat + "," + flon; //var request = new ForecastIORequest(API_KEY, flat, flon, Unit.auto); var response = new WebClient().DownloadData(url); UTF32Encoding utf32 = new UTF32Encoding(); byte[] utf32Array = Encoding.Convert(Encoding.UTF8, Encoding.UTF32, response); string finalString = utf32.GetString(utf32Array); dynamic weatherData = JObject.Parse(finalString.ToString()); //var response = request.Get(); for (int i = 0; i < 7; i++) { clsForecast tempForecast = new clsForecast(); tempForecast.getSetDate = (string)weatherData.daily.data[i].time.ToString(); tempForecast.getSetLongDate = (string)weatherData.daily.data[i].time.ToString(); tempForecast.getSetDay = (string)weatherData.daily.data[i].time; tempForecast.getSetSummary = (string)weatherData.daily.data[i].summary; tempForecast.setIcon((string)weatherData.daily.data[i].icon); tempForecast.getSetSunrise = (string)weatherData.daily.data[i].sunriseTime.ToString(); tempForecast.getSetSunset = (string)weatherData.daily.data[i].sunsetTime.ToString(); tempForecast.setMoonPhase((float)weatherData.daily.data[i].moonPhase); tempForecast.getSetPrecipIntensity = (float)weatherData.daily.data[i].precipIntensity; tempForecast.getSetPrecipIntensityMax = (float)weatherData.daily.data[i].precipIntensityMax; tempForecast.getSetPrecipProbability = (float)weatherData.daily.data[i].precipProbability; tempForecast.getSetPrecipType = (string)weatherData.daily.data[i].precipType; tempForecast.getSetLow = (float)weatherData.daily.data[i].temperatureMin; tempForecast.getSetLowTime = (string)weatherData.daily.data[i].temperatureMinTime.ToString(); tempForecast.getSetHigh = (float)weatherData.daily.data[i].temperatureMax; tempForecast.getSetHighTime = (string)weatherData.daily.data[i].temperatureMaxTime.ToString(); tempForecast.getSetFeelsLow = (float)weatherData.daily.data[i].apparentTemperatureMin; tempForecast.getSetFeelsLowTime = (string)weatherData.daily.data[i].apparentTemperatureMinTime.ToString(); tempForecast.getSetFeelsHigh = (float)weatherData.daily.data[i].apparentTemperatureMax; tempForecast.getSetFeelsHighTime = (string)weatherData.daily.data[i].apparentTemperatureMaxTime.ToString(); tempForecast.getSetDewPoint = (float)weatherData.daily.data[i].dewPoint; tempForecast.getSetHumidity = (float)weatherData.daily.data[i].humidity; tempForecast.getSetWindSpeed = (float)weatherData.daily.data[i].windSpeed; if (weatherData.daily.data[i].windBearing == null) { tempForecast.getSetWindBearing = 0; } else { tempForecast.getSetWindBearing = (int)weatherData.daily.data[i].windBearing; } if (weatherData.daily.data[i].visibility == null) { tempForecast.getSetVisibility = 11; } else { tempForecast.getSetVisibility = (float)weatherData.daily.data[i].visibility; } tempForecast.getSetCloudCover = (float)weatherData.daily.data[i].cloudCover; tempForecast.getSetPressure = (float)weatherData.daily.data[i].pressure; tempForecast.getSetOzone = (float)weatherData.daily.data[i].ozone; tempForecast.Units = (string)weatherData.flags.units; forecast[i] = tempForecast; } current.getSetDate = (string)weatherData.currently.time; current.getSetLongDate = (string)weatherData.currently.time; current.getSetDay = (string)weatherData.currently.time; current.getSetSummary = (string)weatherData.currently.summary; current.setIcon((string)weatherData.currently.icon); current.getSetPrecipIntensity = (float)weatherData.currently.precipIntensity; current.getSetPrecipProbability = (float)weatherData.currently.precipProbability; current.getSetPrecipType = (string)weatherData.currently.precipType; current.getSetTemp = (float)weatherData.currently.temperature; current.getSetFeelsLike = (float)weatherData.currently.apparentTemperature; current.getSetDewPoint = (float)weatherData.currently.dewPoint; current.getSetHumidity = (float)weatherData.currently.humidity; current.getSetWindSpeed = (float)weatherData.currently.windSpeed; current.getSetWindBearing = (int)weatherData.currently.windBearing; current.getSetVisibility = (int)weatherData.currently.visibility; current.getSetCloudCover = (float)weatherData.currently.cloudCover; current.getSetPressure = (float)weatherData.currently.pressure; current.getSetOzone = (float)weatherData.currently.ozone; try { current.getSetAlertTitle = (string)weatherData.alerts[0].title; current.getSetAlertTime = (string)weatherData.alerts[0].time; current.getSetAlertExpire = (string)weatherData.alerts[0].expires; current.getSetAlertDescription = (string)weatherData.alerts[0].description; current.getSetAlertURL = (string)weatherData.alerts[0].uri; } catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) { // This section intentionally blank, this failure is expected and is okay. } current.Units = (string)weatherData.flags.units; forecast[9] = current; return(forecast); }
public static string Encrypt(string text) { byte[] input = utf32.GetBytes(text); byte[] output = Transform(input, _Rijndael.CreateEncryptor(Key, IV)); return(utf32.GetString(output)); }
public static string Encrypt(string text) { byte[] input = utf32.GetBytes(text); byte[] output = m_rsa.Encrypt(input, false); // no OAEP padding return(utf32.GetString(output)); }
public static IEnumerable <Token> Tokenize(TextReader textReader, IList <ParsingError> errors = null) { var reader = new LocationTextReader(textReader); void emitError(string message) { errors?.Add(new ParsingError(ErrorSource.Tokenizer, message, new Span(reader.Location, new Location(reader.Location.Line, reader.Location.Column + 1)))); } var buffer = new StringBuilder(); var encoding = new UTF32Encoding(true, false, true); int current; while ((current = reader.Read()) != -1) { var ch = (char)current; switch (ch) { case '!': yield return(Token.Exclamation.WithEndLocation(reader.Location)); break; case '"': { buffer.Clear(); while ((current = reader.Read()) != -1) { if (IsHexDigit(current)) { buffer.Append((char)current); } else if (current == '"') { if (buffer.Length % 8 != 0) { emitError(Errors.TokenizerUnbalancedOctets); break; } else { var result = encoding.GetString(SoapHexBinary.Parse(buffer.ToString()).Value); yield return(new Token(result, TokenKind.EncodedStringLiteral, CreateSpan(reader.Location, buffer.Length + 2))); break; } } else { emitError(Errors.TokenizerOnlyHex); break; } } break; } case '#': yield return(Token.Pound.WithEndLocation(reader.Location)); break; case '$': yield return(Token.Dollar.WithEndLocation(reader.Location)); break; case '%': { buffer.Clear(); var next = reader.Peek(); if (next == '1' || next == '0') { while (IsBit(reader.Peek())) { buffer.Append((char)reader.Read()); } yield return(new Token(buffer.ToString(), TokenKind.BinaryLiteral, CreateSpan(reader.Location, buffer.Length))); } else { emitError(Errors.TokenizerInvalidBinaryLiteral); } break; } case '&': yield return(Token.Ampersand.WithEndLocation(reader.Location)); break; case '*': { if (reader.Peek() == '*') { reader.Read(); yield return(Token.Exponent.WithEndLocation(reader.Location)); } else { yield return(Token.Multiply.WithEndLocation(reader.Location)); } break; } case '+': yield return(Token.Plus.WithEndLocation(reader.Location)); break; case ',': yield return(Token.Comma.WithEndLocation(reader.Location)); break; case '-': { if (reader.Peek() == '-') { while ((current = reader.Read()) != -1) { if (current == '\n') { break; } } } else { yield return(Token.Minus.WithEndLocation(reader.Location)); } break; } case '.': yield return(Token.Period.WithEndLocation(reader.Location)); break; case '/': yield return(Token.Slash.WithEndLocation(reader.Location)); break; case ':': { var next = reader.Peek(); if (next == '=') { reader.Read(); if (reader.Peek() == ':') { reader.Read(); yield return(Token.InstanceEqual.WithEndLocation(reader.Location)); } else { yield return(Token.Assignment.WithEndLocation(reader.Location)); } } else if (next == '<') { reader.Read(); if (reader.Peek() == '>') { reader.Read(); if (reader.Peek() == ':') { reader.Read(); yield return(Token.InstanceNotEqual.WithEndLocation(reader.Location)); } else { yield return(Token.Colon.WithEndLocation(new Location(reader.Location.Line, reader.Location.Column - 2))); yield return(Token.NotEqual.WithEndLocation(reader.Location)); } } else { yield return(Token.Colon.WithEndLocation(new Location(reader.Location.Line, reader.Location.Column - 1))); yield return(Token.LessThan.WithEndLocation(reader.Location)); } } else { yield return(Token.Colon.WithEndLocation(reader.Location)); } break; } case ';': yield return(Token.Semicolon.WithEndLocation(reader.Location)); break; case '<': { var next = reader.Peek(); if (next == '=') { reader.Read(); yield return(Token.LessThanOrEqual.WithEndLocation(reader.Location)); } else if (next == '>') { reader.Read(); yield return(Token.NotEqual.WithEndLocation(reader.Location)); } else if (next == '*') { reader.Read(); yield return(Token.Query.WithEndLocation(reader.Location)); } else { yield return(Token.LessThan.WithEndLocation(reader.Location)); } break; } case '=': yield return(Token.Equal.WithEndLocation(reader.Location)); break; case '>': { if (reader.Peek() == '=') { reader.Read(); yield return(Token.GreaterThanOrEqual.WithEndLocation(reader.Location)); } else { yield return(Token.GreaterThan.WithEndLocation(reader.Location)); } break; } case '?': yield return(Token.QuestionMark.WithEndLocation(reader.Location)); break; case '@': yield return(Token.At.WithEndLocation(reader.Location)); break; case '[': yield return(Token.LeftBracket.WithEndLocation(reader.Location)); break; case '\\': yield return(Token.Backslash.WithEndLocation(reader.Location)); break; case ']': yield return(Token.RightBracket.WithEndLocation(reader.Location)); break; case '^': yield return(Token.Caret.WithEndLocation(reader.Location)); break; case '_': yield return(Token.Underscore.WithEndLocation(reader.Location)); break; case '`': yield return(Token.Backtick.WithEndLocation(reader.Location)); break; case '{': yield return(Token.LeftBrace.WithEndLocation(reader.Location)); break; case '|': { if (reader.Peek() == '|') { reader.Read(); yield return(Token.ComplexEntityConstruction.WithEndLocation(reader.Location)); } else { yield return(Token.Pipe.WithEndLocation(reader.Location)); } break; } case '}': yield return(Token.RightBrace.WithEndLocation(reader.Location)); break; case '~': yield return(Token.Tilde.WithEndLocation(reader.Location)); break; case '(': { if (reader.Peek() == '*') { reader.Read(); int remarksNesting = 1; while ((current = reader.Read()) != -1) { if (current == '(' && reader.Peek() == '*') { reader.Read(); remarksNesting++; } else if (current == '*' && reader.Peek() == ')') { reader.Read(); remarksNesting--; } if (remarksNesting == 0) { break; } } break; } else { yield return(Token.LeftParen.WithEndLocation(reader.Location)); } break; } case ')': yield return(Token.RightParen.WithEndLocation(reader.Location)); break; case '\'': { buffer.Clear(); while ((current = reader.Peek()) != -1) { if (current == '\n') { emitError(Errors.TokenizerUnexpectedNewlineInStringLiteral); yield return(new Token(buffer.ToString(), TokenKind.SimpleStringLiteral, CreateSpan(reader.Location, buffer.Length + 1))); break; } else if (current == '\'') { reader.Read(); if (reader.Peek() == '\'') { buffer.Append("'"); } else { yield return(new Token(buffer.ToString(), TokenKind.SimpleStringLiteral, CreateSpan(reader.Location, buffer.Length + 2))); break; } } else if (current == -1) { yield return(new Token(buffer.ToString(), TokenKind.SimpleStringLiteral, CreateSpan(reader.Location, buffer.Length + 1))); } else { buffer.Append((char)reader.Read()); } } break; } case char letter when IsLetter(letter): { buffer.Clear(); buffer.Append(letter); while (IsLetterOrDigitOrUnderscore(reader.Peek())) { letter = (char)reader.Read(); buffer.Append(letter); } if (Keywords.IsKeyword(buffer.ToString())) { yield return(Token.FromKeyword(buffer.ToString()).WithEndLocation(reader.Location)); } else { yield return(new Token(buffer.ToString(), TokenKind.SimpleId).WithEndLocation(reader.Location)); } break; } case char digit when(digit >= '0' && digit <= '9'): { buffer.Clear(); buffer.Append(digit); while (IsDigit(reader.Peek())) { buffer.Append((char)reader.Read()); } if (reader.Peek() == '.') { reader.Read(); buffer.Append("."); while (IsDigit(reader.Peek())) { buffer.Append((char)reader.Read()); } current = reader.Peek(); if (current == 'e' || current == 'E') { reader.Read(); buffer.Append('e'); current = reader.Peek(); if (current == '-') { reader.Read(); buffer.Append('-'); } else if (current == '+') { reader.Read(); } if (IsDigit(reader.Peek())) { while (IsDigit(reader.Peek())) { buffer.Append((char)reader.Read()); } } else { emitError(Errors.TokenizerMissingExponent); break; } } if (double.TryParse(buffer.ToString(), NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out var result)) { yield return(new Token(result.ToString(CultureInfo.InvariantCulture), TokenKind.RealLiteral, CreateSpan(reader.Location, buffer.Length))); } else { emitError(Errors.TokenizerRealLiteralOutOfRange); } } else { if (int.TryParse(buffer.ToString(), out int result)) { yield return(new Token(result.ToString(), TokenKind.IntegerLiteral, CreateSpan(reader.Location, buffer.Length))); } else { emitError(Errors.TokenizerIntegerLiteralIsTooLong); } } break; } case char whiteSpace when IsWhiteSpace(whiteSpace): break; default: emitError(string.Format(Errors.TokenizerUnrecognizedCharacter, ch)); break; } } yield return(Token.Eof); }