zoukankan      html  css  js  c++  java
  • c++ string 转GUID及反转

    #include "stdafx.h"
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    #define CONVERT_STR_2_GUID(cstr, stGuid) do
    {
        swscanf_s((const wchar_t*)cstr, L"{%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x}",
        &(stGuid.Data1),&(stGuid.Data2),&(stGuid.Data3),
        &(stGuid.Data4[0]),&(stGuid.Data4[1]),&(stGuid.Data4[2]),&(stGuid.Data4[3]),
        &(stGuid.Data4[4]),&(stGuid.Data4[5]),&(stGuid.Data4[6]),&(stGuid.Data4[7]));
    }while(0);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        const wstring strGuid = L"{9245fe4a-d402-451c-b9ed-9c1a04247482}";
        GUID stGuid = {0};
    
        CONVERT_STR_2_GUID(strGuid.c_str(), stGuid);
    
        wprintf_s(L"%s
    ",strGuid.c_str());
        wprintf_s(L"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", stGuid.Data1,stGuid.Data2,stGuid.Data3,
            stGuid.Data4[0],stGuid.Data4[1],stGuid.Data4[2],stGuid.Data4[3],
            stGuid.Data4[4],stGuid.Data4[5],stGuid.Data4[6],stGuid.Data4[7]);
    
        return 0;
    }

    上面这段程序能看出问题来么? 看出来的兄弟可以在下面指出来!

    反正在vs2012及vs2013环境中验证是有问题的。如GUID {9245fe4a-d402-451c-b9ed-9c1a04247482},报下面现象:

    ---------------------------
    Microsoft Visual C++ Runtime Library
    ---------------------------
    Debug Error!

    Program: D:CPP_ProGUIDTestDebugGUIDTest.exe
    Module: D:CPP_ProGUIDTestDebugGUIDTest.exe
    File:

    Run-Time Check Failure #2 - Stack around the variable 'stGuid' was corrupted.

    (Press Retry to debug the application)

    ---------------------------
    中止(A) 重试(R) 忽略(I)
    ---------------------------

    暂时记在这,其实还有其它方法可以实现,可以避免上面出错, 如下:

    #include "stdafx.h"
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	const wstring strGuid = L"{9245fe4a-d402-451c-b9ed-9c1a04247482}";
    	GUID stGuid = {0};
    
    	CLSIDFromString((LPCOLESTR)strGuid.c_str(), (LPCLSID)&stGuid);
    
    	wprintf_s(L"%s
    ",strGuid.c_str());
    	wprintf_s(L"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", stGuid.Data1,stGuid.Data2,stGuid.Data3,
    		stGuid.Data4[0],stGuid.Data4[1],stGuid.Data4[2],stGuid.Data4[3],
    		stGuid.Data4[4],stGuid.Data4[5],stGuid.Data4[6],stGuid.Data4[7]);
    
    	return 0;
    }
    

    反转就更容易了,可以用sprintf之类的,也可以用StringFromCLSID

    参考:

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms680589(v=vs.85).aspx

  • 相关阅读:
    在Tomcat运行JSP的一个问题
    英语时态的性趣学法
    温哥华蝉联全球最宜居城市榜首 Vancouver still world's most liveable city: survey
    【转】解压缩版tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
    五个常用MySQL图形化管理工具
    windows下将解压缩版的tomcat设置为自动运行的系统服务
    Java初学者Java的学习路径(全集)
    [ZT]森田学习体会
    7种错误冲奶粉法 宝宝的健康会打折
    数据库系统原理
  • 原文地址:https://www.cnblogs.com/kernel0815/p/4254683.html
Copyright © 2011-2022 走看看