3

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.

Community
  • 1
  • 1
Karoy
  • 193
  • 3
  • 9
  • Your remote .NET GUID looks 6 characters short. – Matt Mitchell May 06 '11 at 00:35
  • Could you show a better example of your input and output? The "hex" string you provided is not a hex string at all since it has "digits" that are not hex digits (GHIJKLM...etc). – Chris Dunaway May 06 '11 at 15:32
  • Yes, apologies for the messed up example. Here's what a proper example would look like: `3C0EA2F3-B3A0-8FB0-23F0-9F36DEAA3F7E` would become `F3-A2-0E-3C-A0-B3-B0-8F-23-F0-9F-36-DE-AA-3F-7E`. – Karoy May 06 '11 at 19:24
  • As for the BTW, the last part is probably not interpreted as a number. Only numbers get reversed, you need to know what it is you are parsing to create fault proof code. As for your solution, I don't think it is overly complex (taking in the semantics, it may be too simple actually). – Maarten Bodewes May 06 '11 at 22:52
  • @owlstead so, indeed the conversion of the hex str that required parsing wasn't the issue as such. However, the part that wasn't changed are also hex values, as after reassembling the hex string it gets parsed into a hex byte array just fine. – Karoy May 07 '11 at 01:15

2 Answers2

1

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));
    }
}
karmakaze
  • 34,689
  • 1
  • 30
  • 32
  • On seeing your 'proper example' I see you're not merely splitting the bytes but reordering the endianness of the first three '-' separated parts. In this case you can merely swap elements of the returned byte[] as desired. – karmakaze May 12 '11 at 18:01
0

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.

phoog
  • 42,068
  • 6
  • 79
  • 117