#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[]),&(stGuid.Data4[]),&(stGuid.Data4[]),&(stGuid.Data4[]),\
&(stGuid.Data4[]),&(stGuid.Data4[]),&(stGuid.Data4[]),&(stGuid.Data4[]));\
}while(); int _tmain(int argc, _TCHAR* argv[])
{
const wstring strGuid = L"{9245fe4a-d402-451c-b9ed-9c1a04247482}";
GUID stGuid = {}; CONVERT_STR_2_GUID(strGuid.c_str(), stGuid); wprintf_s(L"%s\r\n",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[],stGuid.Data4[],stGuid.Data4[],stGuid.Data4[],
stGuid.Data4[],stGuid.Data4[],stGuid.Data4[],stGuid.Data4[]); return ;
}

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

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

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

Program: D:\CPP_Pro\GUIDTest\Debug\GUIDTest.exe
Module: D:\CPP_Pro\GUIDTest\Debug\GUIDTest.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\r\n",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

c++ string 转GUID及反转的更多相关文章

  1. c# string 转 GUID

    提供两种方法 1.try...catch... /* * string TO guid */ private static bool ToGuid(string str) { Guid gv = ne ...

  2. string转换为guid类型 split

    string str = "{"+context.Request["ID"]+"}"; KpiUser.ID = new Guid(str) ...

  3. (String)将一个String里面的单词反转

    e.g.  i love java    return   java love i public static String reverseStr(String str) { String[] str ...

  4. 344. Reverse String 最基础的反转字符串

    [抄题]: [暴力解法]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: [思维问题]: 还停留在 i < len / 2的阶段,不行,应该是指针对撞问题了 [一 ...

  5. asp.net MVC Model 类的主键 int类型、string类型、GUID类型。

    在使用asp.net mvc进行定义 模型类的时候,一般情况下,我们都会定义一个属性为 int iD{get;set;} 或为int ClassNameID {get;set;},在这种情况下 1.I ...

  6. Guid的使用

    今天在公司做修改功能时,老大让使用部分更新,但是表中的主键是UNIQUEIDENTIFIER类型,它会在我们添加纪录时,默认生成一个unicode码, 但是我现在必须要将获取到的已经是string类型 ...

  7. Java之控制反转和依赖注入

    1.简介 依赖注入和控制反转,目的是为了使类与类之间解耦合,提高系统的可扩展性和可维护性,下面通过一个例子来引入这一概念. 2.案例 1)一般情况下的类耦合 Main.java public clas ...

  8. SQL Server中的GUID

    GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...

  9. 自定义GUID类

    今天,同事发了个邮件说需要一个内部使用的GUID类,而不是System.Guid 结构体.什么意思呢? 例如public void Test(string guidPara) 这个方法的参数是个str ...

随机推荐

  1. BestCoder Round #84 Aaronson

    Aaronson 题意: 给个中文链接:戳戳戳 题解: 这题一看给的公式就是二进制,之后马上就能想到当m大于等于二进制的位数时,输出n的二进制的1的个数就好了.之后就是m小于二进制的位数时,只要加上2 ...

  2. SecureCRT 的安装和注册

    基本信息:win764位 准备: 下载软件包:scrt70-x86.exe,和SecureCRT.7.3.keygen.exe注册机 下载地址:http://download.csdn.net/det ...

  3. 查看iis错误日志时提示找不到 freb.xsl的解决方法

    http://stackoverflow.com/questions/786638/how-can-i-get-gzip-compression-in-iis7-working/787251 Look ...

  4. 【jQuery】关于选择器中的 :first 、 :first-child 、 :first-of-type

    [:first] <!DOCTYPE html><html lang="zh-CN"><head>    <title>test&l ...

  5. [UIView beginAnimations:context:]与[UIView animateWithDuration:animations:]值得注意的一个区别

    原文链接:http://longtimenoc.com/archives/uiview-beginanimationscontext%E4%B8%8Euiview-animatewithduratio ...

  6. dede文章页调用当前栏目链接方法

    DedeCMS内容页调用当前栏目其实用下来是调用不出来的,{dede:field.typename/}是有效的,可是 {dede:field.typeurl/}却调不出文档当前栏目所在目录链接URL. ...

  7. android 列表开发 ListView

    1.android 端 二个entity consultInfo: private String name; private String id; consultInfoRef private iLi ...

  8. ROWID-Oracle中删除重复行数据

    DELETE FROM DEPT_BAK WHERE ROWID NOT IN (SELECT MIN(ROWID) RID FROM DEPT_BAK GROUP BY DEPTNO,DNAME,L ...

  9. 让我苦苦寻找的那段代码---springmvc的ajax前后台交互

    导入jar包: web.xml <servlet> <servlet-name>spmvc</servlet-name> <servlet-class> ...

  10. 立体匹配:关于理解middlebury提供的立体匹配代码后的精减

    Middlebury立体匹配源码总结 优化方法 图像可否预处理 代价计算可否采用BT方式 可选代价计算方法 可否代价聚合 可否MinFilter优化原始代价 WTA-Box 可以 可以 AD/SD 可 ...