forked from dsw/proquint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProquint.java
More file actions
106 lines (88 loc) · 2.7 KB
/
Copy pathProquint.java
File metadata and controls
106 lines (88 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* This file is part of proquint: http://github.com/dsw/proquint .
* See License.txt for copyright and terms of use.
*/
import java.io.*;
/**
* Convert between proquint, hex, and decimal strings.
* Please see the article on proquints: http://arXiv.org/html/0901.4016
* Daniel S. Wilkerson
*/
public class Proquint {
/** Map uints to consonants. */
static final char uint2consonant[] = {
'b', 'd', 'f', 'g',
'h', 'j', 'k', 'l',
'm', 'n', 'p', 'r',
's', 't', 'v', 'z'
};
/** Map uints to vowels. */
static final char uint2vowel[] = {
'a', 'i', 'o', 'u'
};
/** Convert an unsigned int to a proquint; the output is appended to
* quint; sepChar will be omitted if -1.
*/
static String uint2quint(int i, String sepChar)
{
// http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
// ">>>" Unsigned right shift
StringBuffer quint = new StringBuffer();
quint.append(uint2consonant[i >>> 28]);
i <<= 4;
quint.append(uint2vowel[i >>> 30]);
i <<= 2;
quint.append(uint2consonant[i >>> 28]);
i <<= 4;
quint.append(uint2vowel[i >>> 30]);
i <<= 2;
quint.append(uint2consonant[i >>> 28]);
i <<= 4;
quint.append(sepChar);
quint.append(uint2consonant[i >>> 28]);
i <<= 4;
quint.append(uint2vowel[i >>> 30]);
i <<= 2;
quint.append(uint2consonant[i >>> 28]);
i <<= 4;
quint.append(uint2vowel[i >>> 30]);
i <<= 2;
quint.append(uint2consonant[i >>> 28]);
i <<= 4;
return quint.toString();
}
/**
* Convert a proquint to an unsigned int (long).
*/
static long quint2uint(String quint) {
long res = 0;
for (char c : quint.toCharArray()) {
switch(c) {
/* consonants */
case 'b': res <<= 4; res += 0; break;
case 'd': res <<= 4; res += 1; break;
case 'f': res <<= 4; res += 2; break;
case 'g': res <<= 4; res += 3; break;
case 'h': res <<= 4; res += 4; break;
case 'j': res <<= 4; res += 5; break;
case 'k': res <<= 4; res += 6; break;
case 'l': res <<= 4; res += 7; break;
case 'm': res <<= 4; res += 8; break;
case 'n': res <<= 4; res += 9; break;
case 'p': res <<= 4; res += 10; break;
case 'r': res <<= 4; res += 11; break;
case 's': res <<= 4; res += 12; break;
case 't': res <<= 4; res += 13; break;
case 'v': res <<= 4; res += 14; break;
case 'z': res <<= 4; res += 15; break;
/* vowels */
case 'a': res <<= 2; res += 0; break;
case 'i': res <<= 2; res += 1; break;
case 'o': res <<= 2; res += 2; break;
case 'u': res <<= 2; res += 3; break;
/* separators */
default: break;
}
}
return res;
}
}