Bee
导航
C++博客
首页
新随笔
联系
聚合
管理
<
2009年10月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
统计
随笔 - 1
文章 - 0
评论 - 0
引用 - 0
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
给我留言
查看公开留言
查看私人留言
随笔档案
2009年10月 (1)
搜索
最新评论
一段转换guid字符串的lua代码
--
guid2.lua
--
s
=
"
{0xCC3F04F7, 0x3198, 0x407E, {0xA0, 0x05, 0x73, 0xBA, 0xB6, 0xD3, 0xC2, 0x67} }
"
s
=
"
CC3F04F7-3198-407E-A005-73BAB6D3C267
"
--
通过flag表来控制将纯32位数字纯转换为什么格式的guid串
function convert_with_flags(s_guid, tblflags)
if
string
.len(s_guid)
~=
32
or type(tblflags)
~=
"
table
"
then
error
"
parameter is invalid
"
return
end
local s_ret
=
""
local n_elements
=
table.getn(tblflags)
for
i
=
1
, n_elements,
1
do
if
type(tblflags[i])
==
"
string
"
then
s_ret
=
s_ret..tblflags[i]
elseif type(tblflags[i])
==
"
table
"
then
s_ret
=
s_ret..
string
.sub(s_guid, tblflags[i][
1
], tblflags[i][
2
])
end
end
return
s_ret
end
--
将guid字符串转换成32位数字串
function make_pure_guid(s)
local s_pure
=
string
.gsub(
string
.upper(s),
"
0X
"
,
""
)
s_pure
=
string
.gsub(s_pure,
"
%X
"
,
""
)
return
s_pure
end
function convert2cstyle(s)
local s_pure
=
make_pure_guid(s)
if
string
.len(s_pure)
~=
32
then
error
"
invalid guid string
"
return
end
local tbl_flags
=
{
"
{0x
"
,
{
1
,
8
}
,
"
, 0x
"
,
{
9
,
12
}
,
"
, 0x
"
,
{
13
,
16
}
,
"
, {0x
"
,
{
17
,
18
}
,
"
, 0x
"
,
{
19
,
20
}
,
"
, 0x
"
,
{
21
,
22
}
,
"
, 0x
"
,
{
23
,
24
}
,
"
, 0x
"
,
{
25
,
26
}
,
"
, 0x
"
,
{
27
,
28
}
,
"
, 0x
"
,
{
29
,
30
}
,
"
, 0x
"
,
{
31
,
32
}
,
"
} }
"
}
return
convert_with_flags(s_pure, tbl_flags)
end
function convert2normal(s)
local s_pure
=
make_pure_guid(s)
if
string
.len(s_pure)
~=
32
then
error
"
invalid guid string
"
return
end
local tbl_flags
=
{
{
1
,
8
}
,
"
-
"
,
{
9
,
12
}
,
"
-
"
,
{
13
,
16
}
,
"
-
"
,
{
17
,
20
}
,
"
-
"
,
{
21
,
32
}
}
return
convert_with_flags(s_pure, tbl_flags)
end
print(
"
s =
"
..s)
s_guid_normal
=
convert2normal(s)
print(
"
s_guid_normal =
"
..s_guid_normal)
s_guid_cstyle
=
convert2cstyle(s_guid_normal)
print(
"
s_guid_cstyle =
"
..s_guid_cstyle)
posted on 2009-10-25 12:56
Bee
阅读(876)
评论(0)
编辑
收藏
引用
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
博客园最新博文
博问
管理
Powered by:
C++博客
Copyright © Bee