I have some Python code where each given Base32 character is converted to a 5bit value.
I need to rewrite it in Java, but I don't know the Java equivalent/syntax for this format function.
word_safe_alphabet = "23456789CFGHJMPQRVWXcfghjmpqrvwx"
d = {}
i=0
for char in word_safe_alphabet:
# Create 5 bit patterns for each character in the alphabet
# Start with bit pattern for index 0, to index 31: 00000 -> 11111
d[char] = "{0:05b}".format(i)
i =1
5 bit, because we have 32 possible characters in the alphabet, thus the highest index is 31, which needs 5 bits to be represented => 11111
CodePudding user response:
This should do:
class Binary {
public static void main(String[] args) {
String word_safe_alphabet = "23456789CFGHJMPQRVWXcfghjmpqrvwx";
for(int i=0; i<word_safe_alphabet.length(); i ){
int charNr = Character.getNumericValue(word_safe_alphabet.charAt(i))-2;
String binary = Integer.toBinaryString(charNr);
System.out.println(String.format("%5s", binary).replaceAll(" ", "0"));
}
}
}