none
small GUID RRS feed

  • Question

  • Hello everyone!

     

    Is exist any way for present GUID in something small value (e.g. number or string, which contains only 7-8 characters) ?

     

    Thanks,

    Volodya.

    Wednesday, March 30, 2011 4:53 PM

Answers

  • This is way too complicated and it doesn't exacly do what I had in mind. The GUID bytes need to be converted to base64:

    string s = Convert.ToBase64String(Guid.NewGuid().ToByteArray());

    "but I need user friendly representation."

    Define "user friendly" in the case of GUIDs. They're random and large numbers, there's nothing friendly about them no matter what way you display them.

    • Marked as answer by Volodya Bychko Wednesday, March 30, 2011 6:42 PM
    • Unmarked as answer by Volodya Bychko Wednesday, March 30, 2011 6:42 PM
    • Marked as answer by Volodya Bychko Wednesday, March 30, 2011 6:42 PM
    Wednesday, March 30, 2011 6:23 PM
    Moderator

All replies

  • A GUID has 16 bytes, even if you consider all characters from 0 to 255 to be displayable you can get to 7-8 characters, you get 16. The shortest string representation I know is base64 which uses 22 characters (without the trailing ==). It's less than the 36 characters used in the normal representation of GUIDs.
    Wednesday, March 30, 2011 5:12 PM
    Moderator
  • Hello Mike,

    Do You mean that I should use something like this:

     class Program
      {
        static void Main(string[] args)
        {
          Guid g = Guid.NewGuid();
          Console.WriteLine("Guid -> {0}", g);
          var gb = base64Encode(g.ToString());
          var d = base64Decode(gb);
          Console.WriteLine(gb.Length);
          Console.WriteLine("Guid -> {0}", new Guid(d));
          Console.WriteLine(g == new Guid(d));
          Console.ReadKey();
        }
    
        public static string base64Encode(string data)
        {
          try
          {
            byte[] encData_byte = new byte[data.Length];
            encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
            string encodedData = Convert.ToBase64String(encData_byte);
            return encodedData;
          }
          catch (Exception e)
          {
            throw new Exception("Error in base64Encode" + e.Message);
          }
        }
    
        public static string base64Decode(string data)
        {
          try
          {
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            System.Text.Decoder utf8Decode = encoder.GetDecoder();
    
            byte[] todecode_byte = Convert.FromBase64String(data);
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);
            return result;
          }
          catch (Exception e)
          {
            throw new Exception("Error in base64Decode" + e.Message);
          }
        }
      }

    but I need user friendly representation.

    As I understand, no exist correct solution for my problem.

    Thanks,
    Volodya.
    Wednesday, March 30, 2011 6:09 PM
  • This is way too complicated and it doesn't exacly do what I had in mind. The GUID bytes need to be converted to base64:

    string s = Convert.ToBase64String(Guid.NewGuid().ToByteArray());

    "but I need user friendly representation."

    Define "user friendly" in the case of GUIDs. They're random and large numbers, there's nothing friendly about them no matter what way you display them.

    • Marked as answer by Volodya Bychko Wednesday, March 30, 2011 6:42 PM
    • Unmarked as answer by Volodya Bychko Wednesday, March 30, 2011 6:42 PM
    • Marked as answer by Volodya Bychko Wednesday, March 30, 2011 6:42 PM
    Wednesday, March 30, 2011 6:23 PM
    Moderator
  • Thanks for answers, but I was looking for some algoritm for present GUID to user.

    The best way to present GUID to user is present GUID.


    Wednesday, March 30, 2011 6:42 PM