Solved

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

Posted on 2007-03-27
2
5,839 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 250 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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now