?
Solved

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

Posted on 2007-03-27
2
Medium Priority
?
6,072 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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