Generate GUID using 2 digit State Code
-
I am getting problem in generating new GUID using its constructor who takes one string argument. like this Guid regionID = new Guid(this.State.SelectedValue); state gives 2 digit state code.The error says you need 32 digit with 4 dashes string to generate GUID. any solutions ? Amit Champaneri
-
I am getting problem in generating new GUID using its constructor who takes one string argument. like this Guid regionID = new Guid(this.State.SelectedValue); state gives 2 digit state code.The error says you need 32 digit with 4 dashes string to generate GUID. any solutions ? Amit Champaneri
That's right, because GUID is basically 16 bytes separated in certain places by 4 dashes... You can not have GUID with only two bytes. If you want, you can do it like this:
byte[] guidBytes = new byte[16]; Random rand = new Random(); rand.NextBytes(quidBytes); guidBites[0] = Convert.ToByte(this.State.SelectedValue[0]); guidBites[1] = Convert.ToByte(this.State.SelectedValue[1]); Guid myGuid = new Guid(guidBytes);
this should works Pilo -
I am getting problem in generating new GUID using its constructor who takes one string argument. like this Guid regionID = new Guid(this.State.SelectedValue); state gives 2 digit state code.The error says you need 32 digit with 4 dashes string to generate GUID. any solutions ? Amit Champaneri
I think you are confused about what a GUID actually is. A Guid is a value that is generated by the system to be Globally (as in the world) unique. You should be able to generate as many guid values as you like on your machine and so could everyone else, but every value would always be unique.
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
That's right, because GUID is basically 16 bytes separated in certain places by 4 dashes... You can not have GUID with only two bytes. If you want, you can do it like this:
byte[] guidBytes = new byte[16]; Random rand = new Random(); rand.NextBytes(quidBytes); guidBites[0] = Convert.ToByte(this.State.SelectedValue[0]); guidBites[1] = Convert.ToByte(this.State.SelectedValue[1]); Guid myGuid = new Guid(guidBytes);
this should works PiloThis solution is really unlikely to help the OP because you have not taken into account what he might actually want a GUID for. You need to try to look at the bigger picture when answering questions because most are asking questions because they've got themselves in to a bit of confusion about something. Subsequently they may not ask the best question to get the answer they need.
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos