我有一个GUID变量,我想在文本文件中写入其值。 GUID定义是:
typedef struct _GUID { // size is 16
DWORD Data1;
WORD Data2;
WORD Data3;
BYTE Data4[8];
} GUID;
但我想写下它的价值:
CA04046D-0000-0000-0000-504944564944
我观察到了:
Data1
包含CA04046D的小数值Data2
保存0 Data3
保存下一个0的小数值但其他人呢?
我必须解释自己这个值才能获得输出,或者是否有更直接的方法来打印这样的变量?
答案 0 :(得分:45)
使用StringFromCLSID功能将其转换为字符串
e.g:
GUID guid;
CoCreateGuid(&guid);
OLECHAR* guidString;
StringFromCLSID(guid, &guidString);
// use guidString...
// ensure memory is freed
::CoTaskMemFree(guidString);
另请参阅MSDN definition of a GUID以获取data4的描述,这是一个包含GUID最后8个字节的数组
答案 1 :(得分:45)
有时它对你自己很有用。我喜欢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}"
请参阅Guiddef.h了解GUID布局。
同样,作为一种方法:
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]);
}
您也可以将CLSID传递给此方法。
答案 2 :(得分:11)
如果您的代码使用ATL / MFC,您也可以使用atlbase.h
中的CComBSTR::CComBSTR(REFGUID guid)
:
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
它将进行转换&记忆清理自动。
答案 3 :(得分:9)
如果您更喜欢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;
}
用法:
static const GUID guid =
{ 0xf54f83c5, 0x9724, 0x41ba, { 0xbb, 0xdb, 0x69, 0x26, 0xf7, 0xbd, 0x68, 0x13 } };
std::cout << guid << std::endl;
输出:
F54F83C5-9724-41BA-BBDB-6926F7BD6813
答案 4 :(得分:6)
灵感来自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]
然后
printf("Int = %d, string = %s, GUID = {" GUID_FORMAT "}\n", myInt, myString, GUID_ARG(myGuid));
答案 5 :(得分:5)
使用StringFromGUID2()
可以消除对特殊字符串分配/解除分配的需要GUID guid = <some-guid>;
// note that OLECHAR is a typedef'd wchar_t
wchar_t szGUID[64] = {0};
StringFromGUID2(&guid, szGUID, 64);
答案 6 :(得分:5)
由google's 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);
答案 7 :(得分:2)
我知道这个问题已经过时了,但这可能会有用吗?
inline std::ostream& operator <<(std::ostream& ss,GUID const& item) {
OLECHAR* bstrGuid;
::StringFromCLSID(item, &bstrGuid);
ss << bstrGuid;
::CoTaskMemFree(bstrGuid);
return ss;
}
答案 8 :(得分:1)
使用UuidToString函数将GUID转换为字符串。该函数接受UUID类型,即GUID的typedef。
答案 9 :(得分:0)
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);
}
答案 10 :(得分:-2)
printf(%X-%X-%X-%X-%X", guid.Data1, guid.Data2, guid.Data3, &guid.Data4);