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 ...
随机推荐
- PLSQL_PLSQL中DML/DDL/DCL的概念和区分(概念)
2014-06-20 Created By BaoXinjian
- HDU 5701 中位数计数
这题真的很恶心,做了一天,最后问学长,才懂题意是什么,原来我一直都没读懂题.这我真的就无语了,你的题意多两句话会死啊?你测试样例多两个例子就不行啊?真蛋疼,题意都不懂,还做个shi? 题里说的是区间, ...
- poj 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 AC代码: #include<iostream> #include<algor ...
- 资源文件assets和 res下面raw文件的使用不同点
在建立项目中一般会默认建立assets文件,当然我们还可以在res文件下面建立raw文件夹,这里面都可以存放一些图片,音频或者文本信息,可以供我们在程序当中进行使用,不过他们两个也有不同点: asse ...
- 播放wav聲音格式
1. #import <AudioToolbox/AudioToolbox.h> 2.聲明 成員变量 SystemSoundID soundID; 3.播放 - (void)playSou ...
- Table View滑动时报错
学习表视图(Table View)的应用时,自己写了个demo,最后表格出来了,可是滑动时报错了,报错如下: 这是我ViewController.m部分的代码: #import "ViewC ...
- [AIR] 利用SnapShot.exe实现QQ截屏功能
主类(可作文档类): package { import flash.display.Bitmap; import flash.display.Sprite; import flash.events.E ...
- 使用jQuery解析xml时command节点解析失败
jQuery版本1.8.3 待解析的xml为: <message><user><command>Login</command></message& ...
- Reflect
反射技术:其实就是动态加载一个指定的类,并获取该类中的所有的内容.而且将字节码文件封装成对象,并将字节码文件中的内容都封装成对象,这样便于操作这些成员.简单说:反射技术可以对一个类进行解剖. 反射的好 ...
- Spring Data Jpa 规范接口表
Keyword Sample JPQL snippet And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname ...