Dotnet 十六进制字符串转 Java

发布于 2024-11-05 16:57:28 字数 1061 浏览 1 评论 0原文

有一个问题,就像这篇文章一样:如何阅读将 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-11-12 16:57:28

将其变成 byte[] 并跳过前 3 个字节:

package guid;
import java.util.Arrays;

public class GuidConvert {

    static byte[] convertUuidToBytes(String guid) {
        String hexdigits = guid.replaceAll("-", "");
        byte[] bytes = new byte[hexdigits.length()/2];
        for (int i = 0; i < bytes.length; i++) {
            int x = Integer.parseInt(hexdigits.substring(i*2, (i+1)*2), 16);
            bytes[i] = (byte) x;
        }
        return bytes;
    }

    static String bytesToHexString(byte[] bytes) {
        StringBuilder buf = new StringBuilder();
        for (byte b : bytes) {
            int i = b >= 0 ? b : (int) b + 256;
            buf.append(Integer.toHexString(i / 16));
            buf.append(Integer.toHexString(i % 16));
        }
        return buf.toString();
    }

    public static void main(String[] args) {
        String guid = "3C0EA2F3-B3A0-8FB0-23F0-9F36DEAA3F7E";
        byte[] bytes = convertUuidToBytes(guid);
        System.err.println("GUID  = "+ guid);
        System.err.println("bytes = "+ bytesToHexString(bytes));
        byte[] tail = Arrays.copyOfRange(bytes, 3, bytes.length);
        System.err.println("tail  =       "+ bytesToHexString(tail));
    }
}

Make it into a byte[] and skip the first 3 bytes:

package guid;
import java.util.Arrays;

public class GuidConvert {

    static byte[] convertUuidToBytes(String guid) {
        String hexdigits = guid.replaceAll("-", "");
        byte[] bytes = new byte[hexdigits.length()/2];
        for (int i = 0; i < bytes.length; i++) {
            int x = Integer.parseInt(hexdigits.substring(i*2, (i+1)*2), 16);
            bytes[i] = (byte) x;
        }
        return bytes;
    }

    static String bytesToHexString(byte[] bytes) {
        StringBuilder buf = new StringBuilder();
        for (byte b : bytes) {
            int i = b >= 0 ? b : (int) b + 256;
            buf.append(Integer.toHexString(i / 16));
            buf.append(Integer.toHexString(i % 16));
        }
        return buf.toString();
    }

    public static void main(String[] args) {
        String guid = "3C0EA2F3-B3A0-8FB0-23F0-9F36DEAA3F7E";
        byte[] bytes = convertUuidToBytes(guid);
        System.err.println("GUID  = "+ guid);
        System.err.println("bytes = "+ bytesToHexString(bytes));
        byte[] tail = Arrays.copyOfRange(bytes, 3, bytes.length);
        System.err.println("tail  =       "+ bytesToHexString(tail));
    }
}
带上头具痛哭 2024-11-12 16:57:28

最后一组 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文