Created
August 7, 2015 20:16
-
-
Save Pokechu22/96bebb88f8df8188a09a to your computer and use it in GitHub Desktop.
Fixed JSONEncode method for Minecraft Console Client (https://github.com/ORelio/Minecraft-Console-Client/blob/5b430562ca1194c216f05a48d792eae98b0d40ff/MinecraftClient/Protocol/ProtocolHandler.cs#L266-L290)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Encode a string to a json string. | |
/// Will convert special chars to \u0000 unicode escape sequences. | |
/// </summary> | |
/// <param name="text">Source text</param> | |
/// <returns>Encoded text</returns> | |
private static string jsonEncode(string text) | |
{ | |
StringBuilder result = new StringBuilder(); | |
foreach (char c in text) | |
{ | |
if ((c >= '0' && c <= '9') || | |
(c >= 'a' && c <= 'z') || | |
(c >= 'A' && c <= 'Z')) | |
{ | |
result.Append(c); | |
} | |
else | |
{ | |
result.AppendFormat(@"\u{0:x4}", (int)c) | |
} | |
} | |
return result.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment