[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2007-03-27
2
Medium Priority
?
6,130 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
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 this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

649 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