gpt4 book ai didi

Java将十六进制UTF8转换为UTF-16

转载 作者:行者123 更新时间:2023-12-02 12:21:56 24 4
gpt4 key购买 nike

数据捕获产品正在从大型机向我发送一些交易数据(json 格式)。在 JSON 中,它们应该发送一个值,该值是主机 timeOfDay (TOD) 值,指示等效的事务时间戳。而不是发送值“stck”:“00d2fde04305b72642”

他们反而向我发送“stck”:“\u00c3\u0092\u00c3\u00bd\u00c2\u00aa\u00c2\u009e\u001c\u00c2\u0089\u001cG”

当我问他们为什么时,他们说

“上面的(“stck”:“00d2fde04305b72642”)是二进制数据UTF-16格式。JSON不能很好地处理二进制数据,因此我们已将其转换为十六进制UTF-8格式。您可以将其转换回到你身边的 UTF-16”

我需要帮助在 java 中执行此操作。我看到了多个问题,但没有什么完全符合我想要做的,即将十六进制 UTF-8 转换为 UTF-16,希望看起来像“00d2fde04305b72642”

我已经找到了一个问题,该问题显示了如何使用 java 将生成的 TOD 值(“stck”:“00d2fde04305b72642”)转换为事务时间戳,因此我涵盖了该部分。

最佳答案

“他们”做错了。他们应该简单地以 16 为基数对数值进行编码并使用生成的字符串。

他们所做的是将数值的字节视为字符,并使用 UTF-8 对其进行编码。然后,他们获取这些字节并对非 ASCII 字符应用 Unicode 转义序列。雪上加霜的是,当被问及整个过程时,他们的回答是胡言乱语。从很多层面来说,这都是一场灾难。

以下内容应该允许您恢复数据并转换为 Java 时间戳。

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;

public class SO45704851
{

public static void main(String... argv)
throws Exception
{
String example = "\u00c3\u0092\u00c3\u00bd\u00c2\u00aa\u00c2\u009e\u001c\u00c2\u0089\u001cG";
long tod = sbrogliare(example);
System.out.printf("ToD: 0x%016x%n", tod);
Instant time = toInstant(tod);
System.out.printf("Instant: %s%n", time);
}

/**
* Clean up an infernal mess, miraculously bestowing a 64-bit time-of-day.
*/
public static long sbrogliare(String garbage)
{
byte[] utf8 = new byte[garbage.length()];
for (int idx = 0; idx < garbage.length(); ++idx)
utf8[idx] = (byte) garbage.charAt(idx);
String raw = new String(utf8, StandardCharsets.UTF_8);
if (raw.length() != 8)
throw new IllegalArgumentException();
long n = 0;
for (int idx = 0; idx < raw.length(); ++idx) {
char ch = raw.charAt(idx);
if (ch > 255)
throw new IllegalArgumentException();
n = n << 8 | ch;
}
return n;
}

private static final OffsetDateTime zero = OffsetDateTime.parse("1900-01-01T00:00Z");

/**
* Convert 64-bit time-of-day to {@code Instant}.
*/
public static Instant toInstant(long tod)
{
long nanos = (125 * (tod >>> 32) << 23) + (125 * (tod & 0xFFFFFFFFL) >>> 9);
return zero.plus(nanos, ChronoUnit.NANOS).toInstant();
}

}

关于Java将十六进制UTF8转换为UTF-16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45704851/

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