- 論壇徽章:
- 0
|
問題描述:
存儲(chǔ)過程中有 2 個(gè)變量:- declare code_gb2312 int unsigned default 0;
- declare code_utf8 int unsigned default 0;
復(fù)制代碼 我先已知 code_gb2312 的值,比如以 '中' 字(gb2312 碼為 0xd6d0)為例:
set code_gb2312 = 0xd6d0;
怎樣把它的 utf8 碼得到,放到 code_utf8 中。
我的幾個(gè)做法:
首先定義幾個(gè)字符變量:- declare hanzi_gb2312 char(4) default '';
- declare hanzi_utf8 char(4) default '';
- declare str_tmp char(255) default '';
復(fù)制代碼 把 code_gb2312 做成 gb2312 字符放到 hanzi_gb2312 中,再轉(zhuǎn)字符集 utf8 到 hanzi_utf8:- set hanzi_gb2312 = char(code_gb2312 using gb2312);
- set hanzi_utf8 = convert(hanzi_gb2312 using utf8);
復(fù)制代碼 做法 1:- set str_tmp = hex(hanzi_utf8);
- set code_utf8 = cast(conv(str_tmp, 16, 10) as unsigned);
復(fù)制代碼 做法 2:- set str_tmp = hex(hanzi_utf8);
- set code_utf8 = conv(str_tmp, 16, 10);
復(fù)制代碼 這個(gè)就是方法 1,只是我發(fā)現(xiàn) cast() 不必要。
做法 3:- set code_utf8 = ord(hanzi_utf8);
復(fù)制代碼 交流:
有沒有更好的方法呢?要求是,不要 warning(或給出忽略告警的方法),函數(shù)調(diào)用盡量少。
最好不要用 ord(),它只能抽第一個(gè)字符的編碼,后面的怎么辦?
另外,我覺得 ord() 這個(gè)家伙是根據(jù)被轉(zhuǎn)字符類型的字符集來的,要不它怎么知道多少個(gè)非 ASCII 字符字節(jié)該拼成一個(gè)多字節(jié)字符,對么?那么請問字符類型變量在定義時(shí),是否能指定字符集呢,就跟列類型一樣。 |
|