Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Guid[] to String?

Tags:

c#

.net-3.5

Something like String.Join(",", new string[] { "a", "b" });, but for Guid[]

var guids = new Guid[] { Guid.Empty, Guid.Empty };

var str = /* Magic */

// str = 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
like image 806
BrunoLM Avatar asked Jul 26 '10 15:07

BrunoLM


People also ask

Can I convert GUID to string?

According to MSDN the method Guid. ToString(string format) returns a string representation of the value of this Guid instance, according to the provided format specifier.

Is a GUID a string?

A GUID is a 16-byte (128-bit) number, typically represented by a 32-character hexadecimal string.

How do you convert GUID to int?

An integer uses 32 bits, whereas a GUID is 128 bits - therefore, there's no way to convert a GUID into an integer without losing information, so you can't convert it back. EDIT: you could perhaps store the GUID into a GUIDs table where you assign each a unique integer ID.

What is the format of a GUID?

All GUIDs must be authored in uppercase. The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F). Note that utilities such as GUIDGEN can generate GUIDs containing lowercase letters.


1 Answers

var str = guids.Select(g => g.ToString())
               .Aggregate((working, next) => working + "," + next);

Once your list of Guids starts growing, this method of concatenation is going to cause performance issues. You can modify it to use a StringBuilder:

var str = guids.Select(g => g.ToString())
               .Aggregate(new StringBuilder(),
                          (sb, str) => sb.Append("," + str),
                          sb => sb.ToString());

Both of those are the complicated LINQ Extension method way of doing things. You could also simply use String.Join:

var str = String.Join(",", guids.Select(g => g.ToString()).ToArray());
like image 58
Justin Niessner Avatar answered Sep 28 '22 10:09

Justin Niessner