[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-03-27
2
Medium Priority
?
6,175 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:czue
2 Comments
 
LVL 25

Accepted Solution

by:
dstanley9 earned 1000 total points
ID: 18800947
A GUID is broken into 4 parts:  an Int32 (4 bytes) two Int16s (2 bytes) and 8 bytes.  So when you pass in a byte array, it treats the first three sections as big-endian byte arrays as follows:

9C1D55DB 53FA 3F45 88 9D 6D C1 14 CA BB 6B

and thus the guid constructor converts each section into the appropriate type (int32, int16, int16, byte[]) and goes from there.  So the bytes in the first three sections get reversed:

db551d9c fa53 453f 88 9d 6d c1 14 ca bb 6b

Notice that the pairs (bytes) are reversed in the first three sections.  

Where as your Hex2String method is just outputting strings one byte at a time

Hope that helps.
0
 

Author Comment

by:czue
ID: 18801061
Makes perfect sense - thanks very much.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question