473,507 Members | 6,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimization of Binary Converter

2 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. /********************************************************/
  2. /*                    Binary converter                  */
  3. /*                     By Matt Fowler                   */
  4. /*                email address removed              */
  5. /*  converts text into binary using the division method */
  6. /*                   through ASCII code                 */
  7. /*compiled with the Dev-C++ compiler (www.bloodshed.net)*/
  8. /********************************************************/
  9.  
  10. #include <iostream>
  11. using namespace std;
  12. #include <cstring>
  13. #include <cstdlib>
  14.  
  15. char *entry, letter, choice[2];
  16. int ascii, len, binary[8], total;
  17. void prog();
  18.  
  19. int main()
  20. {
  21.       prog();     
  22.       return 0;
  23. }        
  24.  
  25. void prog()
  26. {
  27.    entry = new char[501]; 
  28.    /* entry should be dynamic, otherwise a new 
  29.       string entry of 501 chars would be created
  30.       each time function is called!  
  31.       Talk about memory hog! */
  32.    cout<<"Enter string to convert (up to 500 chars): ";
  33.    cin.getline(entry, 500);
  34.    len = strlen(entry);  /* get the number of characters in entry. */
  35.    /* this loop is executed for each letter in the string. */
  36.    for(int i = 0; i<len; i++)
  37.    {
  38.       total = 0;
  39.       letter = entry[i]; /* store the first letter */
  40.       ascii = letter;    /* put that letter into an int, so we can 
  41.                             see its ASCII number */ 
  42.       while(ascii>0) /* This while loop converts the ASCII # into binary,
  43.                         stores it backwards into the binary array. */
  44.       {
  45.          /* To get the binary code one must take the decimal number in
  46.             question, take it and divide it by two repeatedly, save
  47.             the remainder (which will become the binary number), save
  48.             the whole number, divide by two, and repeat the whole
  49.             process until 0 is reached.  This if-else statement serves
  50.             this functionality, by getting the remainder of the ascii
  51.             code, storing it in the array and then dividing the int
  52.             ascii by two */
  53.          if((ascii%2)==0)
  54.          {
  55.             binary[total] = 0;
  56.             ascii = ascii/2;
  57.             total++; /* increasing by one each time will yeild the
  58.                         number of numbers in the array. */
  59.          }
  60.          else
  61.          {
  62.             binary[total] = 1;
  63.             ascii = ascii/2;
  64.             total++;
  65.          }
  66.       }
  67.       total--; /* due to data type factors, the program will actually
  68.                   add a 0 at the end of the array that is not supposed
  69.                   to be there, decrementing total will solve this
  70.                   problem, as that 0 will not be displayed. */
  71.       /* this while loop displays the binary code for that letter. */
  72.       while(total>=0)
  73.       {
  74.          cout<<binary[total];
  75.          total--;
  76.       }
  77.    }
  78.    delete[] entry; /* free up the memory used by entry */
  79.    cout<<endl<<"Do again(1 = yes, 2= no)?: ";
  80.    cin.getline(choice,3);
  81.    if(choice[0] == '1')
  82.       prog(); /* program is recursive, it calls itself.  It's kinda
  83.                  like a function loop of sorts. */
  84.    else
  85.       exit(0); /* quits the program */  
  86. }
Feb 18 '08 #1
3 3479
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Feb 18 '08 #2
logaelo
2 New Member
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.
Feb 18 '08 #3
007india
1 New Member
// 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);
}
Jun 5 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
4108
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
93
3547
by: roman ziak | last post by:
I just read couple articles on this group and it keeps amazing me how the portability is used as strong argument for language cleanliness. In my opinion, porting the program (so you just take the...
10
4549
by: MariusI | last post by:
I stumbled over an optimization (or lack of one, to be specific) when viewing IL opcodes generated by the compiler using ms .net 2003. I was testing fast pixel manipulation using Bitmap.LockBits...
1
1793
by: Ed Lai | last post by:
This is an announcement of a bidirectional converter between binary flat file and XML. It is different from other converters because it is free, it is web-based, nothing to download, it...
1
2931
by: girl23 | last post by:
Hi there ]i am new to C programming and totally lost. iam trying to convert decimal to binary. here is what i did please ignore the case h and m. I am trying to get case 'b' to work. i do not...
2
2952
by: Dark Wind | last post by:
Hi, I have been using OPT++ to solve a non linear programming problem. I am totally new to C++, but I looked at an example given on OPT++ website and modified it according to my problem. But I...
2
7023
by: yinka90 | last post by:
Hi. I have a problem with my converter. It works fine. the only problem is that it doesnt convert single digit numbers properly. public class Main { /** * @param args the command line...
5
2562
by: Canned | last post by:
Hi, I'm trying to write a class that can convert ascii to binary and vice versa. I write my class based on this function I've found on internet That works perfectly, but when I try to implement...
1
1623
by: mturner64 | last post by:
Need decimal to binary converter that does #'s like 0.234, etc. Anyone have any links to any? Thanks, Mike
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.