Article View: news.software.nntp
Article #62132Re: UUdecoding question
From: Curt Welch
Date: Wed, 27 Nov 2002 04:13
Date: Wed, 27 Nov 2002 04:13
85 lines
3987 bytes
3987 bytes
"MrMxyzptlk" <yaya@hotmail.com> wrote:
> Ok I have used the code below and passed in a line at time from a
> uuencoded message. (stripped of blank lines, begin and end lines and, the
> leading M removed)
I wouldn't throw the M away. I'd use it instead of trusting the line
length to be correct. But for a properly formated line I guess it doesn't
make much difference. But it would be good to check to make sure the two
match.
> Now what? I tried writing the lines out in a binary format and a text
> format but the files are unusable.
>
> For example say I decode a jpg, when I open the file there is no image.
Find a uudecode tool that works. Compare what it produces to what your
code produces. Fix whatever is wrong.
> Can anyone guide me a little more?
>
> Thank you for your time, Anthony.
What language is this?
> *****************************************************************
>
> public string uuDecode(string sBuffer)
>
> {
>
> string str1=String.Empty;
>
> int j = sBuffer.Length ;
>
> for (int i = 1; i <= j; i += 4)
The last line won't have a length that's a multiple of 4, so you need to do
more testing. That that will only cause your code to add 1 or 2 extra
bytes so that won't prevent a jpg from viewing.
And as a matter of style, the Substring() function uses a 0 based index, so
it's silly to start the loop here at 1. It's cleaner if you write:
for (i = 0; i < j; i += 4)
and then use i, i+1, i+2 and i+3, instead of i-1, i, i+1, i+2 like you
do below.
> {
>
> str1 = String.Concat(str1, Convert.ToString((char)(((int)Convert.ToChar(
> sBuffer.Substring ( i-1, 1)) - 32) * 4 + ((int)Convert.ToChar(
> sBuffer.Substring( i , 1)) - 32) / 16)));
You can't just subtract 32 from the character to get the 6 bit value. You
have to take only the low 6 bits. i.e., do an AND operation with 63 after
subtracting 32. (or mod 64).
The orginal uuencode would map nulls to spaces. But some text programs
will strip trailing spaces from lines which would break most uudecoding
programs. So, common practice today is to use ` for NULL instead of space
which works fine as long as you ignore the higher bits.
> str1 = String.Concat(str1, Convert.ToString((char)(((int)Convert.ToChar(
> sBuffer.Substring( i , 1)) % 16 * 16) +
> ((int)Convert.ToChar(sBuffer.Substring( i + 1, 1)) - 32) / 4)));
>
> str1 = String.Concat(str1,
> Convert.ToString((char)(((int)Convert.ToChar(sBuffer.Substring ( i + 1,
> 1)) % 4 * 64) + (int)Convert.ToChar( sBuffer.Substring( i + 2, 1)) -
> 32)));
>
> }
>
> return str1;
>
> }
The rest of the shifting and adding look correct - assuming your
Convert.ToChar stuff always produces a postive number from 0 to 255.
--
Curt Welch http://CurtWelch.Com/
curt@kcwc.com Webmaster for http://NewsReader.Com/
Technical Details
Message-ID:
<20021126231315.491$FH@newsreader.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!news-out.cwix.com!newsfeed.cwix.com!newsfeed1.cidera.com!Cidera!cyclone1.gnilink.net!ngpeer.news.aol.com!feed3.newsreader.com!newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mailReferences:
<PcycndP5r-WJsnmgXTWcoA@giganews.com>Show Full Headers
Complete Article Headers
Message-ID: <20021126231315.491$FH@newsreader.com>
Subject: Re: UUdecoding question
Date: Wed, 27 Nov 2002 04:13:15 +0000
From: curt@kcwc.com (Curt Welch)
References: <PcycndP5r-WJsnmgXTWcoA@giganews.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!news-out.cwix.com!newsfeed.cwix.com!newsfeed1.cidera.com!Cidera!cyclone1.gnilink.net!ngpeer.news.aol.com!feed3.newsreader.com!newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mail
Organization: NewsReader.Com
Newsgroups: news.software.nntp
X-Trace: NewsReader.Com uXQ,ylw//9xghvw9_FLYPtk,rt1@IipSQ7IcfRAl+Ftc_80e
X-Face: "p}G*1KH{+F7EYGKLb>ogDguabZ+%,?^epeFB!nzu`)`$=QcvL1KF6<0GH!Tbc!Sqo[|tV5
%IW48mQf3K=Ci&gZ7]]aazx@]Y-nq!r5{yH/#,?@lDdUDvOfByB2hVW0.@OM%{l/{cT'{w
X-Url: http://CurtWelch.Com/
User-Agent: NewsReader.Com
Lines: 84