Optimization of Binary Converter | Newbie | | Join Date: Feb 2008
Posts: 2
| |
Hello all,
Could anyone explain how to optimization this code? In the prosess of optimization what is the factor needed and important to know about it?
Thank you very much for all. -
/********************************************************/
-
/* Binary converter */
-
/* By Matt Fowler */
-
/* email address removed */
-
/* converts text into binary using the division method */
-
/* through ASCII code */
-
/*compiled with the Dev-C++ compiler (www.bloodshed.net)*/
-
/********************************************************/
-
-
#include <iostream>
-
using namespace std;
-
#include <cstring>
-
#include <cstdlib>
-
-
char *entry, letter, choice[2];
-
int ascii, len, binary[8], total;
-
void prog();
-
-
int main()
-
{
-
prog();
-
return 0;
-
}
-
-
void prog()
-
{
-
entry = new char[501];
-
/* entry should be dynamic, otherwise a new
-
string entry of 501 chars would be created
-
each time function is called!
-
Talk about memory hog! */
-
cout<<"Enter string to convert (up to 500 chars): ";
-
cin.getline(entry, 500);
-
len = strlen(entry); /* get the number of characters in entry. */
-
/* this loop is executed for each letter in the string. */
-
for(int i = 0; i<len; i++)
-
{
-
total = 0;
-
letter = entry[i]; /* store the first letter */
-
ascii = letter; /* put that letter into an int, so we can
-
see its ASCII number */
-
while(ascii>0) /* This while loop converts the ASCII # into binary,
-
stores it backwards into the binary array. */
-
{
-
/* To get the binary code one must take the decimal number in
-
question, take it and divide it by two repeatedly, save
-
the remainder (which will become the binary number), save
-
the whole number, divide by two, and repeat the whole
-
process until 0 is reached. This if-else statement serves
-
this functionality, by getting the remainder of the ascii
-
code, storing it in the array and then dividing the int
-
ascii by two */
-
if((ascii%2)==0)
-
{
-
binary[total] = 0;
-
ascii = ascii/2;
-
total++; /* increasing by one each time will yeild the
-
number of numbers in the array. */
-
}
-
else
-
{
-
binary[total] = 1;
-
ascii = ascii/2;
-
total++;
-
}
-
}
-
total--; /* due to data type factors, the program will actually
-
add a 0 at the end of the array that is not supposed
-
to be there, decrementing total will solve this
-
problem, as that 0 will not be displayed. */
-
/* this while loop displays the binary code for that letter. */
-
while(total>=0)
-
{
-
cout<<binary[total];
-
total--;
-
}
-
}
-
delete[] entry; /* free up the memory used by entry */
-
cout<<endl<<"Do again(1 = yes, 2= no)?: ";
-
cin.getline(choice,3);
-
if(choice[0] == '1')
-
prog(); /* program is recursive, it calls itself. It's kinda
-
like a function loop of sorts. */
-
else
-
exit(0); /* quits the program */
-
}
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: Optimization of Binary Converter
The characters in a string are already integers. That is, a char containing A is already in binary. Has to be. It's in the computer. In this case the A is 65 so all you need do is convert that 65 to binay.
You convert the char directly to binary.
(str[i] / n ) % 2 ) will get the bit in column n for the ith character in the string str. Just vary n as a power of 2 from 1 to 128.
| | Newbie | | Join Date: Feb 2008
Posts: 2
| | | re: Optimization of Binary Converter Quote:
Originally Posted by weaknessforcats The characters in a string are already integers. That is, a char containing A is already in binary. Has to be. It's in the computer. In this case the A is 65 so all you need do is convert that 65 to binay.
You convert the char directly to binary.
(str[i] / n ) % 2 ) will get the bit in column n for the ith character in the string str. Just vary n as a power of 2 from 1 to 128. Sir, how to check this code can improve performance and usage memory because i'm beginner to know C and dont know about optimization. Could you explain reference about optimization in website. Because it's important for me.
Thanks all.
| | Newbie | | Join Date: Jun 2009
Posts: 1
| | | re: Optimization of Binary Converter
// Binary converter converts text into binary using the division method through ASCII code
#include <iostream>
using namespace std;
char entry[501], letter;
int ascii, len, binary[8], total, choice;
void prog();
int main()
{
prog();
return 0;
}
void prog()
{
do
{
/* entry should be dynamic, otherwise a new
string entry of 501 chars would be created
each time function is called!
Talk about memory hog! */
cout<<"Enter string to convert (up to 500 chars): ";
cin>>entry;
len = strlen(entry); /* get the number of characters in entry. */
for(int i = 0; i<len; i++)
{
total = 0;
letter = entry[i]; /* store the first letter */
ascii = letter; /* put that letter into an int, so we can
see its ASCII number */
while(ascii>0) /* This while loop converts the ASCII # into binary,
stores it backwards into the binary array. */
{
/* To get the binary code one must take the decimal number in
question, take it and divide it by two repeatedly, save
the remainder (which will become the binary number), save
the whole number, divide by two, and repeat the whole
process until 0 is reached. This if-else statement serves
this functionality, by getting the remainder of the ascii
code, storing it in the array and then dividing the int
ascii by two */
if((ascii%2)==0)
{
binary[total] = 0;
ascii = ascii/2;
total++; /* increasing by one each time will yeild the
number of numbers in the array. */
}
else
{
binary[total] = 1;
ascii = ascii/2;
total++;
}
// cout<<binary[6-total];
}
total--; /* due to data type factors, the program will actually
add a 0 at the end of the array that is not supposed
to be there, decrementing total will solve this
problem, as that 0 will not be displayed. */
/* this while loop displays the binary code for that letter. */
while(total>=0)
{
cout<<binary[total];
total--;
}
} /* this loop is executed for each letter in the string. */
cout<<endl<<"Do again(1 = yes, 2= no)?: ";
cin>>choice;
} while (choice==1);
}
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|