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 */
-
}
3 3445
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.
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.
// 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);
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: jeff |
last post by:
how do you convert form byte to Int32 while retaining the binary
value of the byte array
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |