Hi folks,
I have recently created a converter that converts text into ASCII and then into radians. This part works fine.
Now when I try to re-convert radians into text again there seems to be a problem. This is my radians to text programme:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#define pi 3.1415926535897932384626433832795
using namespace std;
int main()
{
int x = 0;
double radiant;
int numeric;
cout <<"Please enter radiant (min. 6 decimal places): ";
cin >> radiant;
cout << "\n";
cout << "The ASCII for this radiant is: " << (radiant/pi*180) << endl;
cout << "\n";
cout << "The text value of " << (radiant/pi*180) << " is: " << (char) (radiant/pi*180) << endl;
cout << "\n";
return 0;
}
Radians to ASCII works fine, but when converting ASCII back into text there is something wrong I don't understand. The letters from a to c (ASCII 97 to 99), k to q (107 to 113), and y to z (121 to 122) are converted as they shoud be. But the remaining letters are not. The programme says that 1.7627825 (radiant) equals 101 but does not convert it back to the letter "e". Instead it says "d". And this is what I don't understand. Just have a try and see for yourself.
Has someone out there a solution or an explanation. I would realy appreciate your help. Many thanks in advance.
What I actually want to do here is to convert text into radians and than back into text again. And I use ASCII as an intermediate step.
I need radians for a further/different application for my research I am doing. I hope this makes it a bit clearer.
However, if someone knows a better/much easier way to do that I am open for any suggestions. Thanks.
The problem is that you're depending on a level of precision that floating point numbers just don't have. The type of conversion (ASCII->radians->ASCII) you're asking for is either impossible or too cumbersome to implement correctly. Do you really need to use integer-floating point conversion? Can't you find a different way to convert a letter to another? ROT13 is very popular.