C++ 打印 GUID 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1672677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:52:00  来源:igfitidea点击:

Print a GUID variable

c++guid

提问by Jeni

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

我有一个 GUID 变量,我想在一个文本文件中写入它的值。GUID定义是:

typedef struct _GUID {          // size is 16
    DWORD Data1;
    WORD   Data2;
    WORD   Data3;
    BYTE  Data4[8];
} GUID;

But I want to write its value like:

但我想写它的价值,如:

CA04046D-0000-0000-0000-504944564944

I observed that:

我观察到:

  • Data1holds the decimal value for CA04046D
  • Data2holds the decimal value for 0
  • Data3holds the decimal value for next 0
  • Data1保存 CA04046D 的十进制值
  • Data2保存 0 的十进制值
  • Data3保存下一个 0 的十进制值

But what about the others?

但是其他人呢?

I have to interpret myself this values in order to get that output or is there a more direct method to print such a variable?

我必须自己解释这些值才能获得该输出,还是有更直接的方法来打印这样的变量?

回答by JustinB

Sometimes its useful to roll your own. I liked fdioff's answer but its not quite right. There are 11 elements of different sizes.

有时自己滚动很有用。我喜欢 fdioff 的回答,但不太正确。有11个不同大小的元素。

printf("Guid = {%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}", 
  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]);

Output: "Guid = {44332211-1234-ABCD-EFEF-001122334455}"

Refer to Guiddef.h for the GUID layout.

有关 GUID 布局,请参阅 Guiddef.h。

Same, as a method:

同样,作为一种方法:

void printf_guid(GUID guid) {
    printf("Guid = {%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}", 
      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]);
}

you can also pass a CLSID to this method.

您还可以将 CLSID 传递给此方法。

回答by John Sibly

Use the StringFromCLSIDfunction to convert it to a string

使用StringFromCLSID函数将其转换为字符串

e.g.:

例如:

GUID guid;
CoCreateGuid(&guid);

OLECHAR* guidString;
StringFromCLSID(guid, &guidString);

// use guidString...

// ensure memory is freed
::CoTaskMemFree(guidString);

Also see the MSDN definition of a GUIDfor a description of data4, which is an array containing the last 8 bytes of the GUID

另请参阅MSDN 对 GUID 的定义以了解 data4,它是一个包含 GUID 的最后 8 个字节的数组

回答by Jeni

In case you prefer C++ way

如果您更喜欢 C++ 方式

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

F54F83C5-9724-41BA-BBDB-6926F7BD6813

回答by Rost

In case when your code uses ATL/MFC you also could use CComBSTR::CComBSTR(REFGUID guid)from atlbase.h:

如果您的代码使用 ATL/MFC,您也可以使用CComBSTR::CComBSTR(REFGUID guid)from atlbase.h

GUID guid = ...;
const CComBSTR guidBstr(guid);  // Converts from binary GUID to BSTR
const CString guidStr(guidBstr); // Converts from BSTR to appropriate string, ANSI or Wide

It will make conversion & memory cleanup automatically.

它将自动进行转换和内存清理。

回答by Tim Dowty

You can eliminate the need for special string allocations/deallocations by using StringFromGUID2()

您可以使用 StringFromGUID2() 消除对特殊字符串分配/解除分配的需要

GUID guid = <some-guid>;
// note that OLECHAR is a typedef'd wchar_t
wchar_t szGUID[64] = {0};
StringFromGUID2(&guid, szGUID, 64);

回答by Marco M.

Courtesy of google's breakpadproject:

google 的 breakpad项目提供:

std::string ToString(GUID *guid) {
    char guid_string[37]; // 32 hex chars + 4 hyphens + null terminator
    snprintf(
          guid_string, sizeof(guid_string),
          "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
          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]);
    return guid_string;
}

UUID guid = {0};
UuidCreate(&guid);
std::cout << GUIDToString(&guid);

回答by steve

Inspired by JustinB's answer

灵感来自JustinB的回答

#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));

回答by Cookie

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;
}

回答by user2443842

Use UuidToStringfunction to convert GUID to string. The function accepts UUID type which is typedef of GUID.

使用UuidToString函数将 GUID 转换为字符串。该函数接受 UUID 类型,即 GUID 的 typedef。

回答by tcb

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);
}