Value conversion

Dear C++ society,

I am new at this form and have already a problem. To my person, I have a mechanical engineering background and have little to none knowledge in C++ programming. Therefore I would really appreciate your help.

I have written a program (see below) that translates text into ASCII code (letter by letter into three digit numbers) and writes the result one below the other. What I need now is to take/grab the three digit values one by one and convert them into radiants (rad = value*pi/180). However, I don't know how to do that. I have already read something about pointers but I am just as clever (stupid) as before.

What I want to achieve is:

letter h = 117
in radians 2.0420352 (117*pi/180) with 6-7 decimal places

I would be really thankful if someone out there would/could show me how to do that (a small example would probably help me further). A big thank you in advance.

Cheers

Csaba

Here is my program:

#include <iostream>
#include <math.h>
#include <stdlib.h>

#define pi 3.141592654

//*****************************************************************************

using namespace std;

int main()
{
int x = 0;
char message[11]; // [message length = 11]
int i;
double y;
char deg, *deg_ptr;

y = deg*pi/180;

//*****************************************************************************

cout <<"Please enter message (max.10 characters, no free space): ";
cin >> message;

char *t = message;

cout << "\n";

cout << "The ASCII for this message is: \n" ;

cout << "\n";

//***************** ASCII conversion ******************************************

int sz = strlen(t);

for (i = 0; i < sz; i++)
{
if (t[i] != '\0')
{
int c = __toascii(t[i]);
if (c < 100) {
cout << "0";
cout << c << endl;

}
else
{
cout << c << endl;

}
}
}

//*****************************************************************************


int *ptr;

//ptr = &c;

printf("Radians: %i", y);

cout << "\n";
return 0;
}

Result looks like that:

Please enter message: house

The ASCII for this message is:
104
111
117
115
101
Radians: -942241540
You should use [c0de] your code here [/c0de] tags. Replace 0 with o.

cout << c << endl;
After that line you could put
cout << (double)((c*3.14)/180) << endl;

That should be sufficient enough to get you on you way to a full solution.

@ Zaita, many thanks for you help. It seems to work.

Now what I want to do is to make it work the other way round. In other words, input is the radiant and the output should be the text. Is it more complicated this way round?

Does someone know how to convert ASCII back to text? Is there something similar to (int c = __toascii(t[i]);) but the other way round?
I appreciate any help I can get. Thanks.

Csaba
Unfortunately you've already been brainwashed by silly MS stuff...
And you are mixing C and C++...

In C and C++, a number is a number is a number. The only difference between a char and an int is that the first is assumed to represent a printable ASCII character and the second is not. So they are displayed differently. But they are both just numbers. You can try this to see the difference:
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>
using namespace std;
int main()
  {
  char c = 'a';  // ASCII value 97
  char d = 97;   // ASCII code for 'a'.

  printf( "c = %03d = \'%c\'\n", c, c );
  printf( "d = %03d = '\%c\'\n", d, d );

  return 0;
  }


All the __toascii() routine does is drop all bits other than 0..6: strict ASCII codes are 7-bit entities, not 8 or 9, etc.

Your ASCII conversion could be just:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// (Just in case your compiler doesn't provide the POSIX M_PI constant.)
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif

int main()
  {
  string message;

  cout << "Please enter your message> ";
  getline( cin, message );

  for (const char* t = message.c_str(); *t != '\0'; t++)
    {
    cout << "The letter \'" << *t << "\' = "
         << setw( 3 ) << setfill( '0' ) << (int)*t
         << " in radians (x*pi/180) is "
         << setprecision( 7 ) << (*t *M_PI /180.0)
         << endl;
    }

  return 0;
  }

That's enough for that.


To convert the other way, first do some algebra. Remember, a number is a number is a number.

Hope this helps.
Topic archived. No new replies allowed.