none
How do I embed numbers in a defined string of numbers RRS feed

  • Question

  • Ok. I want to take a new Guid and embed a few numbers in it. I want to leave the dashes in it and put an "A"as a divider. So the number are like so
    guid = {00000000-0000-0000-0000-000000000000}

    Numbers I have
    ID = 2345678
    req# 234565

    What I need
    2345678A-2345-65A0-0000-000000000000

    OR

    Numbers I haveID = 84768899
    req# 8654

    What I need
    84768899-A865-4A00-0000-000000000000


    www.helixpoint.com

    Thursday, January 23, 2020 5:08 PM

Answers

  • Try this too:

    int ID = 2345678;
    int req = 234565;
    
    string s = ( ID + "A" + req + "A" ).PadRight( 32, '0' );
    string result = s.Insert( 20, "-" ).Insert( 16, "-" ).Insert( 12, "-" ).Insert( 8, "-" );
    
    Console.WriteLine( result );
    

    • Marked as answer by Helixpoint Friday, January 24, 2020 1:22 PM
    Friday, January 24, 2020 7:26 AM

All replies

  • The basic idea is 

    var item = "{2345678-0000-0000-0000-000000000000}";
    var parts = item.Split('-');
    item = $"{parts[0]}A-{parts[1]}-{parts[2]}-{parts[3]}-{parts[4]}";
    Console.WriteLine(item);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, January 23, 2020 6:28 PM
    Moderator
  • Not sure I am following this. 

    This is what I had started. I know it is not correct 100%, but I don't see how yours works?

    public string GetOutlookEmployeeStatusLink(string orgID, string ID)
    		{
    			var id = new Guid();
                string guid = "";
                int i = 0;
                    for (int p = 0; p < orgID.Length; p++)
                    {
                        i++;
                        if (i == 8 || i == 13)
                        {
                            guid = guid + "-";
                        }
                        else
                        {
                            guid = guid + orgID[p];
                        }
                    }
    
                    if (i == 8 || i == 13)
                    {
                        guid = guid + "-";
                    }
                    else
                    {
                        guid = guid + "A";
                    }
                    for (int q = 0; q < orgID.Length; q++)
                    {
                        i++;
                        if (i == 8 || i == 13)
                        {
                            guid = guid + "-";
                        }
                        else
                        {
                            guid = guid + ID[q];
                        }
                    }
                guid = guid + "A";
                for (int r = 0; r < i; r++)
                    {
                        i++;
                        if (i == 8 || i == 13)
                        {
                            guid = guid + "-";
                        }
                        else
                        {
                            guid = guid + "0";
                        }
                    }


    www.helixpoint.com

    Thursday, January 23, 2020 6:36 PM
  • I went by exactly what you asked for, there are two cases and I did the first one, not the second one as if the first is right then you would follow suit with the second. The starting point is a blank GUID ? or a defined GUID ?

    If you are starting with a blank GUID then

    var id = 2345678;
    var item = "{00000000-0000-0000-0000-000000000000}";
    var parts = item.Split('-');
    item = $"{id}A-{parts[1]}-{parts[2]}-{parts[3]}-{parts[4]}";
    Console.WriteLine(item);

    Or

    var req = 8654;
    var item = "{84768899-0000-0000-0000-000000000000}";
    var parts = item.Split('-');
    item = $"{parts[0]}-A{req}-{parts[2]}-{parts[3]}-{parts[4]}";
    Console.WriteLine(item);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, January 23, 2020 6:56 PM
    Moderator
  • I have to replace both the id and the req#  making sure the dashes stay put and there is an  "A" between the numbers. Excuse me if I am being stupid

    www.helixpoint.com

    Thursday, January 23, 2020 7:09 PM
  • Just to be clear, it isn't a GUID anymore if you're putting hard coded values into it because you lost the uniqueness factor. But if you really want to replace values in an existing GUID with some new values then treat it just like it is which is a byte array. The formatting of said value is a UI thing.

    If you could guarantee that the replacement strings would always be even bytes then you could get away with working with the byte array directly which is probably more efficient. However barring that I think simple string replacement would work better. Note that this code is not heavily tested.

    static Guid CreateGuid ( string id, string reqId )
    {
        var baseGuid = Guid.NewGuid().ToString();
    
        var baseValue = baseGuid.Replace("-", "");
    
        //Get the hex string we want to deal with
        //Making an assumption here that the provided strings are valid hex numbers, not too long
        var subId = $"{id}A{reqId}A";
    
        //Stomp over the existing string with the new string - not efficient, string span would be better...
        var newValue = subId + baseValue.Substring(subId.Length);
    
        return new Guid(newValue);
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, January 23, 2020 7:32 PM
    Moderator
  • Try this too:

    int ID = 2345678;
    int req = 234565;
    
    string s = ( ID + "A" + req + "A" ).PadRight( 32, '0' );
    string result = s.Insert( 20, "-" ).Insert( 16, "-" ).Insert( 12, "-" ).Insert( 8, "-" );
    
    Console.WriteLine( result );
    

    • Marked as answer by Helixpoint Friday, January 24, 2020 1:22 PM
    Friday, January 24, 2020 7:26 AM
  • I want to emphasize what Michael said, because this is IMPORTANT.  What you're doing is not allowed.  You can't just shove arbitrary numbers into a GUID format and call it a GUID.  There is a carefully constructed standard for how GUIDs are made (RFC 4122), and everyone has to adhere to that standard, otherwise the identifiers cannot be guaranteed to be "globally unique", as the name says.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, January 24, 2020 7:34 AM