我試圖從 mysql 獲取 UTF-8 重音字符“锓ê”,并在通過 SMPP 發送時將它們轉換為 UCS-2。資料存盤為 utf8_general_ci,我在打開資料庫連接時執行以下操作:
$dbh->{'mysql_enable_utf8'}=1;
$dbh->do("set NAMES 'utf8'");
如果我通過使用 data_encoding=8 用 "é" "ê" 對字串值進行硬編碼來測驗發送部分,它會完美地通過。但是,如果我注釋掉第一行并只使用來自資料庫的內容,它就會失敗。此外,如果我嘗試使用 DB 發送字符并設定 data_encoding=3,它也可以正常作業,但隨后不會出現“ê”,這也是意料之中的。這是我使用的:
$fred = 'éêcole'; <-- If I comment out this line, the SMPP call fails
$fred = decode('utf-8', $fred);
$fred = encode('UCS-2', $fred);
$resp_pdu = $short_smpp->submit_sm(
source_addr_ton => 0x00,
source_addr_npi => 0x01,
source_addr => $didnb,
dest_addr_ton => 0x01,
dest_addr_npi => 0x01,
destination_addr => $number,
data_coding => 0x08,
short_message => $fred
) or do {
Log("ERROR: submit_sm indicated error: " . $resp_pdu->explain_status());
$success = 0;
};
data_coding 欄位的不同值如下: SMPP 中“data_coding”欄位的含義
00000000 (0) - usually GSM7
00000011 (3) for standard ISO-8859-1
00001000 (8) for the universal character set -- de facto UTF-16
SMPP 提供商的檔案還提到應通過 UCS-2 處理特殊字符:https : //community.sinch.com/t5/SMS-365-enterprise-service/Handling-Special-Characters/ta-p/1137
我應該如何準備來自資料庫的資料以使這個 SMPP 呼叫作業?
我正在使用 Perl v5.10.1
謝謝 !
uj5u.com熱心網友回復:
$dbh->{'mysql_enable_utf8'} = 1;用于解碼從資料庫回傳的值,使查詢回傳解碼的文本(Unicode 代碼點字串)。解碼這樣的字串是沒有意義的。直奔encode.
my $s_ucp = "\xE9\xEA\x63\x6F\x6C\x65"; # éêcole
# -or-
use utf8; # Script is encoded using UTF-8.
my $s_ucp = "éêcole";
printf "%vX\n", $s_ucp; # E9.EA.63.6F.6C.65
my $s_ucs2be = encode('UCS-2', $s_ucp);
printf "%vX\n", $s_ucs2be; # 0.E9.0.EA.0.63.0.6F.0.6C.0.65
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/378825.html
上一篇:我無法將任何值存盤在陣列@AA中
