Solved

Converting to a int and back

Posted on 2002-03-05
8
200 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
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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now