• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Converting to a int and back

Hi,
I need help on this. I know this will sound like homework, but it actually isn't. I know this sounds like it's right out of a textbook, but it's not. Here it goes:
For functionality I want to add to a program I'm working on, I want to convert a char to an int, subtract 1 from it's value and make a new char out of it. How would I do this?
0
xebra19
Asked:
xebra19
1 Solution
 
xebra19Author Commented:
unless there's an easier way to accomplish this.
0
 
AxterCommented:
char f[32] = "Hello World";

int x = f[2];
--x; //x=x-1
f[2] = x;
printf(f);
0
 
AxterCommented:
char f[32] = "Hello World";

int x = f[2];
--x; //x=x-1
char NewChar = x;
//NewChar now equals 'k'
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dbaoraCommented:
Hi,
you can use built in function in ANSI C/WIN 95:

// ANSI + WIN 95
convert string to int:
atoi(const char *string);

// ANSI + WIN 95
convert string to float:
atof(const char *string);

// ANSI + WIN 95
convert string to long:
atol(const char *string);

// WIN 95
convert int to string:
_itoa(int i);

// WIN 95
convert long to string:
_ltoa(long l);

regards,
dbaora
0
 
pjknibbsCommented:
Why not just use:

char ch = 'A';

ch--;

You can add and subtract directly from the char--no need for an intermediate integer.
0
 
jonninCommented:
yes. Type char IS an int, values from 0 - 255 or    
-127 to 128.  Just treat it as an int, using an ascii table to do whatever it is you are doing.  
0
 
griesshCommented:
Dear xebra19

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "Axter"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
xebra19Author Commented:
sorry about the neglected question
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now