線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:6178
推到 Plurk!
推到 Facebook!

Base64中文問題

答題得分者是:pcboy
superlevin
高階會員


發表:181
回覆:313
積分:180
註冊:2003-01-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-07-27 16:05:05 IP:61.30.xxx.xxx 訂閱
大家好!以下函數可處理英文,但無法處理中文?請問問題為? <textarea class="delphi" rows="10" cols="60" name="code"> function StrToBase64(const Str: string): string; var I, J, K, Len, Len1: Integer; B3: array[0..2] of Byte; B4: array[0..3] of Byte; begin if Str = '' then begin Result := ''; Exit; end; Len := Length(Str); Len1 := ((Len 2) div 3) shl 2; SetString(Result, nil, Len1); I := 1; J := 1; while I <= Len do begin for K := 0 to 2 do if K I > Len then B3[K] := 0 else B3[K] := Ord(Str[K I]); B4[0] := B3[0] shr 2; B4[1] := ((B3[0] shl 4) or (B3[1] shr 4)) and 63; B4[2] := ((B3[1] shl 2) or (B3[2] shr 6)) and 63; B4[3] := B3[2] and 63; for K := 0 to 3 do begin case B4[K] of 0..25: Result[J] := Chr(B4[K] 65); // 'A'..'Z' 26..51: Result[J] := Chr(B4[K] 71); // 'a'..'z'(B4[K]-26 97) 62: Result[J] := ' '; 63: Result[J] := '/'; else Result[J] := Chr(B4[K] - 4); // '0'..'9'(B4[K]-52 48) end; // Result[J] := Base64_Chars[B4[K] 1]; Inc(J); end; Inc(I, 3); end; K := 3 - Len mod 3 - 1; if K <> 2 then for I := Len1 - K to Len1 do Result[I] := '='; end; function Base64ToStr(const Base64: string): string; var I, J, K, Len, Len1: Integer; B4: array[0..3] of Byte; begin if Base64 = '' then begin Result := ''; Exit; end; Len := Length(Base64); if Len and 3 <> 0 then raise Exception.Create('Invalid Base64 length'); Len1 := (Len shr 2) * 3; SetString(Result, nil, Len1); I := 1; J := 1; while I <= Len do begin for K := 0 to 3 do begin case Base64[I] of 'A'..'Z': B4[K] := Ord(Base64[I]) - 65; 'a'..'z': B4[K] := Ord(Base64[I]) - 71; '0'..'9': B4[K] := Ord(Base64[I]) 4; ' ': B4[K] := 62; '/': B4[K] := 63; '=': B4[K] := 0; else raise Exception.CreateFmt('#%d: Invalid char in Base64', [Ord(Base64[I])]); end; Inc(I); end; Result[J] := Chr((B4[0] shl 2) or (B4[1] shr 4)); Result[J 1] := Chr((B4[1] shl 4) or (B4[2] shr 2)); Result[J 2] := Chr((B4[2] shl 6) or B4[3]); Inc(J, 3); end; I := Pos('=', Base64); if I <> 0 then begin I := Len - I 1; Delete(Result, Len1 - I 1, I); end; end;</textarea>
------
林壽山
網站: http://superlevin.ifengyuan.tw
mail: superlevin@gmail.com
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-07-27 16:40:34 IP:61.220.xxx.xxx 訂閱
測試正常啊, 您使用的中文字是什麼 ?

<textarea class="delphi" rows="10" cols="60" name="code"> // Delphi 2007 Enterprise for Win32 procedure TForm1.FormCreate(Sender: TObject); var s: String; begin s:=StrToBase64('測試'); s:=Base64ToStr(s); end;</textarea>
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
superlevin
高階會員


發表:181
回覆:313
積分:180
註冊:2003-01-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-07-27 16:48:42 IP:61.30.xxx.xxx 訂閱
delphi.ktop.com.tw/download.php
如果是中文字發出後無法傳送手機簡訊
------
林壽山
網站: http://superlevin.ifengyuan.tw
mail: superlevin@gmail.com
danny
版主


發表:100
回覆:522
積分:595
註冊:2002-03-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-07-27 16:52:52 IP:211.76.xxx.xxx 訂閱
我用比較會有問題的中文字 "庭" "" 也正常!
------
將問題盡快結案也是一種禮貌!
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-07-27 17:02:22 IP:61.220.xxx.xxx 訂閱
您的 Delphi 版本和小弟 Delphi 2007 不同 (不過這個錯誤不重要, 可以忽略)

Error Reading Form
Error reading IdHTTP1.MaxLineAction: Property
MaxLineAction does not exist. Ignore the error and
continue? NOTE: Ignoring the error may cause
components to be delete ot property values to be lost.
--------------------------------------------------
小弟沒有 http://api.mysms.tw/ 的帳號密碼可以測試耶
隨便輸入沒有效果
--------------------------------------------------
您確定該網頁可以用來傳送 中文字簡訊 給手機 嗎 ?
或者另外找一個 BASE64 編碼軟體, 比對看看您編碼的結果是否正確
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
superlevin
高階會員


發表:181
回覆:313
積分:180
註冊:2003-01-12

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-07-27 17:09:44 IP:61.30.xxx.xxx 訂閱
確認是可以的,因為之前以C#開發過此類的函數
<textarea class="c#" rows="10" cols="60" name="code"> /// /// 傳送手機簡訊程式 for 簡訊快易通 /// Example: /// SendMessage("0921123456", "test", "test", "簡訊測試"); /// /// 手機號碼(0921123456)
/// 登入帳號(test)
/// 登入密碼(test)
/// 簡訊內容 (test)
/// 成功/失敗 /// public static Boolean SendMessage(string sPhoneNum, string sUserName, string sPassword, string sContent) { #region API規格說明 /* ●傳送內容規格 傳送格式【參數】 功能 備註 level 登入等級 #必須 經銷商為B,公司為C,個人為D user_name 使用者名稱 #必須 password 密碼 #必須 fun 執行功能 #必須 請參考功能說明及代碼 mobile 發送號碼 #發送簡訊必需 手機門號的範圍限於台灣六大系統業者,手機號碼發送請使用這樣的格式, 例如:0920222111 如果是多通傳送,中間請使用逗號(,)隔開。 例如:0920222111,0920111222 PO_Time 發送時間 #發送預約簡訊必需 格式為 YYYY-MM-DD HH:MM (24小時制,II單位以每5分鐘為一單位) 例如: 2002-11-08 15:30 注意:1小時內將自動轉為即時發送 sms_text 訊息內容 #發送簡訊必需 英文簡訊160個字元一則,中英文70個字元一則。 注意:請將內容以 base64編碼後在進行傳送 job_id 簡訊ID #查詢必須 ●功能說明及代碼: 功能說明 功能代碼 查詢簡訊額度 fun = 0 發送即時簡訊 fun = 1 發送預約簡訊 fun = 2 詳細簡訊記錄查詢 fun = 4 查詢簡易簡訊情形 fun = 5 ● 回傳規格 1.回傳負數為錯誤碼,請參考錯誤碼說明表 2.查詢簡訊額度,回傳額度 3.發送簡訊,回傳簡訊ID 4.簡訊記錄查詢,回傳為CSV格式 第一行為資料說明,第2行開始為資料,一行為一筆 ●錯誤碼說明表(error code): 錯誤代號 錯誤說明 -1 序號無效 -2 未設定客戶聯絡手機 -3 電話格式錯誤 -4 帳號/密碼/Mac不正確 (含無此帳號) -5 帳號被停權 -6 額度不足 -7 沒有「發送電話」、「簡訊內容」 -8 日期/時間格式不正確 -9 沒有此訊息ID -10 系統維護中 -11 此序號已開通 -12 資料不正確 -13 網卡位址無效 -14 網卡已被使用 -99 未定義 ●簡訊狀態回報代碼: 狀態代碼 備註 1 已傳送 DELIVERED 已傳送至收件人 2 未傳送 UNDELIVERED ?手機未開機 ?訊息已滿 3 重送 REJECTED 4 錯誤的訊息 FAIL-TO-SMSG ?空號 ?號碼被擋 5 等待 WAIT 處理中,等候回應 6 送達 SUMMIT 已送達業者 7 錯誤 ERROR 系統發生錯誤 8 刪除 DELETED 9 逾時 EXPIRED 簡訊系統無回應 10 查詢 QUEUE */ #endregion int number; if (sPhoneNum == "") { MessageBox.Show("手機號碼未輸入,請確認!", "警告"); return false; } if (sContent == "") { MessageBox.Show("簡訊內容未輸入,請確認!", "警告"); return false; } //檢查手機號碼格式 // try { number = int.Parse(sPhoneNum); } catch { MessageBox.Show("手機格式錯誤,請確認!", "警告"); return false; } //宣告參數值 string level = "D"; string user_name = sUserName; string password = sPassword; string fun = "1"; string mobile = sPhoneNum; string encode = ""; byte[] bytes = Encoding.UTF8.GetBytes(sContent); encode = Convert.ToBase64String(bytes); //2007.05.11 add by lin shou shan //需再經由UrlEncodeUnicode才能正確輸出字元 encode = System.Web.HttpUtility.UrlEncodeUnicode(encode); //傳遞網頁-post UTF8Encoding encoding = new UTF8Encoding(); string postData = "level=" level "&user_name=" user_name "&password=" password "&fun=" fun "&mobile=" mobile "&sms_text=" encode; byte[] data = encoding.GetBytes(postData); // Prepare web request... HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.mysms.tw/api/api.php"); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; Stream newStream = myRequest.GetRequestStream(); // Send the data. newStream.Write(data, 0, data.Length); newStream.Close(); // Get response HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default); string content = reader.ReadToEnd(); Console.WriteLine(content); if (int.Parse(content) > 0) { MessageBox.Show("手機簡訊傳輸成功!", "結果"); return true; } else { MessageBox.Show("手機簡訊傳輸失敗,請確認!", "結果"); return false; } return true; } } </textarea>
------
林壽山
網站: http://superlevin.ifengyuan.tw
mail: superlevin@gmail.com
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#7 引用回覆 回覆 發表時間:2007-07-27 17:21:16 IP:61.220.xxx.xxx 訂閱
UniCode 編碼和 BASE64 編碼好像不同吧, 對字串和對 URL 編碼不知道是否也不同 (URL 編碼是否會對某些字不編碼?)
<textarea class="delphi" rows="10" cols="60" name="code"> Content.Text :='level=D&user_name='+SMSUser.Text+'&password='+SMSPwd.Text+'&fun=1&mobile='+SendToNo.Lines[i]+'&sms_text=' +strtobase64(sendmsg.Lines.Text); try //傳送 Response := IdHTTP1.Post(web,Content); </textarea>
下面好像是 URL 的 UniCode 編碼 <textarea class="delphi" rows="10" cols="60" name="code"> encode = System.Web.HttpUtility.UrlEncodeUnicode(encode); </textarea>
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#8 引用回覆 回覆 發表時間:2007-08-03 03:00:14 IP:61.64.xxx.xxx 訂閱
這根是否為中文無關吧

如過將中文視為英文然後編碼
這樣解碼後,還是原來的東西
除非你的編解碼有錯,但是這樣連英文都會錯誤

所以解出來,在將之當成中文來看,也會是原結果
系統時間:2024-04-19 3:12:57
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!