繁体   English   中英

Base32 字符转换为 5bit 值,Python 到 Java

[英]Base32 character conversion to a 5bit value, Python to Java

我有一些 Python 代码,其中每个给定的 Base32 字符都转换为 5 位值。

我需要在 Java 中重写它,但我不知道这种格式 function 的 Java 等效/语法。

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位,因为我们在字母表中有32个可能的字符,因此最高索引是31,需要5位来表示=> 11111

这应该这样做:

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"));
            
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM