- 论坛徽章:
- 0
|
问题描述:
存储过程中有 2 个变量:- declare code_gb2312 int unsigned default 0;
- declare code_utf8 int unsigned default 0;
复制代码 我先已知 code_gb2312 的值,比如以 '中' 字(gb2312 码为 0xd6d0)为例:
set code_gb2312 = 0xd6d0;
怎样把它的 utf8 码得到,放到 code_utf8 中。
我的几个做法:
首先定义几个字符变量:- declare hanzi_gb2312 char(4) default '';
- declare hanzi_utf8 char(4) default '';
- declare str_tmp char(255) default '';
复制代码 把 code_gb2312 做成 gb2312 字符放到 hanzi_gb2312 中,再转字符集 utf8 到 hanzi_utf8:- set hanzi_gb2312 = char(code_gb2312 using gb2312);
- set hanzi_utf8 = convert(hanzi_gb2312 using utf8);
复制代码 做法 1:- set str_tmp = hex(hanzi_utf8);
- set code_utf8 = cast(conv(str_tmp, 16, 10) as unsigned);
复制代码 做法 2:- set str_tmp = hex(hanzi_utf8);
- set code_utf8 = conv(str_tmp, 16, 10);
复制代码 这个就是方法 1,只是我发现 cast() 不必要。
做法 3:- set code_utf8 = ord(hanzi_utf8);
复制代码 交流:
有没有更好的方法呢?要求是,不要 warning(或给出忽略告警的方法),函数调用尽量少。
最好不要用 ord(),它只能抽第一个字符的编码,后面的怎么办?
另外,我觉得 ord() 这个家伙是根据被转字符类型的字符集来的,要不它怎么知道多少个非 ASCII 字符字节该拼成一个多字节字符,对么?那么请问字符类型变量在定义时,是否能指定字符集呢,就跟列类型一样。 |
|