Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting to a int and back

Posted on 2002-03-05
8
Medium Priority
?
207 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

705 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