亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2008 | 回復(fù): 1
打印 上一主題 下一主題

用 MySQL 函數(shù)轉(zhuǎn)換字符集交流 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2010-04-16 11:54 |只看該作者 |倒序?yàn)g覽
問題描述

存儲(chǔ)過程中有 2 個(gè)變量:
  1.   declare code_gb2312 int unsigned default 0;
  2.   declare code_utf8 int unsigned default 0;
復(fù)制代碼
我先已知 code_gb2312 的值,比如以 '中' 字(gb2312 碼為 0xd6d0)為例:
set code_gb2312 = 0xd6d0;

怎樣把它的 utf8 碼得到,放到 code_utf8 中。

我的幾個(gè)做法

首先定義幾個(gè)字符變量:
  1. declare hanzi_gb2312 char(4) default '';
  2. declare hanzi_utf8 char(4) default '';
  3. declare str_tmp char(255) default '';
復(fù)制代碼
把 code_gb2312 做成 gb2312 字符放到 hanzi_gb2312 中,再轉(zhuǎn)字符集 utf8 到 hanzi_utf8:
  1. set hanzi_gb2312 = char(code_gb2312 using gb2312);
  2. set hanzi_utf8 = convert(hanzi_gb2312 using utf8);
復(fù)制代碼
做法 1:
  1. set str_tmp = hex(hanzi_utf8);
  2. set code_utf8 = cast(conv(str_tmp, 16, 10) as unsigned);
復(fù)制代碼
做法 2:
  1. set str_tmp = hex(hanzi_utf8);
  2. set code_utf8 = conv(str_tmp, 16, 10);
復(fù)制代碼
這個(gè)就是方法 1,只是我發(fā)現(xiàn) cast() 不必要。

做法 3:
  1. set code_utf8 = ord(hanzi_utf8);
復(fù)制代碼
交流

有沒有更好的方法呢?要求是,不要 warning(或給出忽略告警的方法),函數(shù)調(diào)用盡量少。

最好不要用 ord(),它只能抽第一個(gè)字符的編碼,后面的怎么辦?

另外,我覺得 ord() 這個(gè)家伙是根據(jù)被轉(zhuǎn)字符類型的字符集來的,要不它怎么知道多少個(gè)非 ASCII 字符字節(jié)該拼成一個(gè)多字節(jié)字符,對么?那么請問字符類型變量在定義時(shí),是否能指定字符集呢,就跟列類型一樣。

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2010-04-16 11:57 |只看該作者
手冊上 ord() 的說明太少了:

ORD(str)

If the leftmost character of the string str is a multi-byte character, returns the code for that character, calculated from the numeric values of its constituent bytes using this formula:

  (1st byte code)
+ (2nd byte code × 256)
+ (3rd byte code × 2562) ...

If the leftmost character is not a multi-byte character, ORD() returns the same value as the ASCII() function.

mysql> SELECT ORD('2');
        -> 50
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP