Beginners "luck"
-
Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":
array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
{
//converts entered characters into binary n stores in message array
int ascii;
array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
for(int i=0;i<imgsize1;i++)>
{
messag[i] = gcnew array(8);
}
for(int x= 0;x<length;x++)>
{
ascii =(int) arr[x];
int* binary_reverse = new int [9];
int* binary = new int [9];
int y = 0;
while(ascii != 1)
{
if(ascii % 2 == 0) //if ascii is divisible by 2
{
binary_reverse[y] = 0; //then put a zero
}
else if(ascii % 2 == 1) //if it isnt divisible by 2
{
binary_reverse[y] = 1; //then put a 1
}
ascii /= 2;
y++;
}
if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
{
binary_reverse[y] = 1;
y++;
}
if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
{
for(; y < 8; y++) //add until binary_reverse[7] (8th element)
{
binary_reverse[y] = 0;
}
}
for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
{
binary[z] = binary_reverse[7 - z];
if(binary[z]==0)
{
binary[z]=-1;
}
}
for(int z = 0; z < 8; z++)
{
messag[x][z]=binary[z];
}
delete [] binary_reverse; //free the memory created by dynamic mem. allocation
delete [] binary;
}
return messag;
}You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":
array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
{
//converts entered characters into binary n stores in message array
int ascii;
array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
for(int i=0;i<imgsize1;i++)>
{
messag[i] = gcnew array(8);
}
for(int x= 0;x<length;x++)>
{
ascii =(int) arr[x];
int* binary_reverse = new int [9];
int* binary = new int [9];
int y = 0;
while(ascii != 1)
{
if(ascii % 2 == 0) //if ascii is divisible by 2
{
binary_reverse[y] = 0; //then put a zero
}
else if(ascii % 2 == 1) //if it isnt divisible by 2
{
binary_reverse[y] = 1; //then put a 1
}
ascii /= 2;
y++;
}
if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
{
binary_reverse[y] = 1;
y++;
}
if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
{
for(; y < 8; y++) //add until binary_reverse[7] (8th element)
{
binary_reverse[y] = 0;
}
}
for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
{
binary[z] = binary_reverse[7 - z];
if(binary[z]==0)
{
binary[z]=-1;
}
}
for(int z = 0; z < 8; z++)
{
messag[x][z]=binary[z];
}
delete [] binary_reverse; //free the memory created by dynamic mem. allocation
delete [] binary;
}
return messag;
}You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
Yes a bit of a horror, but, yes there is a but, without knowing what was the driver for this question or what the OP was upto, they might have been forced to do it this way, particularly if they are on a programming course etc. I am currently doing a degree with the OU, and it is all based around Java. The question papers etc, sometimes tell you must do some things a certain way and not use other methods (or built in libraries/extensions etc. The last paper I was doing was based around threads, it had the 'main' thread, and 3 other worker threads. We had to use semaphores/countdown latches etc. to ensure the workers were finished before the main thread did something, rather than a simple join.
Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.com -
Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":
array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
{
//converts entered characters into binary n stores in message array
int ascii;
array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
for(int i=0;i<imgsize1;i++)>
{
messag[i] = gcnew array(8);
}
for(int x= 0;x<length;x++)>
{
ascii =(int) arr[x];
int* binary_reverse = new int [9];
int* binary = new int [9];
int y = 0;
while(ascii != 1)
{
if(ascii % 2 == 0) //if ascii is divisible by 2
{
binary_reverse[y] = 0; //then put a zero
}
else if(ascii % 2 == 1) //if it isnt divisible by 2
{
binary_reverse[y] = 1; //then put a 1
}
ascii /= 2;
y++;
}
if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
{
binary_reverse[y] = 1;
y++;
}
if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
{
for(; y < 8; y++) //add until binary_reverse[7] (8th element)
{
binary_reverse[y] = 0;
}
}
for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
{
binary[z] = binary_reverse[7 - z];
if(binary[z]==0)
{
binary[z]=-1;
}
}
for(int z = 0; z < 8; z++)
{
messag[x][z]=binary[z];
}
delete [] binary_reverse; //free the memory created by dynamic mem. allocation
delete [] binary;
}
return messag;
}You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
On a different note from my other post on this, i am a hobbyist programmer and do not do it as my day-to-day line of work. As a result of this i have many many times found myself producing completely bizarre code to do a task, only to find out that there a specific classes/methods available within the .net framework, usually resulting in me giving myself a slap. Its one of those......experience makes a difference.....kind of things i guess.
Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.com -
Yes a bit of a horror, but, yes there is a but, without knowing what was the driver for this question or what the OP was upto, they might have been forced to do it this way, particularly if they are on a programming course etc. I am currently doing a degree with the OU, and it is all based around Java. The question papers etc, sometimes tell you must do some things a certain way and not use other methods (or built in libraries/extensions etc. The last paper I was doing was based around threads, it had the 'main' thread, and 3 other worker threads. We had to use semaphores/countdown latches etc. to ensure the workers were finished before the main thread did something, rather than a simple join.
Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.comI know what you mean, but I think what got me was the way you could see the OP original thought process run through the whole thing: "Good that works." "Damn, forgot the last digit. That fixes it" "Bugger, forgot the zeros if there are any left over...better put them in." "What do you mean its back to front - sod it!" "Oh, yes, I had better include it in the output..." Without at any time wanting to change what the OP already has that is working (sort of)! I do find that a reluctance to look back and change partly broken code does cause a lot of horrors...
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":
array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
{
//converts entered characters into binary n stores in message array
int ascii;
array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
for(int i=0;i<imgsize1;i++)>
{
messag[i] = gcnew array(8);
}
for(int x= 0;x<length;x++)>
{
ascii =(int) arr[x];
int* binary_reverse = new int [9];
int* binary = new int [9];
int y = 0;
while(ascii != 1)
{
if(ascii % 2 == 0) //if ascii is divisible by 2
{
binary_reverse[y] = 0; //then put a zero
}
else if(ascii % 2 == 1) //if it isnt divisible by 2
{
binary_reverse[y] = 1; //then put a 1
}
ascii /= 2;
y++;
}
if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
{
binary_reverse[y] = 1;
y++;
}
if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
{
for(; y < 8; y++) //add until binary_reverse[7] (8th element)
{
binary_reverse[y] = 0;
}
}
for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
{
binary[z] = binary_reverse[7 - z];
if(binary[z]==0)
{
binary[z]=-1;
}
}
for(int z = 0; z < 8; z++)
{
messag[x][z]=binary[z];
}
delete [] binary_reverse; //free the memory created by dynamic mem. allocation
delete [] binary;
}
return messag;
}You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
One of the first things I did wen I learned C# was to translate a bunch of my C routines -- only to find that a lot of them were already built-in.
-
Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":
array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
{
//converts entered characters into binary n stores in message array
int ascii;
array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
for(int i=0;i<imgsize1;i++)>
{
messag[i] = gcnew array(8);
}
for(int x= 0;x<length;x++)>
{
ascii =(int) arr[x];
int* binary_reverse = new int [9];
int* binary = new int [9];
int y = 0;
while(ascii != 1)
{
if(ascii % 2 == 0) //if ascii is divisible by 2
{
binary_reverse[y] = 0; //then put a zero
}
else if(ascii % 2 == 1) //if it isnt divisible by 2
{
binary_reverse[y] = 1; //then put a 1
}
ascii /= 2;
y++;
}
if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
{
binary_reverse[y] = 1;
y++;
}
if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
{
for(; y < 8; y++) //add until binary_reverse[7] (8th element)
{
binary_reverse[y] = 0;
}
}
for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
{
binary[z] = binary_reverse[7 - z];
if(binary[z]==0)
{
binary[z]=-1;
}
}
for(int z = 0; z < 8; z++)
{
messag[x][z]=binary[z];
}
delete [] binary_reverse; //free the memory created by dynamic mem. allocation
delete [] binary;
}
return messag;
}You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
I also loved:
int* binary_reverse = new int [9];
int* binary = new int [9];...
//add until binary_reverse[7] (8th element)
-
Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":
array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
{
//converts entered characters into binary n stores in message array
int ascii;
array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
for(int i=0;i<imgsize1;i++)>
{
messag[i] = gcnew array(8);
}
for(int x= 0;x<length;x++)>
{
ascii =(int) arr[x];
int* binary_reverse = new int [9];
int* binary = new int [9];
int y = 0;
while(ascii != 1)
{
if(ascii % 2 == 0) //if ascii is divisible by 2
{
binary_reverse[y] = 0; //then put a zero
}
else if(ascii % 2 == 1) //if it isnt divisible by 2
{
binary_reverse[y] = 1; //then put a 1
}
ascii /= 2;
y++;
}
if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
{
binary_reverse[y] = 1;
y++;
}
if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
{
for(; y < 8; y++) //add until binary_reverse[7] (8th element)
{
binary_reverse[y] = 0;
}
}
for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
{
binary[z] = binary_reverse[7 - z];
if(binary[z]==0)
{
binary[z]=-1;
}
}
for(int z = 0; z < 8; z++)
{
messag[x][z]=binary[z];
}
delete [] binary_reverse; //free the memory created by dynamic mem. allocation
delete [] binary;
}
return messag;
}You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy