Java Siphash tostring和getBytes()

Java Siphash tostring和getBytes(),java,string,byte,guava,Java,String,Byte,Guava,我已经为1个字符串和2个长值生成了SipHash(对于许多这样的字符串和长值组合)。我用过- Hasher hash = Hashing.sipHash24().newHasher().putUnencodedChars("abcd").putLong(123).putLong(123); 现在我使用- String hashString = hash.hash().toString(); 但是,我想要字符串的字节数组,有什么方法可以让我从这个字符串中得到字节数组,就像从byte[]hash

我已经为1个字符串和2个长值生成了SipHash(对于许多这样的字符串和长值组合)。我用过-

Hasher hash = Hashing.sipHash24().newHasher().putUnencodedChars("abcd").putLong(123).putLong(123);
现在我使用-

String hashString = hash.hash().toString();
但是,我想要字符串的字节数组,有什么方法可以让我从这个字符串中得到字节数组,就像从
byte[]hashBytes=hash.hash().asBytes()中得到的一样我想把从这些散列中得到的字符串转换成字节数组


实际上,我意识到字节数组只使用了8字节的空间用于siphash,其中字符串的长度为18字节。因此,我想将散列存储为字节数组会更加优化。

以下是从字符串中获取字节数组的代码-

public static byte[] getBytes(String hashString) {

    final byte[] bytes = new byte[8];

    HashMap<Character, String> bin = new HashMap<>();
    bin.put('0', "0000");
    bin.put('1', "0001");
    bin.put('2', "0010");
    bin.put('3', "0011");
    bin.put('4', "0100");
    bin.put('5', "0101");
    bin.put('6', "0110");
    bin.put('7', "0111");
    bin.put('8', "1000");
    bin.put('9', "1001");
    bin.put('a', "1010");
    bin.put('b', "1011");
    bin.put('c', "1100");
    bin.put('d', "1101");
    bin.put('e', "1110");
    bin.put('f', "1111");

    for (int i = 0; i < 16 && i < hashString.length(); i += 2) {

        final BitSet bitset = new BitSet(8);
        String byteBinary = bin.get(hashString.charAt(i)) + bin.get(hashString.charAt(i + 1));
        for (int j = 0; j<8; j++) {

            if (byteBinary.charAt(j) == '1')
                bitset.set(7-j, true);
            else
                bitset.set(7-j, false);
        }


        bytes[i/2] = bitset.toByteArray()[0];
        //System.out.println(byteBinary);
    }
    return bytes;
}
publicstaticbyte[]getBytes(stringhashstring){
最终字节[]字节=新字节[8];
HashMap bin=新的HashMap();
银行本票面值('0','0000”);
银行同业拆放('1','0001');
银行同业拆放('2','0010');
银行同业拆放('3','0011');
银行同业拆放('4','0100');
银行本票('5','0101');
银行本票('6','0110');
银行本票('7','0111');
银行本票('8','1000');
银行本票('9','1001');
银行本票('a','1010');
银行本票('b','1011');
银行本票('c','1100');
银行本票('d','1101');
银行本票('e','1110');
银行本票('f','1111');
对于(int i=0;i<16&&i对于(int j=0;j,以下是从字符串获取字节数组的代码-

public static byte[] getBytes(String hashString) {

    final byte[] bytes = new byte[8];

    HashMap<Character, String> bin = new HashMap<>();
    bin.put('0', "0000");
    bin.put('1', "0001");
    bin.put('2', "0010");
    bin.put('3', "0011");
    bin.put('4', "0100");
    bin.put('5', "0101");
    bin.put('6', "0110");
    bin.put('7', "0111");
    bin.put('8', "1000");
    bin.put('9', "1001");
    bin.put('a', "1010");
    bin.put('b', "1011");
    bin.put('c', "1100");
    bin.put('d', "1101");
    bin.put('e', "1110");
    bin.put('f', "1111");

    for (int i = 0; i < 16 && i < hashString.length(); i += 2) {

        final BitSet bitset = new BitSet(8);
        String byteBinary = bin.get(hashString.charAt(i)) + bin.get(hashString.charAt(i + 1));
        for (int j = 0; j<8; j++) {

            if (byteBinary.charAt(j) == '1')
                bitset.set(7-j, true);
            else
                bitset.set(7-j, false);
        }


        bytes[i/2] = bitset.toByteArray()[0];
        //System.out.println(byteBinary);
    }
    return bytes;
}
publicstaticbyte[]getBytes(stringhashstring){
最终字节[]字节=新字节[8];
HashMap bin=新的HashMap();
银行本票面值('0','0000”);
银行同业拆放('1','0001');
银行同业拆放('2','0010');
银行同业拆放('3','0011');
银行同业拆放('4','0100');
银行本票('5','0101');
银行本票('6','0110');
银行本票('7','0111');
银行本票('8','1000');
银行本票('9','1001');
银行本票('a','1010');
银行本票('b','1011');
银行本票('c','1100');
银行本票('d','1101');
银行本票('e','1110');
银行本票('f','1111');
对于(int i=0;i<16&&iHashCode.toString()
转换回从
asBytes()
获得的字节数组


应将
HashCode.toString()
转换回从
asBytes()

获得的字节数组。您可以使用
HashCode.fromString(string)
将字符串解析回
HashCode
实例。然后可以调用
.asBytes()
HashCode
实例上获取基础
字节[]
的副本

所以基本上你想要:


byte[]bytes=HashCode.fromString.asBytes();

您可以使用
HashCode.fromString(string)
将字符串解析回一个
HashCode
实例。然后您可以调用
HashCode
实例上的
.asBytes()
,以获取底层
字节[/code>的副本

所以基本上你想要:

byte[]bytes=HashCode.fromString(string).asBytes();