Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Base64 and ISO-8859 Decode

Status
Not open for further replies.

ThunderForest

IS-IT--Management
Mar 3, 2003
189
US
Delphi 4.0

Can someone point me to some useful Delphi information on how to decode Base64 eMail message text and ISO-8859 encoded eMail subjects? Thanks.

Getting answers before I'm asked
is why I go to Tek-Tips.
 
I'm not sure as to why I'm not getting a response on this. Wrong post? I have code below, however crude, that seems to obtain at least the 8 bit binary values of an ASCII char. What am I doing wrong on the 6 bit? I get all zeros. First, this is my logic for decoding Base64:

Get character(s)
Get decimal value of each character
Convert decimal value to 6 bit binary for each character
Concatenate string of 6 bit binary values
Divide string by 8 characters (8 bit binary value)
Obtain decimal value of each 8 bit string
Get ASCII value of decimal

R1 := 1;
M:=1;
while (R1 <= 255) do begin
// Get 8 bit binary value
Str := chr(R1);
M := abs(M);
S := '';
for i:=1 to SizeOf(M)*8 do begin
if M<0 then begin
S:=S+'1'
end
else begin
S:=S+'0';
end;
M:=M shl 1;
end;
Delete(S,1,Pos('1',S)-1);

// Get 6 bit binary ????
M := R1;
M := abs(M);
t := '';
for i:=1 to SizeOf(M)*6 do begin
if M<0 then begin
t:=t+'1'
end
else begin
t:=t+'0';
end;
M:=M shl 1;
end;
Delete(t,1,Pos('1',t)-1);

if (length(s) = 1) then begin
s := '0000000'+s;
end
else if (length(s) = 2) then begin
s := '000000'+s;
end
else if (length(s) = 3) then begin
s := '00000'+s;
end
else if (length(s) = 4) then begin
s := '0000'+s;
end
else if (length(s) = 5) then begin
s := '000'+s;
end
else if (length(s) = 6) then begin
s := '00'+s;
end
else if (length(s) = 7) then begin
s := '0'+s;
end;

if (length(t) = 1) then begin
t := '00000'+t;
end
else if (length(t) = 2) then begin
t := '0000'+t;
end
else if (length(t) = 3) then begin
t := '000'+t;
end
else if (length(t) = 4) then begin
t := '00'+t;
end
else if (length(t) = 5) then begin
t := '0'+t;
end;

cTbl.insert;
cTbl.fieldByName('Decimal').asInteger := R1;
cTbl.fieldByName('Letter').asString := chr(R1);
cTbl.fieldByName('Bit8').asString := s;
cTbl.fieldByName('Bit6').asString := t;
cTbl.post;
R1 := R1 + 1;
M := R1;
end;
cTbl.active := True;


Getting answers before I'm asked
is why I go to Tek-Tips.
 
The better I can do is point you to RFC 1521, where base64 encoding is defined. The way to decode base64 is not shifting bits one by one but converting 4 bytes input bursts in 3 bytes output bursts.

About your code, it depends on the datatype of M, what you are not showing here, so I only can guess.

If M is a byte, you can't never get it below zero, so "S" and "t" will be '00000000' and '000000', not matter what value it can have. A byte is unsigned by definition.

If M is not a byte, you are screwing what I believe is your idea, as SizeOf(M) * 8 will produce 16 or 32 and not 8 and the "if length(t)... else if..." have no meaning.

The only way I see for your code doing something is using a shortint for M. But "something" is probably not what you are trying to get and I can't understand what you are trying to do anyway :).

buho (A).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top