how to add all integer values of string using java? - java

String something = "123e45,j, _122";
int length = something.length();
String result = "";
for (int i = 0; i < length; i++) {
Character character = something.charAt(i);
if (Character.isDigit(character)) {
result += character;
}
}
// out.println("result is:"+result);
Here I'm getting value in resut as:12345122 but I need to calculate sum of all values ,that is 20.

Change type of result to int.
Parse the char to an integer. If you only cast the char to an integer, you add the ascii value of this character to your result.
String something = "123e45,j, _122";
int length = something.length();
int result = 0;
for (int i = 0; i < length; i++) {
Character character = something.charAt(i);
if (Character.isDigit(character)) {
result += Character.getNumericValue(character);
}
}
System.out.println("result is: "+result);
Output: result is: 20

you can use regex like this :
public static void main(String[] args) {
String s = "123e45,j, _122";
int val = 0;
Pattern p = Pattern.compile("\\d"); // match single digit
Matcher m = p.matcher(s);
while (m.find()) { // for all digits
val = val + Integer.parseInt(m.group()); // covert each String (digit) into numeric value and add to count
}
System.out.println(val);
}
O/P :
20

You could stay with your loop and use StringUtils.isNumeric()

String something = "123e45,j, _122";
int length = something.length();
int sum=0;
for (int i = 0; i < length; i++) {
Character character = something.charAt(i);
if (Character.isDigit(character)) {
sum+= Integer.parseInt( Character.toString(character) );
}
}
// out.println("result is:"+sum);
This works. sum is an integer now you can use it to perform calculations or you can parse it to String again with: String result = String.valueOf(sum);

While the solutions of others will get you the desired result with the help of some libraries and wrappers, but I think once you get what happens in this piece of code, your basic understanding of the other code pieces increases as well.
int sumString(String some) {
int sum = 0;
for (int i = 0; i < some.length(); i++)
sum += (some.charAt(i) >= '0' && some.charAt(i) <= '9') ? (some.charAt(i) - '0') : 0;
return sum;
}
In short: for every index of the string, check of the character is numeric by checking if the character value is between 0 and 9. If it is, convert it into its integer value by subtracting the ASCII value of 0 (0 = 48 and 9 = 57, and if we subtract 48 to 48, you get 0, and 57 - 48 = 9)

Related

Adding numbers which are stored in string variables

Given two non-negative numbers num1 and num2 represented as strings, return the sum of num1 and num2.
The length of both num1 and num2 is less than 5100.
Both num1 and num2 contain only digits 0-9.
Both num1 and num2 do not contain any leading zeros.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
I tried my solution but it doesn't work. Suggestions?
public class Solution {
public String addStrings(String num1, String num2) {
double multiplier = Math.pow(10, num1.length() - 1);
int sum = 0;
for (int i = 0; i < num1.length(); i++){
sum += ((((int) num1.charAt(i)) - 48) * multiplier);
multiplier /= 10;
}
multiplier = Math.pow(10, num2.length() - 1);
for (int i = 0; i < num2.length(); i++){
sum += ((((int) num2.charAt(i)) - 48) * multiplier);
multiplier /= 10;
}
return "" + sum;
}
}
You must not use any built-in BigInteger library or convert the inputs to integer directly.
Note that you are adding two integers of up to 5100 digits each. That is not that max value, but the max number of digits.
An int (your sum variable) cannot hold values like that. BigInteger can, but you're not allowed to use it.
So, add the numbers like you would on paper: Add last digits, write lower digit of the sum as last digit of result, and carry-over a one if needed. Repeat for second-last digit, third-last digit, etc. until done.
Since the sum will be at least the number of digits of the longest input value, and may be one longer, you should allocate a char[] of length of longest input plus one. When done, construct final string using String(char[] value, int offset, int count), with an offset of 0 or 1 as needed.
The purpose of this question is to add the numbers in the string form. You should not try to convert the strings to integers. The description says the length of the numbers could be up to 5100 digits. So the numbers are simply too big to be stored in integers and doubles. For instance In the following line:
double multiplier = Math.pow(10, num1.length() - 1);
You are trying to store 10^5100 in a double. In IEEE 754 binary floating point standard a double can a store number from ±4.94065645841246544e-324 to ±1.79769313486231570e+308. So your number won't fit. It will instead turn into Infinity. Even if it fits in double it won't be exact and you will encounter some errors in your follow up calculations.
Because the question specifies not to use BigInteger or similar libraries you should try and implement string addition yourself.
This is pretty straightforward just implement the exact algorithm you follow when you add two numbers on paper.
Here is working example of adding two strings without using BigInteger using char array as intermediate container. The point why double can't be used has been explained on #Tempux answer. Here the logic is similar to how adding two numbers on paper works.
public String addStrings(String num1, String num2) {
int carry = 0;
int m = num1.length(), n = num2.length();
int len = m < n ? n : m;
char[] res = new char[len + 1]; // length is maxLen + 1 incase of carry in adding most significant digits
for(int i = 0; i <= len ; i++) {
int a = i < m ? (num1.charAt(m - i - 1) - '0') : 0;
int b = i < n ? (num2.charAt(n - i - 1) - '0') : 0;
res[len - i] = (char)((a + b + carry) % 10 + '0');
carry = (a + b + carry) / 10;
}
return res[0] == '0' ? new String(res, 1, len) : new String(res, 0, len + 1);
}
This snippet is relatively small and precise because here I didn't play with immutable String which is complicated/messy and yield larger code. Also one intuition is - there is no way of getting larger output than max(num1_length, num2_length) + 1 which makes the implementation simple.
You have to addition as you do on paper
you can't use BigInteger and the String Length is 5100, so you can not use int or long for addition.
You have to use simple addition as we do on paper.
class AddString
{
public static void main (String[] args) throws java.lang.Exception
{
String s1 = "98799932345";
String s2 = "99998783456";
//long n1 = Long.parseLong(s1);
//long n2 = Long.parseLong(s2);
System.out.println(addStrings(s1,s2));
//System.out.println(n1+n2);
}
public static String addStrings(String num1, String num2) {
StringBuilder ans = new StringBuilder("");
int n = num1.length();
int m = num2.length();
int carry = 0,sum;
int i, j;
for(i = n-1,j=m-1; i>=0&&j>=0;i--,j--){
int a = Integer.parseInt(""+num1.charAt(i));
int b = Integer.parseInt(""+num2.charAt(j));
//System.out.println(a+" "+b);
sum = carry + a + b;
ans.append(""+(sum%10));
carry = sum/10;
}
if(i>=0){
for(;i>=0;i--){
int a = Integer.parseInt(""+num1.charAt(i));
sum = carry + a;
ans.append(""+(sum%10));
carry = sum/10;
}
}
if(j>=0){
for(;j>=0;j--){
int a = Integer.parseInt(""+num2.charAt(j));
sum = carry + a;
ans.append(""+(sum%10));
carry = sum/10;
}
}
if(carry!=0)ans.append(""+carry);
return ans.reverse().toString();
}
}
You can run the above code and see it works in all cases, this could be written in more compact way, but that would have been difficult to understand for you.
Hope it helps!
you can use this one that is independent of Integer or BigInteger methods
public String addStrings(String num1, String num2) {
int l1 = num1.length();
int l2 = num2.length();
if(l1==0){
return num2;
}
if(l2==0){
return num1;
}
StringBuffer sb = new StringBuffer();
int minLen = Math.min(l1, l2);
int carry = 0;
for(int i=0;i<minLen;i++){
int ind = l1-i-1;
int c1 = num1.charAt(ind)-48;
ind = l2-i-1;
int c2 = num2.charAt(ind)-48;
int add = c1+c2+carry;
carry = add/10;
add = add%10;
sb.append(add);
}
String longer = null;
if(l1<l2){
longer = num2;
}
else if(l1>l2){
longer = num1;
}
if(longer!=null){
int l = longer.length();
for(int i=minLen;i<l;i++){
int c1 = longer.charAt(l-i-1)-48;
int add = c1+carry;
carry = add/10;
add = add%10;
sb.append(add);
}
}
return sb.reverse().toString();
}
The method takes two string inputs representing non-negative integers and returns the sum of the integers as a string. The algorithm works by iterating through the digits of the input strings from right to left, adding each digit and any carryover from the previous addition, and appending the resulting sum to a StringBuilder. Once both input strings have been fully processed, any remaining carryover is appended to the output string. Finally, the string is reversed to produce the correct output order.
Hope this will solve the issue.!
public string AddStrings(string num1, string num2)
{
int i = num1.Length - 1, j = num2.Length - 1, carry = 0;
StringBuilder sb = new StringBuilder();
while (i >= 0 || j >= 0 || carry != 0) {
int x = i >= 0 ? num1[i--] - '0' : 0;
int y = j >= 0 ? num2[j--] - '0' : 0;
int sum = x + y + carry;
sb.Append(sum % 10);
carry = sum / 10;
}
char[] chars = sb.ToString().ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
Previous solutions have excess code. This is all you need.
class ShortStringSolution {
static String add(String num1Str, String num2Str) {
return Long.toString(convert(num1Str) + convert(num2Str));
}
static long convert(String numStr) {
long num = 0;
for(int i = 0; i < numStr.length(); i++) {
num = num * 10 + (numStr.charAt(i) - '0');
}
return num;
}
}
class LongStringSolution {
static String add(String numStr1, String numStr2) {
StringBuilder result = new StringBuilder();
int i = numStr1.length() - 1, j = numStr2.length() - 1, carry = 0;
while(i >= 0 || j >= 0) {
if(i >= 0) {
carry += numStr1.charAt(i--) - '0';
}
if(j >= 0) {
carry += numStr2.charAt(j--) - '0';
}
if(carry > 9) {
result.append(carry - 10);
carry = 1;
} else {
result.append(carry);
carry = 0;
}
}
if(carry > 0) {
result.append(carry);
}
return result.reverse().toString();
}
}
public class Solution {
static String add(String numStr1, String numStr2) {
if(numStr1.length() < 19 && numStr2.length() < 19) {
return ShortStringSolution.add(numStr1, numStr2);
}
return LongStringSolution.add(numStr1, numStr2);
}
}
For the sake of comprehension of the question
your method's name is addition
you are trying to do a power operation but the result is stored in a variable named multiplication...
there is more than one reason why that code doesnt work...
You need to do something like
Integer.parseInt(string)
in order to parse strings to integers
here the oficial doc

Java methods isbn char conversion and output problems. Can't find out mistake in algorithm

I'm trying create a method which returns char type. The condition is this: system takes 9 digits from user input and calculate it. The method converts each char in the nextLine() input and computes the sum then take % 11. If the remainder is 10, method return 'X' and if the remainder is 0 through 9, then the method returns that digit but must be in char type. So far I'm lost at why it will always output '/' and nothing else. Please help me find out the mistake of my algorithms.
public static char getCheckSum(String isbn) {
int sum = 0;
char charSum;
for (int i = 0; i < isbn.length(); i++) {
int[] num = new int[isbn.length()];
num[i] = Character.getNumericValue(i) * (i+1);
sum = sum + num[i];
}
int last = (sum % 11);
if (last == 10){
charSum = (char) 88;
} else {
charSum = (char) (last + 48);
}
return charSum;
//this is the next part where it inserts hyphens just as a reference
public static String formatISBNWithHyphens(String isbn) {
// original isbn: 123456789
// possible new isbn: 1-234-56789-X
char isbn10 = getCheckSum(isbn);
String isbn10Str = isbn + Character.toString(isbn10);
// char[] c = new char[isbn10Str.length()]; *leaving this here for future learning.
String[] cStr = new String[isbn10Str.length()];
String isbnStr = "";
for (int i = 0; i < isbn10Str.length(); i++){
cStr[i] = Character.toString(isbn10Str.charAt(i));
// c[i] = isbn10Str.charAt(i); *leaving this here for future learning.
if (i == 0 || i == 3 || i == 8 ) {
cStr[i] += '-';
}
isbnStr += cStr[i];
}
return isbnStr;
}
// The final outcome is always like this 321654987/ and 3-216-54987-/
it is supposed to be either numbers from 0 to 9 or X if remainder is 10.
Please help. Thanks a bunch.
I think the problem is here
for (int i = 0; i < isbn.length(); i++) {
int[] num = new int[isbn.length()];
num[i] = Character.getNumericValue(i) * (i+1);
sum = sum + num[i];
}
The for cycle returning a result has no relationship with the isbn's content, the result just depends on the isbn string length!
so you can change the code to the following one below
for (int i = 0; i < isbn.length(); i++) {
int[] num = new int[isbn.length()];
num[i] = Character.getNumericValue(isbn.charAt(i));
sum = sum + num[i];
}
the code above returns a result depending on the isbn's content

Generating Binary Sequences

I want to generate every possible binary sequence of numbers, where each sequence in the list is limited to a specific number of 1's and there is padding of zeros to make every list the same length.
For example, if the sequence is supposed to be 4 numbers long, and have 2 ones, all sequences would be:
1100 1010 1001 0110 0101 0011
and the zeros at the front of the number are preserved.
This can be solved using recursive function calls:
public class BinarySequences {
public static void main(String[] args) {
final int numCount = 4;
final int oneCount = 2;
checkSubString(numCount, oneCount, "");
for (String res : results) {
System.out.println(res);
}
}
private static List<String> results = new ArrayList<>();
private static void checkSubString(int numCount, int oneCount, String prefix) {
if ((numCount >= oneCount) && (oneCount >= 0)) {
if (numCount==1) {
if (oneCount==1) {
results.add(prefix + "1");
} else {
results.add(prefix + "0");
}
} else {
checkSubString(numCount-1, oneCount , prefix + "0");
checkSubString(numCount-1, oneCount-1, prefix + "1");
}
}
}
}
If you want to preserve the 0s, then just add padding:
int end = 100; //Change this
for (int i = 0; i <= end; i++) {
String bytestring = Integer.toBinaryString(i);
String padding = "00000000000000000000000000000000";
bytestring = padding.substring(0, 32 - bytestring.length()) + bytestring;
System.out.println(bytestring);
}
Try this:
//Edit your min and max, e.g. Integer.MAX_VALUE and Integer.MIN_VALUE
int min = 0;
int max = 10;
for(int i = min; i < max; i++){
//Make 16 bit long binary Strings
String s = String.format("%16s", Integer.toBinaryString(i)).replace(' ', '0');
//Make 4 bits long chunks
List<String> chunks = new ArrayList<>();
Matcher matcher = Pattern.compile(".{0,4}").matcher(s);
while (matcher.find()) {
chunks.add(s.substring(matcher.start(), matcher.end()));
}
StringBuilder b = new StringBuilder();
for (String c : chunks) {
//Here you can count the 1 and 0 of the current chunk with c.charAt(index)
b.append(c);
b.append(" ");
}
System.out.println(b.toString());
}
Requires org.apache.commons.lang.StringUtils, but this makes it a short one:
final int digits = 4;
final int onesrequired = 2;
int maxindex = (int) Math.pow(2, digits);
for (int i = 0; i < maxindex; i++) {
String binaryStr = Integer.toBinaryString(i);
if (StringUtils.countMatches(binaryStr, "1") == onesrequired) {
System.out.print(String.format("%" + digits + "s", binaryStr).replace(' ', '0') + " ");
}
}

I'm curious how i would grab a char that is the smallest/biggest out of the rest in said string without using ARRAYS

I'm just having a hard time grasping this concept.
each char has a different ASCII value, so how do i grab the lowest value or the highest value?
and if i passed an empty string to my method for all of this min() would just get thrown an error or would it return a 0?
i wrote a test driver that should pass if my min method returns w as the minimum, which is just a stub method right now, character in that string.
final String PASS = "Pass";
final String FAIL = "Fail";
L05A lab5 = new L05A();
int testNum = 1;
String tst = ""; // test empty string
String result = FAIL;
System.out.println("\nTesting min\n");
tst = "";
char ch = lab5.min(tst);
result = (ch == '!') ? PASS : FAIL;
System.out.println(testNum + ": " + result);
++testNum;
tst = "zyxw"; //would return w?
ch = lab5.min(tst);
result = (ch == 'w') ? PASS : FAIL;
System.out.println(testNum + ": " + result);
++testNum;
So how would i scan that string i pass to return the smallest char?
At first i thought i could use str.charAt(0); but silly me, that just returns the first index of the string, so i'm very confused! Any help would be great to develop this min method
I SHOULD SPECIFY THAT WE ARE NOT USING ANY FORM OF ARRAYS[] ON THIS ASSIGNMENT
UNFORTUNATELY.. :(
It's pretty simple:
Convert the string to a char array using String.toCharArray
Convert the array to a Collection<Character>
Pass the collection to Collections.min and max from java.util.Collections
For example:
String test = "test";
List<Character> strArr = new ArrayList<Character>(test.length());
for (char c : test.toCharArray()) strArr.add(c);
System.out.println(Collections.min(strArr)); // prints "e"
EDIT Ok, so now you say you can't use Arrays, so you just do this instead:
String test = "test";
char[] chars = test.toCharArray();
char max = Character.MIN_VALUE;
char min = Character.MAX_VALUE;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
max = max > c ? max : c;
min = min < c ? min : c;
}
System.out.println(min);
And, finally, if you can't use an array (like char[]) then you just drop the toCharArray call, and start the loop like this:
for (int i = 0; i < test.length(); i++) {
char c = test.charAt(i);
get char array out of string
iterate over it
define temp int variable assign to 0
compare ASCII of char to temp var and assign temp var to ascii if temp var is smaller/bigger(based on min max value you need from that function)
once the loop is over, you have what you want in that temp var
You should sort the array first:
Arrays.sort(arr);
Then your minimum value will be its first element:
int minimum = arr[0];
Do note that Arrays.sort does sorting in-place and returns nothing.
I expect there is a utility class which will do this for you, but what you could do is call toCharArray on the string, iterate over the array it returns and look for the smallest char (char is effectively a number so comparisons like < > <= >= etc will work on it)
edit:
String foo = "ksbvs";
final char[] chars = foo.toCharArray();
// create a variable to bung the smallest char in
// iterate over the array chars
// compare the current char to the smallest char you have seen so far, update if it's smaller
public class TestCharASCIIValue {
public static void main(String[] args) {
String slogan = "Programming Java Makes me go nutts"; // The String under test
int maxASCII = 0; // As minimum value for a character in ASCII is 0.
int minACSII = 255; // As maximum value for a character in ASCII is 255
int stringLength = slogan.length();
for(int i =0; i < stringLength; i++) {
int tempASCII = slogan.codePointAt(i);
if(tempASCII > maxASCII)
maxASCII = tempASCII;
else if (tempASCII < minACSII)
minACSII = tempASCII;
}
System.out.println("Max : " + maxASCII + " MIN : " + minACSII);
char maxChar = (char)maxASCII;
char minChar = (char) minACSII;
System.out.println("MaxChar : "+maxChar +"\t MinChar : " + minChar);
}
}
OUTPUT :
Max : 118 MIN : 32
MaxChar : v MinChar : (blank space)
EDIT : As per your Query, If you want to test only for max or min valued char then you can use this.
public class TestCharASCIIValue {
public static void main(String[] args) {
String slogan = "Programming Java Makes me go nutts";
char maxChar = (char)getMaxChar(slogan);
char minChar = (char)getMinChar(slogan);
System.out.println("MaxChar : "+maxChar +"\t MinChar : " + minChar);
}
private static int getMaxChar(String testString) {
int maxASCII = 0;
int stringLength = testString.length();
if(stringLength == 0) {
return -1;
}
for(int i =0; i < stringLength; i++) {
int tempASCII = testString.codePointAt(i);
if(tempASCII > maxASCII)
maxASCII = tempASCII;
}
return maxASCII;
}
private static int getMinChar(String testString) {
int minASCII = 255;
int stringLength = testString.length();
if(stringLength == 0) {
return -1;
}
for(int i =0; i < stringLength; i++) {
int tempASCII = testString.codePointAt(i);
if(tempASCII < minASCII)
minASCII = tempASCII;
}
return minASCII;
}
}

How to convert integers to base64 (0-9A-Za-z)

I have been trying to reduce the length of the way. I represent some integer ID's in my program. For Example
2
3
15
26
63
...
151564852
I would like them to be represented as such (0-9A-Za-z only)
2
3
F
Q
z
...
vDF25a //For example
The approach I thought of is to have 63 if statements which each of the mappings from 0-63 to 0-z respectively and for anything above 64 do a recursion on the value minus 63.
Needless to say, I think my approach is very flawed and impractical. What would be a more appropriate way of doing it?
Update:
Following fge's suggestion I've got the encoder to work correctly, however my decode function only works for up-to length 2 strings, in cases where the string is larger the sum becomes erroneous. For example for 3840 to 3845 this is the output
// Encoded
zw
x
zy
zz
100
// Decoded
3840
3841
3842
3843
124 //Invalid decoding
Here is my code for the decode function
public static int decode(String value)
{
String revStr = new StringBuilder(value).reverse().toString();
int sum = 0;
for (int i=1; i < revStr.length(); i++)
{
for (int j=0; j < ALPHABET.length; j++)
{
if (ALPHABET[j] == revStr.charAt(i))
{
sum += (ALPHABET.length * j) * i;
break;
}
}
}
for (int j=0; j < ALPHABET.length; j++)
{
if (ALPHABET[j] == revStr.charAt(0))
{
sum += j;
break;
}
}
return sum;
}
This is not base64; base64 encodes binary data.
Anyway, you don't need a s*load of if statements; use an array:
public final class AlphabetEncoder
{
private static final char[] ALPHABET = { '0', '1', '2', ...., 'z' };
private static final int ENCODE_LENGTH = ALPHABET.length;
public static String encode(int victim)
{
final List<Character> list = new ArrayList<>();
do {
list.add(ALPHABET[victim % ENCODE_LENGTH]);
victim /= ENCODE_LENGTH;
} while (victim > 0);
Collections.reverse(list);
return new String(list.toArray(new char[list.size()],
StandardCharsets.UTF_8);
}
public int decode(final String encoded)
{
int ret = 0;
char c;
for (int index = 0; index < encoded.length(); index++) {
c = encoded.charAt(index);
ret *= ENCODE_LENGTH;
ret += Arrays.binarySearch(ALPHABET, c);
}
return ret;
}
}
NOTE ABOUT THE DECODE FUNCTION: it is possible to use Arrays.binarySearch() here since the alphabet has the nice property of being naturally sorted (0 < 1 < 2 < ... < z). However, a test should probably be added that its return code not be negative!
Depending on the language you use, there should already be a module which converts a string from/to base64.
Check this other post: Base64 Encoding in Java
You can refer to already existing logic for converting from Decimal [0-9] to Hexadecimal conversion present in Integer class and extend the logic for your Base 64 converison. Refer
Integer.toHexString(int i)
This maybe the efficient implementation for conversion.
My thanks to the #fge answer.
Using it with some changes I've get it to support much larger integers with BigInteger, added support of negative integers and changed Arrays.binarySearch() with HashMap.
BTW, it should be called base62 encoding because [0-9A-Za-z] contains just 62 chars.
public class Base62{
private static final char[] ALPHABET = new char[ 62 ];
private static final Map<Character, Integer> ALPHABET_MAPPING = new HashMap<>();
private static final BigInteger ENCODE_LENGTH = BigInteger.valueOf( ALPHABET.length );
static{
int position = 0;
// numbers
for( int i = 48; i <= 57; i++ ){
ALPHABET[ position++ ] = (char)i;
}
// uppercase letters
for( int i = 65; i <= 90; i++ ){
ALPHABET[ position++ ] = (char)i;
}
// lowercase letters
for( int i = 97; i <= 122; i++ ){
ALPHABET[ position++ ] = (char)i;
}
for( int i = 0; i < ALPHABET.length; i++ ){
ALPHABET_MAPPING.put( ALPHABET[ i ], i );
}
}
public static String encode( final BigInteger in ){
final List<Character> list = new ArrayList<>();
boolean negative = in.signum() == -1;
BigInteger use;
if( negative ){
use = in.negate();
} else {
use = in;
}
do{
BigInteger[] divisionResultAndReminder = use.divideAndRemainder( ENCODE_LENGTH );
list.add( ALPHABET[ divisionResultAndReminder[ 1 ].intValue() ] );
use = divisionResultAndReminder[ 0 ];
} while( use.equals( BigInteger.ZERO ) == false );
Collections.reverse( list );
char[] res = new char[ list.size() ];
for( int i = 0; i < list.size(); i++ ){
res[ i ] = list.get( i );
}
return ( negative ? "-" : "" ) + new String( res );
}
public static BigInteger decode( final String encoded ){
BigInteger res = BigInteger.ZERO;
char c;
boolean negative;
String use;
if( '-' == encoded.charAt( 0 ) ){
negative = true;
use = encoded.substring( 1 );
} else {
negative = false;
use = encoded;
}
for( int index = 0; index < use.length(); index++ ){
c = use.charAt( index );
res = res.multiply( ENCODE_LENGTH );
res = res.add( BigInteger.valueOf( ALPHABET_MAPPING.get( c ) ) );
}
return negative ? res.negate() : res;
}
}

Categories

Resources