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
Solved

Converting to a int and back

Posted on 2002-03-05
8
203 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

856 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