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[]),&(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及反转的更多相关文章
- c# string 转 GUID
提供两种方法 1.try...catch... /* * string TO guid */ private static bool ToGuid(string str) { Guid gv = ne ...
- string转换为guid类型 split
string str = "{"+context.Request["ID"]+"}"; KpiUser.ID = new Guid(str) ...
- (String)将一个String里面的单词反转
e.g. i love java return java love i public static String reverseStr(String str) { String[] str ...
- 344. Reverse String 最基础的反转字符串
[抄题]: [暴力解法]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: [思维问题]: 还停留在 i < len / 2的阶段,不行,应该是指针对撞问题了 [一 ...
- asp.net MVC Model 类的主键 int类型、string类型、GUID类型。
在使用asp.net mvc进行定义 模型类的时候,一般情况下,我们都会定义一个属性为 int iD{get;set;} 或为int ClassNameID {get;set;},在这种情况下 1.I ...
- Guid的使用
今天在公司做修改功能时,老大让使用部分更新,但是表中的主键是UNIQUEIDENTIFIER类型,它会在我们添加纪录时,默认生成一个unicode码, 但是我现在必须要将获取到的已经是string类型 ...
- Java之控制反转和依赖注入
1.简介 依赖注入和控制反转,目的是为了使类与类之间解耦合,提高系统的可扩展性和可维护性,下面通过一个例子来引入这一概念. 2.案例 1)一般情况下的类耦合 Main.java public clas ...
- SQL Server中的GUID
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...
- 自定义GUID类
今天,同事发了个邮件说需要一个内部使用的GUID类,而不是System.Guid 结构体.什么意思呢? 例如public void Test(string guidPara) 这个方法的参数是个str ...
随机推荐
- 用widthStep的方法来增加某范围的像素----与imageROI对比
//用widthStep的方法来增加某范围的像素 //作者:sandy //时间:2015-10-5 #include <cv.h> #include <highgui.h> ...
- php socket 学习
socket超时设置 ini_set("default_socket_timeout", -1); stream_set_timeout $fp = fsockopen(" ...
- 基于jquery的表单校验插件 - formvalidator使用体验
下载地址:http://www.formvalidator.net/ 基本样例 <form action="/registration" method="POST& ...
- centos nginx环境下删除CI框架Index.php入口遇到404问题
今天在网上百度看了很多文章,想要去掉index.php入口文件有好多方法,自己也照着在网站到根目录下新建了一个.htaccess文件,内容如下: RewriteEngine On RewriteCon ...
- 转--2014年最新810多套android源码2.46GB免费一次性打包下载
转载自:http://www.eoeandroid.com/thread-497046-1-1.html 感谢该博客主人无私奉献~~ 下面的源码是从今年3月份开始不断整理源码区和其他网站上的安卓例子源 ...
- Echarts-地图扩展-标准geoJson格式扩展地图-例子
本人菜鸟一枚,最近搞echarts地图.看到官方给的“标准geoJson格式扩展地图-全国主要城市”的例子,瞬间就蒙逼了.不怪人官网的例子不好,实在是我看不懂它是怎么弄得.最后折腾了一晚上,最后终于弄 ...
- Ubuntu 13.10 安装 ia32-lib
Ubuntu 13.10下面不参直接安装ia32-libs,直接安装的时候会提示下面的信息: output$ sudo apt-get install ia32-libs Reading packag ...
- oracle10g如何配置客户端
http://jingyan.baidu.com/article/4d58d541c108939dd4e9c0f5.html 不行的话: 对tnsnames.ora进行编辑:ORCL = (DESCR ...
- arp -s 157.55.85.212 00-aa-00-62-c6-09 .... Adds a static entry.
ARp是一个重要的TCp/Ip协议,并且用于确定对应Ip地址的网卡物理地址.实用arp命令,我们能够查看本地计算机或另一台计算机的ARp高速缓存中的当前内容.此外,使用arp命令,也可以用人工方式输入 ...
- 找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)"的解决方案
找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)" http://www.microsoft.com/down ...