{"id":6352,"date":"2022-06-29T14:47:02","date_gmt":"2022-06-29T13:47:02","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=6352"},"modified":"2022-06-29T14:47:03","modified_gmt":"2022-06-29T13:47:03","slug":"base32-encoding","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/base32-encoding\/","title":{"rendered":"Base32 encoding"},"content":{"rendered":"\n<p>Source: <a href=\"https:\/\/stackoverflow.com\/a\/7135008\">https:\/\/stackoverflow.com\/a\/7135008<\/a><\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">public class Base32Encoding\n{\n    public static byte[] ToBytes(string input)\n    {\n        if (string.IsNullOrEmpty(input)) throw new ArgumentNullException(\"input\");\n\n        input = input.TrimEnd('='); \/\/ remove padding characters\n        int byteCount = input.Length * 5 \/ 8; \/\/ this must be TRUNCATED\n        byte[] returnArray = new byte[byteCount];\n        \n        byte curByte = 0, bitsRemaining = 8;\n        int mask = 0, arrayIndex = 0;\n\n        foreach (char c in input)\n        {\n            int cValue = CharToValue(c);\n            if (bitsRemaining > 5)\n            {\n                mask = cValue &lt;&lt; (bitsRemaining - 5);\n                curByte = (byte)(curByte | mask);\n                bitsRemaining -= 5;\n            }\n            else\n            {\n                mask = cValue >> (5 - bitsRemaining);\n                curByte = (byte)(curByte | mask);\n                returnArray[arrayIndex++] = curByte;\n                curByte = (byte)(cValue &lt;&lt; (3 + bitsRemaining));\n                bitsRemaining += 3;\n            }\n        }\n        if (arrayIndex != byteCount) \/\/ if we didn't end with a full byte\n            returnArray[arrayIndex] = curByte;\n        return returnArray;\n    }\n\n    public static string ToString(byte[] input)\n    {\n        if (input == null || input.Length == 0) throw new ArgumentNullException(\"input\");\n\n        int charCount = (int)Math.Ceiling(input.Length \/ 5d) * 8;\n        char[] returnArray = new char[charCount];\n\n        byte nextChar = 0, bitsRemaining = 5;\n        int arrayIndex = 0;\n\n        foreach (byte b in input)\n        {\n            nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining)));\n            returnArray[arrayIndex++] = ValueToChar(nextChar);\n            \n            if (bitsRemaining &lt; 4)\n            {\n                nextChar = (byte)((b >> (3 - bitsRemaining)) &amp; 31);\n                returnArray[arrayIndex++] = ValueToChar(nextChar);\n                bitsRemaining += 5;\n            }\n            bitsRemaining -= 3;\n            nextChar = (byte)((b &lt;&lt; bitsRemaining) &amp; 31);\n        }\n\n        if (arrayIndex != charCount) \/\/if we didn't end with a full char\n        {\n            returnArray[arrayIndex++] = ValueToChar(nextChar);\n            while (arrayIndex != charCount) returnArray[arrayIndex++] = '='; \/\/padding\n        }\n        return new string(returnArray);\n    }\n\n    private static int CharToValue(char c)\n    {\n        int value = (int)c;\n        if (value &lt; 91 &amp;&amp; value > 64) return value - 65; \/\/65-90 == uppercase letters\n        if (value &lt; 56 &amp;&amp; value > 49) return value - 24; \/\/50-55 == numbers 2-7\n        if (value &lt; 123 &amp;&amp; value > 96) return value - 97; \/\/97-122 == lowercase letters\n        throw new ArgumentException(\"Character is not a Base32 character.\", \"c\");\n    }\n\n    private static char ValueToChar(byte b)\n    {\n        if (b &lt; 26) return (char)(b + 65);\n        if (b &lt; 32) return (char)(b + 24);\n        throw new ArgumentException(\"Byte is not a value Base32 value.\", \"b\");\n    }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/stackoverflow.com\/a\/7135008<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6352","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=6352"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6352\/revisions"}],"predecessor-version":[{"id":6353,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6352\/revisions\/6353"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=6352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=6352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=6352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}