Dotnet 十六进制字符串转 Java
有一个问题,就像这篇文章一样:如何阅读将 .NET Guid 转换为 Java UUID。
除此之外,我从远程 svc 获得一个十六进制 str 格式如下:ABCDEFGH-IJKL-MNOP-QRST-123456
。
我需要匹配 GUID.ToByteArray() 生成的 .net 字节数组 GH-EF-CD-AB-KL-IJ-OP-MN- QR- ST-12-34-56
在 Java 中用于散列目的。
我有点不知道如何解析这个。我是否要切断 QRST-123456 部分,并可能在另一部分上使用 Commons IO EndianUtils 之类的东西,然后将 2 个数组重新缝合在一起?看起来太复杂了。 我可以重新排列字符串,但我不必执行任何这些操作。 Google 先生也不想帮助我。
顺便说一句,Little Endian 领域中保持最后 6 个字符不变的逻辑是什么?
是的,作为参考,这就是我所做的{对“答案”感到抱歉,但在评论中无法正确格式化它}:
String s = "3C0EA2F3-B3A0-8FB0-23F0-9F36DEAA3F7E";
String[] splitz = s.split("-");
String rebuilt = "";
for (int i = 0; i < 3; i++) {
// Split into 2 char chunks. '..' = nbr of chars in chunks
String[] parts = splitz[i].split("(?<=\\G..)");
for (int k = parts.length -1; k >=0; k--) {
rebuilt += parts[k];
}
}
rebuilt += splitz[3]+splitz[4];
我知道,这很糟糕,但它可以用于测试。
Have a problem, much like this post: How to read a .NET Guid into a Java UUID.
Except, from a remote svc I get a hex str formatted like this: ABCDEFGH-IJKL-MNOP-QRST-123456
.
I need to match the GUID.ToByteArray() generated .net byte array GH-EF-CD-AB-KL-IJ-OP-MN- QR- ST-12-34-56
in Java for hashing purposes.
I'm kinda at a loss as to how to parse this. Do I cut off the QRST-123456
part and perhaps use something like the Commons IO EndianUtils on the other part, then stitch the 2 arrays back together as well? Seems way too complicated.
I can rearrange the string, but I shouldn't have to do any of these. Mr. Google doesn't wanna help me neither..
BTW, what is the logic in Little Endian land that keeps those last 6 char unchanged?
Yes, for reference, here's what I've done {sorry for 'answer', but had trouble formatting it properly in comment}:
String s = "3C0EA2F3-B3A0-8FB0-23F0-9F36DEAA3F7E";
String[] splitz = s.split("-");
String rebuilt = "";
for (int i = 0; i < 3; i++) {
// Split into 2 char chunks. '..' = nbr of chars in chunks
String[] parts = splitz[i].split("(?<=\\G..)");
for (int k = parts.length -1; k >=0; k--) {
rebuilt += parts[k];
}
}
rebuilt += splitz[3]+splitz[4];
I know, it's hacky, but it'll do for testing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其变成 byte[] 并跳过前 3 个字节:
Make it into a byte[] and skip the first 3 bytes:
最后一组 6 个字节没有反转,因为它是字节数组。前四组是相反的,因为它们是一个四字节整数,后跟三个两字节整数。
The last group of 6 bytes is not reversed because it is an array of bytes. The first four groups are reversed because they are a four-byte integer followed by three two-byte integers.