I wish to have a java method which gives me, index given, a corresponding letter set excel like, so:
258 => IZ (last index)
30 => AD
120 => DR
56 => BD
First method gives correct output, but it's very dumb and I don't like that.
I tried to build a second method that involves a bit of thinking.
I already saw methods using String Builder or something else like this one
but I tried to build a method myself aka betterGetColumnName.
better 258 => IHGFEDCBAX (not ok)
better 30 => AD (OK, 2nd alphabet round it's ok)
better 120 => DCBAP (not ok)
better 56 => BAD (seems like 3rd alphabet round breaks my logic)
public String getColumnName(int index){
String[] letters = {
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R",
"S","T","U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF","AG","AH",
"AI","AJ","AK","AL","AM","AN","AO","AP","AQ","AR","AS","AT","AU","AV",
"AW","AX","AY","AZ","BA","BB","BC","BD","BE","BF","BG","BH","BI","BJ",
"BK","BL","BM","BN","BO","BP","BQ","BR","BS","BT","BU","BV","BW","BX",
"BY","BZ","CA","CB","CC","CD","CE","CG","CH","CI","CJ","CK","CL","CM",
"CN","CO","CP","CQ","CR","CS","CT","CU","CV","CW","CX","CY","CZ","DA",
"DB","DC","DD","DF","DG","DH","DI","DJ","DK","DL","DM","DN","DO","DP",
"DQ","DR","DS","DT","DU","DV","DW","DX","DY","DZ","EA","EB","EC","ED",
"EE","EF","EG","EH","EI","EJ","EK","EL","EM","EN","EO","EP","EQ","ER",
"ES","ET","EU","EV","EW","EX","EY","EZ","FA","FB","FC","FD","FE","FF",
"FG","FH","FI","FJ","FK","FL","FM","FN","FO","FP","FQ","FR","FS","FT",
"FU","FV","FW","FX","FY","FZ","GA","GB","GC","GD","GE","GF","GG","GH",
"GI","GJ","GK","GL","GM","GN","GO","GP","GQ","GR","GS","GT","GU","GV",
"GW","GX","GY","GZ","HA","HB","HC","HD","HE","HF","HG","HH","HI","HJ",
"HK","HL","HM","HN","HO","HP","HQ","HR","HS","HT","HU","HV","HW","HX",
"HY","HZ","IA","IB","IC","ID","IE","IF","IG","IH","II","IJ","IK","IL",
"IM","IN","IO","IP","IQ","IR","IS","IT","IU","IV","IW","IX","IY","IZ"
};
if (index<=letters.length){
return letters[index-1];
}else{
return null;
}
}
I think I should save how many times I made a full alphabet round, I wouldn't use StringBuilder or else, just char, String and integers because at school we can't upgrade java version (1.5.x) also I think it might be useful for me to understand why is my logic so wrong.
public String betterGetColumnName(int index){
int res=0;
String s = "";
char h='0';
while(index>26){
res=index/26;
h=(char)(res+64);
s+=h;
index -=26;
}
h=(char)(index+64);
s+=h;
return s;
}
You are definitely on the right track, though your logic is a bit off. What you are effectively trying to do is to convert a base 10 integer into a base 26 character. But instead of digits, the converted "number" actually consists of the 26 letters of the alphabet.
The algorithm you want here is to determine each letter of the output by taking the remainder of the input number divided by 26. Then, divide the input by 26 and again inspect the "tens" position to see what letter it is. In the code snippet below, I assume that 1 corresponds to A, 26 corresponds to Z, and 27 to AA. You may shift the indices however you feel is best.
int input = 53;
String output = "";
while (input > 0) {
int num = (input - 1) % 26;
char letter = (char)(num+65);
output = letter + output;
input = (input-1) / 26;
}
System.out.println(output);
BA
Demo
Note: A helpful edit was suggested which uses StringBuilder instead of String to do the concatenations. While this might be more optimal than the above code, it might make it harder to see the algorithm.
I just recently started learning Basics in Java, and was testing around initializing a string variable by concatenating primitive variables.
public class Main{
public static void main(String[] args){
byte lbyte = 3;
short lshort = 1;
int lint = 1;
long llong = 0;
float lfloat = 2.0f;
double ldouble = lfloat;
char lchar = 'H';
boolean lbool = true;
String lstring = " w0r1d ";
String lOutput = lchar+lbyte+lshort+lint+llong+lstring+ldouble+' '+lbool;
System.out.println(lOutput);
}
}
In this code, my objective was to create a string that would output:
H3110 w0r1d 2.0 true
However, when i run it the output is: 77 w0r1d 2.0 true
The unexpected result is the 77, while the rest is fine. Even if i would assume the number variables would get added, it would only be a total of 5. The variable lchar is also apparently "absorbed" into the numbers.
Where did the 77 come from and what happened to the H in lchar?
Edit: the goal is to use as much primitive variables as possible in the concatenation.
Edit2: Thanks for all the helpful answers.
The ASCII / Unicode value for 'H' is 72. The additions are processed left to right, so lchar + lbyte is 'H' + (byte) 3 which is equal to 72 + 3.
You'll only get a string result from + if one of the operands is a string. That doesn't happen until you finally concatenate lstring, which explains why all of the numerical (and the char) variables are added together to get 77. Everything to the right of lstring is concatenated one by one, each converted to a string, since the left hand operands of all those +s are all strings at that point.
A quick fix is to start with "" to force everything to be done with strings.
String lOutput = ""+lchar+lbyte+lshort+lint+llong+lstring+ldouble+' '+lbool;
The 77 came from when you were adding the chars. When adding chars it adds their values since they act like integers. Also the other variables which are numbers might have added together. 'H' = 72 in java and 72 + 3 = 75 and 75 + 1 = 76 and finally 76 + 1 = 77.
To fix this you can put "" at the beginning of where you are building your string.
Fix:
String lOutput = ""+lchar+lbyte+lshort+lint+llong+lstring+ldouble+' '+lbool;
Java evaluates expressions like that left to right so what's happening is that you're telling Java to add a char and a byte together and Java will then take the integer value of the character (72) and add the integer value of the byte (3) to it.
So what you're really telling Java is to do this:
String lOutput = (72+3+1+1+0)+" w0r1d"+2.0+' '+true;
When Java comes to +" w0r1d" it will convert the first part into a String and concatenate them, but before that you're adding types where + is defined as addition and therefore Java will sum them up to 77.
To get the desired behaviour you need to start out with a String, so doing this will work as you want it to:
String lOutput = String.valueOf(lchar)+lbyte+lshort+lint+llong+lstring+ldouble+' '+lbool;
This is for automatic type conversion.Look the in the ASCI chart the integer representation of capital 'H' is 72. At first the 'H' is upgraded to int from char and then it is concatenated with the string right to it
I would like to implement a simple substitution cipher to mask private ids in URLs.
I know how my IDs will look like (combination of uppercase ASCII letters, digits and underscore), and they will be rather long, as they are composed keys. I would like to use a longer alphabet to shorten the resulting codes (I'd like to use upper- and lowercase ASCII letters, digits and nothing else). So my incoming alphabet would be
[A-Z0-9_] (37 chars)
and my outgoing alphabet would be
[A-Za-z0-9] (62 chars)
so a compression of almost 50% reasonable amount of compression would be available.
Let's say my URLs look like this:
/my/page/GFZHFFFZFZTFZTF_24_F34
and I want them to look like this instead:
/my/page/Ft32zfegZFV5
Obviously both arrays would be shuffled to bring some random order in.
This does not have to be secure. If someone figures it out: fine, but I don't want the scheme to be obvious.
My desired solution would be to convert the string to an integer representation of radix 37, convert the radix to 62 and use the second alphabet to write out that number. is there any sample code available that does something similar? Integer.parseInt() has some similar logic, but it is hard-coded to use standard digit behavior.
Any ideas?
I am using Java to implement this but code or pseudo-code in any other language is of course also helpful.
Inexplicably Character.MAX_RADIX is only 36, but you can always write your own base conversion routine. The following implementation isn't high-performance, but it should be a good starting point:
import java.math.BigInteger;
public class BaseConvert {
static BigInteger fromString(String s, int base, String symbols) {
BigInteger num = BigInteger.ZERO;
BigInteger biBase = BigInteger.valueOf(base);
for (char ch : s.toCharArray()) {
num = num.multiply(biBase)
.add(BigInteger.valueOf(symbols.indexOf(ch)));
}
return num;
}
static String toString(BigInteger num, int base, String symbols) {
StringBuilder sb = new StringBuilder();
BigInteger biBase = BigInteger.valueOf(base);
while (!num.equals(BigInteger.ZERO)) {
sb.append(symbols.charAt(num.mod(biBase).intValue()));
num = num.divide(biBase);
}
return sb.reverse().toString();
}
static String span(char from, char to) {
StringBuilder sb = new StringBuilder();
for (char ch = from; ch <= to; ch++) {
sb.append(ch);
}
return sb.toString();
}
}
Then you can have a main() test harness like the following:
public static void main(String[] args) {
final String SYMBOLS_AZ09_ = span('A','Z') + span('0','9') + "_";
final String SYMBOLS_09AZ = span('0','9') + span('A','Z');
final String SYMBOLS_AZaz09 = span('A','Z') + span('a','z') + span('0','9');
BigInteger n = fromString("GFZHFFFZFZTFZTF_24_F34", 37, SYMBOLS_AZ09_);
// let's convert back to base 37 first...
System.out.println(toString(n, 37, SYMBOLS_AZ09_));
// prints "GFZHFFFZFZTFZTF_24_F34"
// now let's see what it looks like in base 62...
System.out.println(toString(n, 62, SYMBOLS_AZaz09));
// prints "ctJvrR5kII1vdHKvjA4"
// now let's test with something we're more familiar with...
System.out.println(fromString("CAFEBABE", 16, SYMBOLS_09AZ));
// prints "3405691582"
n = BigInteger.valueOf(3405691582L);
System.out.println(toString(n, 16, SYMBOLS_09AZ));
// prints "CAFEBABE"
}
Some observations
BigInteger is probably easiest if the numbers can exceed long
You can shuffle the char in the symbol String, just stick to one "secret" permutation
Note regarding "50% compression"
You can't generally expect the base 62 string to be around half as short as the base 36 string. Here's Long.MAX_VALUE in base 10, 20, and 30:
System.out.format("%s%n%s%n%s%n",
Long.toString(Long.MAX_VALUE, 10), // "9223372036854775807"
Long.toString(Long.MAX_VALUE, 20), // "5cbfjia3fh26ja7"
Long.toString(Long.MAX_VALUE, 30) // "hajppbc1fc207"
);
It's not a substitution cipher at all, but your question is clear enough.
Have a look at Base85: http://en.wikipedia.org/wiki/Ascii85
For Java (as indirectly linked by the Wikipedia article):
http://java.freehep.org/freehep-io/apidocs/org/freehep/util/io/ASCII85InputStream.html
http://java.freehep.org/freehep-io/apidocs/org/freehep/util/io/ASCII85OutputStream.html
I now have a working solution which you can find here:
http://pastebin.com/Mctnidng
The problem was that a) I was losing precision in long codes through this part:
value = value.add(//
BigInteger.valueOf((long) Math.pow(alphabet.length, i)) // error here
.multiply(
BigInteger.valueOf(ArrayUtils.indexOf(alphabet, c))));
(long just wasn't long enough)
and b) whenever I had a text that started with the character at offset 0 in the alphabet, this would be dropped, so I needed to add a length character (a single character will do fine here, as my codes will never be as long as the alphabet)