// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; namespace E { public class Base60 { private const string ENC = "ABCDEFGHIJKLMNOPQRSTUWVXYZabcdefghijklmnopqrstuwvxyz01234567"; private static int[] SKIP57 = { 0, 1, 2, 4, 5 }; private static int[] SKIP75 = { 0, 1, 2, 3, 3, 4, 5 }; public static string ToBase60(byte[] b) { int n = (b.Length - 1) / 5 + 1; int n2 = 7 * n - SKIP57[5 * n - b.Length]; StringBuilder sb = new StringBuilder(""); for(int i = 0; i < n; i++) { long tmp = 0; for(int j = 4; j >= 0; j--) { if((5 * i + j) < b.Length) { tmp = 256 * tmp + b[5 * i + j]; } } for(int k = 0; k < 7; k++) { if((7 * i + k) < n2) { sb.Append(ENC[(int)(tmp % 60)]); tmp = tmp / 60; } } } return sb.ToString(); } public static byte[] FromBase60(string s) { int n = (s.Length - 1) / 7 + 1; int n2 = 5 * n - SKIP75[7 * n - s.Length]; byte[] b = new byte[n2]; for(int i = 0; i < n; i++) { long tmp = 0; for(int j = 6; j >= 0; j--) { if(7 * i + j < s.Length) { tmp = 60 * tmp + ENC.IndexOf(s[7 * i + j]); } } for(int k = 0; k < 5; k++) { if(5 * i + k < n2) { b[5 * i + k] = (byte)(tmp % 256); tmp = tmp / 256; } } } return b; } } class TestClass { public static void Main(string[] args) { byte[] b1 = { 1, 2, 3, 4, 5, 6, 7, 8 }; string s1 = Base60.ToBase60(b1); Console.WriteLine(s1); byte[] b2 = Base60.FromBase60(s1); for(int i = 0; i < b2.Length; i++) Console.Write(" " + b2[i]); Console.WriteLine(); byte[] b3 = { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 24, 255 }; string s2 = Base60.ToBase60(b3); Console.WriteLine(s2); byte[] b4 = Base60.FromBase60(s2); for(int i = 0; i < b4.Length; i++) Console.Write(" " + b4[i]); Console.WriteLine(); } } }