/* Thor Server 2.0 Project - Codenamed "Stockholm" Copyright 2008 - 2009 Joe Hegarty This file is part of The Thor Server 2.0 Project - Codenamed "Stockholm". The Thor Server 2.0 Project - Codenamed "Stockholm" is free software: you can redistribute it and/or modify it under the terms of the Microsoft Public License The Thor Server Project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Microsoft Public License for more details. You should have received a copy of the Microsoft Public License along with The Thor Server Project. If not, see http://www.opensource.org/licenses/ms-pl.html */ using System; using System.Text; namespace StockholmLibrary.Net { //------------------------------------ public static class B64Encoding { //********************************************** public static byte[] EncodeInt(int value, int length) { byte[] result = new byte[length]; for (int x = 1; x <= length; x++) { int offset = 6 * (length - x); byte val = (byte)(64 + (value >> offset & 0x3f)); result[x - 1] = val; } return result; } //********************************************** public static int DecodeInt(byte[] val) { return DecodeInt(val, val.Length); } //********************************************** public static int DecodeInt(byte[] val, int length) { int intTot = 0; int y = 0; for (int x = (length - 1); x >= 0; x--) { int intTmp = (int)((val[x] - 64)); if (y > 0) { intTmp = intTmp * (int)(Math.Pow(64, y)); } intTot += intTmp; y++; } return intTot; } } }