Page 1 of 2
UUEncode \ UUDecode
Posted: Sat Jan 03, 2004 8:26 am
by Wayne Diamond
Code updated For 5.20+
Code: Select all
Procedure.s UUDecode(sInBuf.s)
sOutBuf.s = ""
For lLoop = 1 To Len(sInBuf) Step 4
sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop, 1)) - 32) * 4 + (Asc(Mid(sInBuf, lLoop + 1, 1)) - 32) / 16)
sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop + 1, 1)) % 16) * 16 + (Asc(Mid(sInBuf, lLoop + 2, 1)) - 32) / 4)
sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop + 2, 1)) % 4) * 64 + Asc(Mid(sInBuf, lLoop + 3, 1)) - 32)
Next lLoop
ProcedureReturn sOutBuf
EndProcedure
Procedure.s UUEncode(sInBuf.s)
sOutBuf.s = ""
For lLoop = 1 To Len(sInBuf) Step 3
sOutBuf = sOutBuf + Chr(Asc(Mid(sInBuf, lLoop, 1)) / 4 + 32)
sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop, 1)) % 4) * 16 + Asc(Mid(sInBuf, lLoop + 1, 1)) / 16 + 32)
sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop + 1, 1)) % 16) * 4 + Asc(Mid(sInBuf, lLoop + 2, 1)) / 64 + 32)
sOutBuf = sOutBuf + Chr(Asc(Mid(sInBuf, lLoop + 2, 1)) % 64 + 32)
Next lLoop
ProcedureReturn sOutBuf
EndProcedure
sText.s = uuEncode("1234567890abcdefghijklmnopqrstuvwxyz")
Debug "Encoded=" + sText
sText = uuDecode(sText)
Debug "Decoded=" + sText
Posted: Sat Jan 03, 2004 9:07 am
by blueznl
thx needed this
Posted: Sat Jan 03, 2004 11:23 am
by einander
Thanks Wayne, it's very useful!
Tip:
In PB you don't need to write
is shorter and the result is the same; works also for numeric values.
Regards
Einander
Posted: Sun Jan 04, 2004 9:09 pm
by RJP Computing
I have a few stupid newbie to UUEncode and email stuff. So be nice.
I am tring to make myself a program to turn email attachments back to usable items. Can I use these for this function? I also don't know how to tell where to start to grab the portion of the email that is the attachment. Can somebody help me out?
Thanks
Posted: Mon Jan 05, 2004 6:43 am
by Wayne Diamond
RJP,
Can I use these for this function?
Yes, that's they're primary use (email-related), as that is pretty much the only time that UUEncoding is ever used on the Internet.
I also don't know how to tell where to start to grab the portion of the email that is the attachment.
In my demo you'll notice that I was encoding the string "1234567890abcdefghijklmnopqrstuvwxyz". So I ensured that my email client was encoding attachments in UUEncode rather than the default Base64 (there'll be an option in your email client for this), created a dummy file called test.exe, and added that "123(etc)..." string to it. I then emailed that file to myself, and checking the email message source I can then see this:
Code: Select all
begin 666 temp.exe
D,3(S-#4V-S@Y,&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ
`
end
If you just take that one encoded line and remove the "D" from the start of it (so, ",3(S-#4V-S@Y,&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ"), you can then send that entire line to my decode function. The result:
1234567890abcdefghijklmnopqrstuvwxyz
Regards,
Wayne
Posted: Mon Jan 05, 2004 9:35 am
by Seldon
It means I can pass a memory buffer filled with a file (instead of a string buffer) to your UUEconde function (of course with some modifications) ?
Posted: Mon Jan 05, 2004 9:39 am
by Wayne Diamond
Seldon, that's correct. The only thing you'd probably need to do is strip out any carriage returns/line feeds (0x0D and 0x0A respectively).
Regards,
Wayne
Posted: Tue Jan 06, 2004 3:08 am
by RJP Computing
Do you have any base64 encoding/decoding routines? Just wondering.
Sweet picture

Posted: Tue Jan 06, 2004 3:30 am
by Wayne Diamond
I have half a dozen different variations of Base 64 including inline asm but haven't had a chance to port any over to Purebasic yet, maybe later this week with any luck
Regards,
Wayne
Posted: Tue Jan 06, 2004 4:37 am
by PB
> Do you have any base64 encoding routines?
What's wrong with PureBasic's base64 encoder?
http://www.purebasic.com/documentation/ ... coder.html
Posted: Tue Jan 06, 2004 4:40 am
by RJP Computing
@PB,
I need a decode that isn't in pb or I would definatly use that!
@Wayne,
Can't wait. Thanks Wayne.
I use Popcorn for my email program and it doesn't work with attachments, so I want to make a utility that can decode attachments. I am eagerly waiting. I am close with an example from the forum but I can't get it to create working .exe from what outlook sends. I think it is because I can't figure out how to find what the output file size should be.
http://purearea.net/pb/CodeArchiv/Encod ... Example.pb
Posted: Tue Jan 06, 2004 4:53 am
by PB
> I need a decode that isn't in pb or I would definatly use that!
You originally said encode (a typo I now realize) and hence my confusion.
To decode base64:
viewtopic.php?t=4970
Hope this helps!
Posted: Tue Jan 06, 2004 7:55 am
by RJP Computing
@PB
Yes. Thanks for that help.
Re: UUEncode \ UUDecode
Posted: Mon Oct 26, 2015 12:26 pm
by Dude
The UUEncode function is buggy: it always leaves off the first character, compared to online UUEncoders.
The result from the code sample in the first post:
Code: Select all
,3(S-#4V-S@Y,&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ
But from
http://www.textencode.com/encoder/uuencodeEncoder and other sites:
Code: Select all
D,3(S-#4V-S@Y,&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ
Re: UUEncode \ UUDecode
Posted: Mon Oct 26, 2015 1:55 pm
by infratec
Hi,
the procedure is correct.
What you are missing is the length character.
You can add
at the start to get the wanted result.
Like this (optimized version):
Code: Select all
Procedure.s UUEncode(sInBuf.s)
Protected sOutBuf.s, lLoop.i
sOutBuf + Chr(Len(sInBuf) + ' ')
For lLoop = 1 To Len(sInBuf) Step 3
sOutBuf + Chr(Asc(Mid(sInBuf, lLoop, 1)) >> 2 + ' ')
sOutBuf + Chr(((Asc(Mid(sInBuf, lLoop, 1)) & %11) << 4) | (Asc(Mid(sInBuf, lLoop + 1, 1)) >> 4) + ' ')
sOutBuf + Chr(((Asc(Mid(sInBuf, lLoop + 1, 1)) & %1111) << 2) | (Asc(Mid(sInBuf, lLoop + 2, 1)) >> 6) + ' ')
sOutBuf + Chr(Asc(Mid(sInBuf, lLoop + 2, 1)) & %111111 + ' ')
Next lLoop
ProcedureReturn sOutBuf
EndProcedure
But then you have also to change the Decode to respect the length byte in front.
Bernd