Link to home
Start Free TrialLog in
Avatar of czue
czue

asked on

C# byte[] to hex string conversion inconsistency with Guid

I am trying to figure out why I am having inconsistencies converting byte[]'s to Guids / hex strings.

I have the following code:
            byte[] bytes = [A 32-bit byte array]
            string guid1 = ByteUtility.HexStringFromByteArray(bytes);
          string guid2 = new Guid(bytes).ToString("N");

what I will see is (i don't care about the casing - just the out of order of the first 16 chars):

guid1 = 9C1D55DB53FA3F45889D6DC114CABB6B
guid2 = db551d9cfa53453f889d6dc114cabb6b


Here is the method in question:

            public static string HexStringFromByteArray(byte[] bytes) {
                  string s = "";

                  foreach (byte b in bytes) {
                        s += string.Format("{0:X2}", b);
                  }

                  return s;
            }

thanks
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of czue
czue

ASKER

Makes perfect sense - thanks very much.