tstr is not null terminated. strcpy behavior will be undefined.
str2 is not null terminated. strcpy behaviour will be undefined.
Off by one error in toBinary, str2 will overflow by one as well as overwrite one "bit" of previous data.
To win you must decrypt this string: D3430373C3A3719191910343910301031303A393
Post your code too!
Winner gets a cookie.
The implementation of the encryption to get this was:
The most compact decimal to binary and vice versa functions you will ever see xDCode:#include <iostream> using namespace std; void toBinary(int n,char* str,int d){ if(d<0)return; str[d]=n%2+'0'; toBinary(n>>1,str,d-1); } int toDecimal(char* str,int d){ if(d<=1)return str[0]-'0'; return ((str[0]-'0')<<(d-1))+toDecimal(str+1,d-1); } char* encrypt(char* str){ int i,j; int len = (strlen(str))*8; char* str2=new char[len+1]; for(i=0;i<(strlen(str))*8;i++){ str2[i]='0'; } for(i=0;i<strlen(str);i++){ int n = (int)str[i]; toBinary(n,str2+i*8,8); } //edit str2[len]='\0'; char tstr[5]; memcpy(tstr,str2,4); //edit tstr[4]='\0'; strcpy(str2,str2+4); strcpy(str2+len-4,tstr); char* encryptedStr=new char[len/4+1]; char map[]="0123456789ABCDEF"; for(i=0;i<len/4;i++){ encryptedStr[i]=map[toDecimal(str2+4*i,4)]; } encryptedStr[i]='\0'; return encryptedStr; } int main(void){ char str[256]; gets(str); char* encrypterStr = encrypt(str); cout<<encrypterStr; }
Last edited by zhaoyun333; 08-14-2011 at 08:53 AM.
There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi
tstr is not null terminated. strcpy behavior will be undefined.
str2 is not null terminated. strcpy behaviour will be undefined.
Off by one error in toBinary, str2 will overflow by one as well as overwrite one "bit" of previous data.
Hell_Demon (08-14-2011)
Must have removed the null terminators while removing my debug stuff =3
Added them back though
toBinary will always write only to 8 characters
There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi
Nope, you still have an off by one error. C arrays are zero based. Your base case for your recursive function checks for less than zero. The index parameter passed to toBinary is eight. Indices eight to zero are written to, which amounts to nine characters written.
You should check your code and save some face next time you present a challenge.
Shit ok the string is : A68606E78746E323232206872206020626074737
New code:
Now how about actually decrypting it FoveaCode:#include <iostream> using namespace std; void toBinary(int n,char* str,int d){ if(d<0)return; str[d-1]=n%2+'0'; toBinary(n>>1,str,d-1); } int toDecimal(char* str,int d){ if(d<=1)return str[0]-'0'; return ((str[0]-'0')<<(d-1))+toDecimal(str+1,d-1); } char* encrypt(char* str){ int i,j; int len = (strlen(str))*8; char* str2=new char[len+1]; for(i=0;i<(strlen(str))*8;i++){ str2[i]='0'; } for(i=0;i<strlen(str);i++){ int n = (int)str[i]; toBinary(n,str2+i*8,8); } str2[len]='\0'; char tstr[5]; memcpy(tstr,str2,4); tstr[4]='\0'; strcpy(str2,str2+4); strcpy(str2+len-4,tstr); char* encryptedStr=new char[len/4+1]; char map[]="0123456789ABCDEF"; for(i=0;i<len/4;i++){ encryptedStr[i]=map[toDecimal(str2+4*i,4)]; } encryptedStr[i]='\0'; return encryptedStr; } int main(void){ char str[256]; gets(str); char* encrypterStr = encrypt(str); cout<<encrypterStr; }
There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi
You're still off by one. Here's how to fix it.
Code:void toBinary(int n, char* str, int d) { if (d == 0) return; str[d - 1] = n % 2 + '0'; toBinary(n >> 1, str, d - 1); }