By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,760 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,760 IT Pros & Developers. It's quick & easy.

Converting from and INT back to ASCII character

P: n/a
Hi all, i have been able to convert an ASCII character to an INT however im
lost as to how to change them back. Cant find anything on the net (though im
probably looking in the wrong places!). Could anyone help me out?

Thanks
Dave
Jul 19 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Hi,

You probably mean something else then the following I guess

char Char = static_cast<char>( 5 );

What is exactly your question?

Regards, Ron AF Greve

"David Williams" <ds********@dsl.pipex.com> wrote in message
news:3f**********************@news.dial.pipex.com. ..
Hi all, i have been able to convert an ASCII character to an INT however im lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!). Could anyone help me out?

Thanks
Dave

Jul 19 '05 #2

P: n/a
> Hi all, i have been able to convert an ASCII character to an INT however
im
lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!). Could anyone help me out?


#include<iostream>
#include<sstream>

int main()
{
std::string s = "123";
int i = 0;

std::istringstream iss(s);
iss >> i;

std::cout << i; // 123

i = 321;
std::ostringstream oss;
oss << i;

std::cout << oss.str(); // "123"
}
Jonathan
Jul 19 '05 #3

P: n/a
Thanks both of you, that did the trick!

Dave

"David Williams" <ds********@dsl.pipex.com> wrote in message
news:3f**********************@news.dial.pipex.com. ..
Hi all, i have been able to convert an ASCII character to an INT however im lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!). Could anyone help me out?

Thanks
Dave

Jul 19 '05 #4

P: n/a
ian
Hello Dave,

to convert integer to ASCII string:
#include <stdio.h>

char szBuf[50];
int i = 123;
sprintf (szBuf, "%i", i);
to convert ASCII string to integer
#include <stdio.h>
#include <stdlib.h>

char szBuf[50] = "123";
int i;
sscanf (szBuf, "%i", &i); // method #1
i = atoi (szBuf); // method #2
to convert an ASCII character to an integer
char cCh = 'A';
int i = static_cast<int>(cCh);
to convert an integer to an ASCII character
int i = 65;
char cCh = static_cast<char>( i); // cCh is set to 'A'
"David Williams" <ds********@dsl.pipex.com> wrote in message
news:3f**********************@news.dial.pipex.com. ..
Hi all, i have been able to convert an ASCII character to an INT however im lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!). Could anyone help me out?

Thanks
Dave

Jul 19 '05 #5

P: n/a

"David Williams" <ds********@dsl.pipex.com> wrote in message
news:3f**********************@news.dial.pipex.com. ..
Hi all, i have been able to convert an ASCII character to an INT however im lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!). Could anyone help me out?

Thanks
Dave


char c = 'A';
int i = 0;

i = c; /* character to integer */
c = char(i); /* integer to character */

This has nothing to do with ASCII or any particular
character set.

-Mike
Jul 19 '05 #6

P: n/a
> Hi all, i have been able to convert an ASCII character to an INT however
im
lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!). Could anyone help me out?


Another proof that an unprecise question can have at least 5 good answers...
Jonathan
Jul 19 '05 #7

P: n/a
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message news:<Q3*********************@weber.videotron.net> ...
Hi all, i have been able to convert an ASCII character to an INT however im
lost as to how to change them back. Cant find anything on the net (though

im
probably looking in the wrong places!). Could anyone help me out?


#include<iostream>
#include<sstream>

int main()
{
std::string s = "123";
int i = 0;

std::istringstream iss(s);
iss >> i;

std::cout << i; // 123

i = 321;
std::ostringstream oss;
oss << i;

std::cout << oss.str(); // "123"


??

Why doesn't that print "321"?
}
Jonathan

Jul 19 '05 #8

P: n/a
My apologies about any misunderstanding over the question, so that you know,
i needed to convert an value such as '72' into the 'H' representation.
Thanks to all of you who replied to my post, the speed and detail of the
posts here is impressive!

Thanks Again

Dave
Jul 19 '05 #9

P: n/a
> > i = 321;
std::ostringstream oss;
oss << i;

std::cout << oss.str(); // "123"


??

Why doesn't that print "321"?


Because it was a typo :)

std::cout << oss.str(); // "321"
Jonathan
Jul 19 '05 #10

P: n/a
"David Williams" <ds********@dsl.pipex.com> wrote in message news:<3f***********************@news.dial.pipex.co m>...
My apologies about any misunderstanding over the question, so that you know,
i needed to convert an value such as '72' into the 'H' representation.
Thanks to all of you who replied to my post, the speed and detail of the
posts here is impressive!

Thanks Again

Dave


Dave...this is trivial.

int an_int = 0x48; // decimal 72
unsigned char a_char = an_int;

cout << a_char; // 'H'

You can do a simple assignment. If your int is greater than hex FF,
all bits beyond FF will be truncated. If you want to make sure this
behavior, you can do the previous assignment as follows:

an_int = 0x451048; // added some nums...but ends with 0x48 (dec 72)
a_char = (an_int & 0x48);

cout << a_char; //is still 'H'...even if you don't use the AND mask
Jul 19 '05 #11

P: n/a
I know its simple, after all its quite obvious im a newbie! Still it would
have been rude not to thank you all for your time, you didnt have to reply,
after all.

Dave

"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
"David Williams" <ds********@dsl.pipex.com> wrote in message

news:<3f***********************@news.dial.pipex.co m>...
My apologies about any misunderstanding over the question, so that you know, i needed to convert an value such as '72' into the 'H' representation.
Thanks to all of you who replied to my post, the speed and detail of the
posts here is impressive!

Thanks Again

Dave


Dave...this is trivial.

int an_int = 0x48; // decimal 72
unsigned char a_char = an_int;

cout << a_char; // 'H'

You can do a simple assignment. If your int is greater than hex FF,
all bits beyond FF will be truncated. If you want to make sure this
behavior, you can do the previous assignment as follows:

an_int = 0x451048; // added some nums...but ends with 0x48 (dec 72)
a_char = (an_int & 0x48);

cout << a_char; //is still 'H'...even if you don't use the AND mask

Jul 19 '05 #12

P: n/a


David Williams wrote:

I know its simple, after all its quite obvious im a newbie! Still it would
have been rude not to thank you all for your time, you didnt have to reply,
after all.


Please don't top post.
Put your reply underneath the text you are replying to.
Thank you.

The key point is, that character in C++ really are nothing else
the small integers. It is only during input and output that they
are handled differently.
While a normal int is output by creating a textual representation
of the number, a char is output by showing a glyph which corresponds
to the number in the char according to some table.

Other then that and the different sizeof, there is no real difference
between char and int. You can even do arithmetik with char. But
beware, char alone does not suggest if it is signed or unsigned. If you
want to nail this down use unsigned char or signed char.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.