Solved

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

Posted on 2007-03-27
2
5,998 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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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