Method: ConvertGuidToBase64




    The code in question creates a span of bytes to hold the GUID. It then copies the bytes of the GUID to the span. Finally, it converts the span to a base64 string.

    // this code created by ChatGPT.
    // Method that accepts a guid as a string and return a base64 version of the guid, it handle invalid input and it's optimized to not have heap allocations by create a span of bytes to hold the guid.
    public string ConvertGuidToBase64(string input)
      // Validate the input string
      if (!Guid.TryParse(input, out Guid guid))
        throw new ArgumentException("Input string is not a valid GUID.");
      // Create a span to hold the bytes of the GUID
      Span<byte> bytes = stackalloc byte[16];
      // Copy the bytes of the GUID to the span
      // Convert the span to a base64 string
      return Convert.ToBase64String(bytes);
    // used like this.
        string input = "12345678-9ABC-DEF0-1234-56789ABCDEF0";
        string base64 = ConvertGuidToBase64(input);
    catch (ArgumentException ex)
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.