gpt4 book ai didi

java - 将 UUID 转换为十六进制字符串,反之亦然

转载 作者:行者123 更新时间:2023-11-30 05:55:58 26 4
gpt4 key购买 nike

A UUID in the form of "b2f0da40ec2c11e00000242d50cf1fbf"has been transformed (see the following code segment) into a hex string as 6232663064613430656332633131653030303030323432643530636631666266. I want to code a reverse routine and get it back to the original format as in "b2f0.. ”,但很难做到,有什么帮助吗?

    byte[] bytes = uuid.getBytes("UTF-8");

StringBuilder hex = new StringBuilder(bytes.length* 2);
Formatter fmt = new Formatter(hex);

for (byte b : bytes)
fmt.format("%x", b);

最佳答案

final String input = "6232663064613430656332633131653030303030323432643530636631666266";
System.out.println("input: " + input);
final StringBuilder result = new StringBuilder();
for (int i = 0; i < input.length(); i += 2) {
final String code = input.substring(i, i + 2);
final int code2 = Integer.parseInt(code, 16);
result.append((char)code2);

}
System.out.println("result: " + result);

它打印:

input: 6232663064613430656332633131653030303030323432643530636631666266
result: b2f0da40ec2c11e00000242d50cf1fbf

关于java - 将 UUID 转换为十六进制字符串,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7628362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com