Skip to content

Instantly share code, notes, and snippets.

@machinamentum
Last active December 14, 2015 21:08
Show Gist options
  • Save machinamentum/5148682 to your computer and use it in GitHub Desktop.
Save machinamentum/5148682 to your computer and use it in GitHub Desktop.
import java.io.UnsupportedEncodingException;
/**
* Converts a given String to base 26 then spits out its decimal equivalent.
* @author joshuahuelsman
*/
public class StringToNumber {
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(args[0]);
byte[] b = args[0].toUpperCase().getBytes("UTF-8");
long number = 0;
for(int i = 0; i < b.length; i++) {
b[i] -= 'A';
}
for(int i = b.length - 1; i >= 0; i--) {
number += b[i] * (Math.pow(26, (b.length - 1) - i));
}
System.out.println(number);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment