Encode String base 32 - CSharp System

CSharp examples for System:String Base32

Description

Encode String base 32

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*  ww w  .j a  v a 2  s  . co m*/

public class Main{
        static string Encode32(string input)
      {
         var output = "";
         var i = 0;
         do
         {
            var chr1 = (uint)input[i++];
            var chr2 = i >= input.Length ? 0 : (uint)input[i++];
            var chr3 = i >= input.Length ? 0 : (uint)input[i++];
            var enc1 = chr1 >> 2;
            var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            var enc4 = chr3 & 63;
            if (chr2 == 0)
            {
               enc3 = enc4 = 64;
            }
            else if (chr3 == 0)
            {
               enc4 = 64;
            }
            output = output + _keyStr[(int)enc1] + _keyStr[(int)enc2] + _keyStr[(int)enc3] + _keyStr[(int)enc4];
         } while (i < input.Length);
         return output;
      }
}

Related Tutorials