Solved

Converting to a int and back

Posted on 2002-03-05
8
206 Views
Last Modified: 2010-04-01
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
Comment
Question by:xebra19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

by:xebra19
ID: 6843561
unless there's an easier way to accomplish this.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6843619
char f[32] = "Hello World";

int x = f[2];
--x; //x=x-1
f[2] = x;
printf(f);
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 6843622
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!

 
LVL 2

Expert Comment

by:dbaora
ID: 6843652
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
 
LVL 12

Expert Comment

by:pjknibbs
ID: 6843685
Why not just use:

char ch = 'A';

ch--;

You can add and subtract directly from the char--no need for an intermediate integer.
0
 
LVL 2

Expert Comment

by:jonnin
ID: 6847087
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
 
LVL 11

Expert Comment

by:griessh
ID: 6955268
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
 

Author Comment

by:xebra19
ID: 6958659
sorry about the neglected question
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question