問之前有人po上去的東西有關base64的 |
尚未結案
|
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
http://delphi.ktop.com.tw/topic.php?topic_id=28163
var
OrgString,EncodeString:string;
begin
OrgString:='=?Rnc6IERPTU9URVggQ0hJTkEgRkxPT1LlnLDpnaLmnZDmnpPlj4roiJboo50=?=';
ShowMessage('OrgString is:' OrgString);
//EncodeString := IdencoderMIME1.EncodeString(OrgString);
//ShowMessage('After Base64 Encoding is:' #10 #13 EncodeString);
OrgString := IdDecoderMIME1.DecodeString(EncodeString);
ShowMessage('After Base64 Decoding :' OrgString);
end;怎麼無法解出 Fw: DOMOTEX CHINA FLOOR?圈??????鋆
解碼後的樣子啊,可否教教我,我也想學會如何一段程式碼
Base64Decode MultiByteToWideChar WideCharToMultiByte
Base64----------->UTF8-------------->UNICODE--------->ANSI
|
wyndog
資深會員 發表:7 回覆:362 積分:348 註冊:2004-10-12 發送簡訊給我 |
procedure TForm1.Button1Click(Sender: TObject); var OrgString, UTF8String, UnicodeString: string; begin OrgString := 'Rnc6IERPTU9URVggQ0hJTkEgRkxPT1LlnLDpnaLmnZDmnpPlj4roiJboo50='; ShowMessage('OrgString is: ' OrgString); UTF8String := IdDecoderMIME1.DecodeString(OrgString); ShowMessage('=> UTF8String: ' UTF8String); UnicodeString := UTF8Decode(UTF8String); ShowMessage(UnicodeString); end;首先,我在 OrgString 裡頭就把前後那些不屬於 Base64 的拿掉 最後的 UTF8Decode 其實是解出 UnicodeString 不過,這部份 Delphi 是自動處理的,所以 ShowMessage 之類的,還是可以正常解釋 如果還是想轉成 BIG5(ANSI) 字串的,就用 WideCharToMultiByte 你所需要的東西,你引用的文章都有,自己再研究看看 另外,就是我不清楚你解不出來的意思是? 就我所看出來的地方是,一,OrgString 的部份沒有拿乾淨,實驗的結果是會有影響;二,DecodeString 是拿 EncodeString 來 Decode,我看到 EncodeString 的設定被 Remark 掉了,那麼空空的 EncodeString 自然解不出什麼東西來,這個部份請多留意 |
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
function DecodeUTF8 (S: String): WideString;
var rl: Integer;
procedure Plus (c: word);
begin
inc (rl);
if (rl>length(Result)) then //alloc some extra mem
SetLength (Result, length(Result) 512);
Result[rl] := WideChar(c);
end;
var b,c,d,e,r: byte;
w: Word;
i,l: Integer;
begin
//Result := '';
SetLength (Result, length(S));
rl := 0;
i := 1;
l := length(S);
while i<=l do
begin
b := byte(S[i]);
if (b and $80)=0 then //7-bit
Plus (b)
else
if (b and $E0)=$C0 then //11-bit
begin
if i c:=byte(S[i 1])
else
c:=$80;
if (c and $C0)<>$80 then
c := $80; //error. tag with zero. sorry.
b:=b and $1F;
c:=c and $3F;
plus (b shl 6 or c);
inc (i);
end
else
if (b and $F0) = $E0 then //16-bit
begin
if i c := byte(S[i 1])
else
c := $80;
if i d := byte (S[i 2])
else
d := $80;
if (c and $C0)<>$80 then
c := $80; //error. tag with zero. sorry.
if (d and $C0)<>$80 then
d := $80; //error. tag with zero. sorry.
b := b and $0F;
c := c and $3F;
d := d and $3F;
plus ((b shl 12) or (c shl 6) or d);
inc (i,2);
end
else
begin //we have a problem here. a value > 16 bit was encoded
//obviously, this doesn't fit in a widestring..
//fix: leave blank ('space'). sorry.
Plus (ord(' '));
b := b shl 1;
repeat
b := b shl 1;
inc (i);
until (b and $80)=0;
end;
inc (i);
end;
SetLength (Result, rl);
end;請問這個上面放DecodeUTF8這個函數對吧,可是怎麼run的時候有錯呢?可否教教我呢?
|
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
function DecodeUTF8 (S: String): WideString;
var rl: Integer;
procedure Plus (c: word);
begin
inc (rl);
if (rl>length(Result)) then //alloc some extra mem
SetLength (Result, length(Result) 512);
Result[rl] := WideChar(c);
end;
var b,c,d,e,r: byte;
w: Word;
i,l: Integer;
begin
//Result := '';
SetLength (Result, length(S));
rl := 0;
i := 1;
l := length(S);
while i<=l do
begin
b := byte(S[i]);
if (b and $80)=0 then //7-bit
Plus (b)
else
if (b and $E0)=$C0 then //11-bit
begin
if i c:=byte(S[i 1])
else
c:=$80;
if (c and $C0)<>$80 then
c := $80; //error. tag with zero. sorry.
b:=b and $1F;
c:=c and $3F;
plus (b shl 6 or c);
inc (i);
end
else
if (b and $F0) = $E0 then //16-bit
begin
if i c := byte(S[i 1])
else
c := $80;
if i d := byte (S[i 2])
else
d := $80;
if (c and $C0)<>$80 then
c := $80; //error. tag with zero. sorry.
if (d and $C0)<>$80 then
d := $80; //error. tag with zero. sorry.
b := b and $0F;
c := c and $3F;
d := d and $3F;
plus ((b shl 12) or (c shl 6) or d);
inc (i,2);
end
else
begin //we have a problem here. a value > 16 bit was encoded
//obviously, this doesn't fit in a widestring..
//fix: leave blank ('space'). sorry.
Plus (ord(' '));
b := b shl 1;
repeat
b := b shl 1;
inc (i);
until (b and $80)=0;
end;
inc (i);
end;
SetLength (Result, rl);
end;請問這個上面放DecodeUTF8這個函數對吧,可是怎麼run的時候有錯呢?可否教教我呢?
|
wyndog
資深會員 發表:7 回覆:362 積分:348 註冊:2004-10-12 發送簡訊給我 |
|
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
你說在delphi版本裡就有啦,那請問是在哪,怎麼用呢,我是用第七版的,對了,在請問一下
MultiByteToWideChar(CP_UTF8,0,value,-1,swap,1024);//utf8轉unicode
WideCharToMultiByte(CP_ACP,0,swap,-1,value,1024,0,0);//unicode轉ansi
Result :=value;
是什麼意思啊,我看不太懂耶,只是上面寫從UTF8-->UNICode----->ANSI
可是裡面的CP_UTF8,0,value,-1,swap,1024(CP_ACP,0,swap,-1,value,1024,0,0)這些我都看不太懂耶,上面我貼的那個程式是utf8decrypt的,是從網路上下載的,可是run的時候if i c:=byte(S[i 1])if i c := byte(S[i 1])if i d := byte (S[i 2])這三行都顯示type of expression must be BOOLEAN error
原本我以為從網路上下載這個utf8decrypt解密就可以了,但沒想不行,可否請高手教教我,謝謝。我是一位新手,想學。
|
wyndog
資深會員 發表:7 回覆:362 積分:348 註冊:2004-10-12 發送簡訊給我 |
|
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
其實你照我寫的那幾行做
就可以
Base64--->UTF8--->Unicode 至於 MultiByteToWideChar, WideCharToMultiByte
裡頭的參數,查 MSDN 即可知道 UTF8Decode 就照我那樣用就可以了
有什麼問題嗎?它是在 System Unit 的
我也是用 Delphi 7 的,直接就可以用了
請問去哪查msdm啊?那我現在已經解出來Base64--->UTF8--->Unicode,要怎麼轉到unicode--.>ansi啊,
WideCharToMultiByte(CP_ACP,0,swap,-1,value,1024,0,0);//unicode轉ansi
Result :=value;'('expected but','found error顯示這行有錯耶。
|
wyndog
資深會員 發表:7 回覆:362 積分:348 註冊:2004-10-12 發送簡訊給我 |
完整版
procedure TForm1.Button1Click(Sender: TObject); var OrgString, UTF8String, UnicodeString, Big5String: string; begin OrgString := 'Rnc6IERPTU9URVggQ0hJTkEgRkxPT1LlnLDpnaLmnZDmnpPlj4roiJboo50='; ShowMessage('OrgString is: ' OrgString); UTF8String := IdDecoderMIME1.DecodeString(OrgString); ShowMessage('=> UTF8String: ' UTF8String); UnicodeString := UTF8Decode(UTF8String); ShowMessage('=> UnicodeStr: ' UnicodeString); Big5String := UnicodeString; WideCharToMultiByte(CP_ACP, WC_DISCARDNS, PWChar(UTF8String), -1, PChar(Big5String), length(Big5String), nil, nil); ShowMessage('=> AnsiString: ' Big5String); end;MSDN 在 http://msdn.microsoft.com/library WideCharToMultiByte 的參數,依序是 CP_ACP: 表示我要轉成 ANSI (即 BIG5) WC_DISCARDNS: 無法轉換的字不予理會 原始 Unicode 字串的位址 原始 Unicode 字串的長度 (-1 表示此字串是 0 結尾的,長度由 API 自行判定) 轉成 Ansi 字串的位址 (*1) 轉成 Ansi 字串的長度 (*1) 後面二個參數,我們用不到,不理它,設成 nil (*1) 因為上面的碼中,Big5String 沒初值是不能拿來給WideCharToMultiByte用的 你可以用 SetLength(Big5String, 1024) 來配給該字串長度 我比較懶,反正 Big5String 的長度一定是小於 UnicodeString 的 所以,我就直接設 Big5String := UnicodeString 這樣就算是給了 Big5String 一個初始值了 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |