10343 - Base64 Decoding

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

User avatar
the LA-Z-BOy
Learning poster
Posts: 94
Joined: Wed Jul 31, 2002 12:44 pm
Location: Dacca, Bangladesh
Contact:

Post by the LA-Z-BOy » Tue Jun 13, 2006 8:02 am

To pipo

1. You print the output using printf("%s")... which is not good here because you might have to print ASCII value 0's.
2. Your program does not differentiate the following cases ... (It treats the same...)

Code: Select all

AA==#
`or'
AAA=#
`or'
AAAA#
You have to check for '='.... more carefully.
Istiaque Ahmed [the LA-Z-BOy]

beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

Post by beloni » Tue Jun 13, 2006 4:38 pm

thank you very much LA-Z-BOy...
my code was AC!
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor

bobeagle
New poster
Posts: 1
Joined: Wed Jan 24, 2007 7:06 am
Location: Hong Kong
Contact:

Why should they be different?

Post by bobeagle » Wed Jan 24, 2007 7:12 am

the LA-Z-BOy wrote:To pipo

1. You print the output using printf("%s")... which is not good here because you might have to print ASCII value 0's.
2. Your program does not differentiate the following cases ... (It treats the same...)

Code: Select all

AA==#
`or'
AAA=#
`or'
AAAA#
You have to check for '='.... more carefully.
Excuse me, but I don't understand why should they be different?
I treat '=' the same as 0, then change every 4*6bits into 3*8bits. 0s are checked in the output process, when they are occured, ignore them.
No signature is my signature

tsangsiryoyo
New poster
Posts: 1
Joined: Thu Jan 25, 2007 6:17 am

10343 Help~~~

Post by tsangsiryoyo » Thu Jan 25, 2007 6:18 am

my program always get WA and i don't know why. pls help!!!

can anyone give me a test case?



#include<stdio.h>

char table[] = {
0,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,62,-1,-1,-1,63,52,53,
54,55,56,57,58,59,60,61,-1,-1,
-1, 0,-1,-1,-1, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9,10,11,12,13,14,
15,16,17,18,19,20,21,22,23,24,
25,-1,-1,-1,-1,-1,-1,26,27,28,
29,30,31,32,33,34,35,36,37,38,
39,40,41,42,43,44,45,46,47,48,
49,50,51,-1,-1,-1,-1,-1,-1,-1
};

int lineCount = 0;
char ended = 0;

void printing(unsigned char Char){
lineCount ++;

putchar(Char);
if (Char == '\n')
lineCount = 0;
else if (!(lineCount % 76)){
putchar('\n');
}
}

void decode(char string[]){
char temp[] = {
table[string[0]],
table[string[1]],
table[string[2]],
table[string[3]]
};

char result = (temp[0] << 2) + (temp[1] >> 4);
printing(result);

result = (temp[1] << 4) + (temp[2] >> 2);
if (string[2] != '=')
printing(result);

result = (temp[2] << 6) + (temp[3]);
if (string[3] != '=')
printing(result);
}

char readKey(){
char temp;
reread:
temp = getchar();

recheck:
if (temp == -1){
exit(0);
}else if (temp == '#'){
if (!ended){
printing('#');
ended = 1;
}
temp = getchar();
goto recheck;
}else if (table[temp] == -1){
goto reread;
}else if (temp == 0){
exit(0);
}
ended = 0;
return temp;
}

int main(){
while(1){
char string[] = {
readKey(),
readKey(),
readKey(),
readKey(),
0
};

decode(string);
}
return 0;
}

User avatar
nealzane
New poster
Posts: 23
Joined: Tue Dec 10, 2002 2:13 am
Location: China
Contact:

Post by nealzane » Wed Feb 13, 2008 7:49 am

It seems the input is blended with some characters NOT within the base 64 set (upper cases, lower cases, digits, '+', '/', '=' and '#').
My code gets WA if I do not check the character set but got AC just after I filter the characters.
8-)

dibery
Learning poster
Posts: 76
Joined: Sat Feb 23, 2013 4:16 pm
Location: Taiwan, Taipei
Contact:

Re: 10343 - Base64 Decoding

Post by dibery » Thu Jun 23, 2016 3:39 pm

Getting PE.
Please help me.

My idea is as follows:
1. Use input() function to get input from stream.
2. Treat 4 chars from input each time.
3. Convert into index.
4. Calculate the value of the output.

Note. If I delete the puts( "" ) on line 36, I get a WA.

Thanks.

Code: Select all

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const char* list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

bool input( string& s )
{
	s.clear();
	while( cin.peek() != '#' )
		if( strchr( list, cin.peek() ) )
			s += cin.get();
		else
			cin.get();
	cin.get();
	return !s.empty();
}

int main()
{
	for( string s; input( s ); putchar( '#' ) )
		for( int i = 0; i < s.size(); i += 4 )
		{
			char now[] = { s[ i ], s[ i + 1 ], s[ i + 2 ], s[ i + 3 ] };
			int val[ 4 ], word[ 3 ];

			for( int p = 0; p < 4; ++p )
				val[ p ] = ( strchr( list, now[ p ] ) - list ) % 64;
			word[ 0 ] = val[ 0 ] << 2 | val[ 1 ] >> 4;
			word[ 1 ] = val[ 1 ] % 16 << 4 | val[ 2 ] >> 2;
			word[ 2 ] = val[ 2 ] % 4 << 6 | val[ 3 ];
			for( int p = 0; p < 3; ++p )
				if( now[ p + 1 ] != '=' )
					printf( "%c", word[ p ] );
		}
	puts( "" );
}
Life shouldn't be null.

Post Reply

Return to “Volume 103 (10300-10399)”