Print a GUID variable

前端 未结 11 1683
孤城傲影
孤城傲影 2021-02-01 02:12

I have a GUID variable and I want to write inside a text file its value. GUID definition is:

typedef struct _GUID {          // size is 16
    DWORD Data1;
             


        
相关标签:
11条回答
  • 2021-02-01 02:33

    In case you prefer C++ way

    std::ostream& operator<<(std::ostream& os, REFGUID guid){
    
        os << std::uppercase;
        os.width(8);
        os << std::hex << guid.Data1 << '-';
    
        os.width(4);
        os << std::hex << guid.Data2 << '-';
    
        os.width(4);
        os << std::hex << guid.Data3 << '-';
    
        os.width(2);
        os << std::hex
            << static_cast<short>(guid.Data4[0])
            << static_cast<short>(guid.Data4[1])
            << '-'
            << static_cast<short>(guid.Data4[2])
            << static_cast<short>(guid.Data4[3])
            << static_cast<short>(guid.Data4[4])
            << static_cast<short>(guid.Data4[5])
            << static_cast<short>(guid.Data4[6])
            << static_cast<short>(guid.Data4[7]);
        os << std::nouppercase;
        return os;
    }
    

    Usage:

    static const GUID guid = 
    { 0xf54f83c5, 0x9724, 0x41ba, { 0xbb, 0xdb, 0x69, 0x26, 0xf7, 0xbd, 0x68, 0x13 } };
    
    std::cout << guid << std::endl;
    

    Output:

    F54F83C5-9724-41BA-BBDB-6926F7BD6813

    0 讨论(0)
  • 2021-02-01 02:34

    I know the question is quite old, but would this work maybe?

    inline std::ostream& operator <<(std::ostream& ss,GUID const& item) {
      OLECHAR* bstrGuid;
      ::StringFromCLSID(item, &bstrGuid);
      ss << bstrGuid;
      ::CoTaskMemFree(bstrGuid);
      return ss;
    }
    
    0 讨论(0)
  • 2021-02-01 02:35
    printf(%X-%X-%X-%X-%X", guid.Data1, guid.Data2, guid.Data3, &guid.Data4);
    
    0 讨论(0)
  • 2021-02-01 02:42

    Inspired by JustinB's answer

    #define GUID_FORMAT "%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX"
    #define GUID_ARG(guid) guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]
    

    and then

    printf("Int = %d, string = %s, GUID = {" GUID_FORMAT "}\n", myInt, myString, GUID_ARG(myGuid));
    
    0 讨论(0)
  • 2021-02-01 02:42
    std::string
    GuidToString(const GUID& guid, bool lower = false)
    {
        const char* hexChars = lower ? "0123456789abcdef" : "0123456789ABCDEF";
    
        auto f = [hexChars](char* p, unsigned char v)
        {
            p[0] = hexChars[v >> 4];
            p[1] = hexChars[v & 0xf];
        };
    
        char s[36];
        f(s, static_cast<unsigned char>(guid.Data1 >> 24));
        f(s + 2, static_cast<unsigned char>(guid.Data1 >> 16));
        f(s + 4, static_cast<unsigned char>(guid.Data1 >> 8));
        f(s + 6, static_cast<unsigned char>(guid.Data1));
        s[8] = '-';
        f(s + 9, static_cast<unsigned char>(guid.Data2 >> 8));
        f(s + 11, static_cast<unsigned char>(guid.Data2));
        s[13] = '-';
        f(s + 14, static_cast<unsigned char>(guid.Data3 >> 8));
        f(s + 16, static_cast<unsigned char>(guid.Data3));
        s[18] = '-';
        f(s + 19, guid.Data4[0]);
        f(s + 21, guid.Data4[1]);
        s[23] = '-';
        f(s + 24, guid.Data4[2]);
        f(s + 26, guid.Data4[3]);
        f(s + 28, guid.Data4[4]);
        f(s + 30, guid.Data4[5]);
        f(s + 32, guid.Data4[6]);
        f(s + 34, guid.Data4[7]);
    
        return std::string(s, 36);
    }
    
    0 讨论(0)
提交回复
热议问题