If you think the Android project bitfynd-wallet-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2011 Google Inc./*fromwww.java2s.com*/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package de.schildbach.wallet.util;
import javax.annotation.Nonnull;
import com.google.common.base.Charsets;
/**
* Base43, derived from bitcoinj Base58
*
* @author Andreas Schildbach
*/publicclass Base43
{
privatestaticfinalchar[] ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$*+-./:".toCharArray();
privatestaticfinalint[] INDEXES = newint[128];
static
{
for (int i = 0; i < INDEXES.length; i++)
INDEXES[i] = -1;
for (int i = 0; i < ALPHABET.length; i++)
INDEXES[ALPHABET[i]] = i;
}
publicstatic String encode(@Nonnull byte[] input)
{
if (input.length == 0)
return"";
input = copyOfRange(input, 0, input.length);
// Count leading zeroes.
int zeroCount = 0;
while (zeroCount < input.length && input[zeroCount] == 0)
++zeroCount;
// The actual encoding.
finalbyte[] temp = newbyte[input.length * 2];
int j = temp.length;
int startAt = zeroCount;
while (startAt < input.length)
{
byte mod = divmod43(input, startAt);
if (input[startAt] == 0)
++startAt;
temp[--j] = (byte) ALPHABET[mod];
}
// Strip extra '1' if there are some after decoding.
while (j < temp.length && temp[j] == ALPHABET[0])
++j;
// Add as many leading '1' as there were leading zeros.
while (--zeroCount >= 0)
temp[--j] = (byte) ALPHABET[0];
finalbyte[] output = copyOfRange(temp, j, temp.length);
returnnew String(output, Charsets.US_ASCII);
}
publicstaticbyte[] decode(@Nonnull final String input) throws IllegalArgumentException
{
if (input.length() == 0)
returnnewbyte[0];
finalbyte[] input43 = newbyte[input.length()];
// Transform the String to a base43 byte sequence
for (int i = 0; i < input.length(); ++i)
{
finalchar c = input.charAt(i);
int digit43 = -1;
if (c >= 0 && c < 128)
digit43 = INDEXES[c];
if (digit43 < 0)
thrownew IllegalArgumentException("Illegal character " + c + " at " + i);
input43[i] = (byte) digit43;
}
// Count leading zeroes
int zeroCount = 0;
while (zeroCount < input43.length && input43[zeroCount] == 0)
++zeroCount;
// The encoding
finalbyte[] temp = newbyte[input.length()];
int j = temp.length;
int startAt = zeroCount;
while (startAt < input43.length)
{
byte mod = divmod256(input43, startAt);
if (input43[startAt] == 0)
++startAt;
temp[--j] = mod;
}
// Do no add extra leading zeroes, move j to first non null byte.
while (j < temp.length && temp[j] == 0)
++j;
return copyOfRange(temp, j - zeroCount, temp.length);
}
//
// number -> number / 43, returns number % 43
//
privatestaticbyte divmod43(finalbyte[] number, finalint startAt)
{
int remainder = 0;
for (int i = startAt; i < number.length; i++)
{
finalint digit256 = (int) number[i] & 0xFF;
finalint temp = remainder * 256 + digit256;
number[i] = (byte) (temp / 43);
remainder = temp % 43;
}
return (byte) remainder;
}
//
// number -> number / 256, returns number % 256
//
privatestaticbyte divmod256(finalbyte[] number43, finalint startAt)
{
int remainder = 0;
for (int i = startAt; i < number43.length; i++)
{
finalint digit58 = (int) number43[i] & 0xFF;
finalint temp = remainder * 43 + digit58;
number43[i] = (byte) (temp / 256);
remainder = temp % 256;
}
return (byte) remainder;
}
privatestaticbyte[] copyOfRange(finalbyte[] source, finalint from, finalint to)
{
finalbyte[] range = newbyte[to - from];
System.arraycopy(source, from, range, 0, range.length);
return range;
}
}