Converting from and INT back to ASCII character

D

David Williams

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
 
M

Moonlit

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
 
J

Jonathan Mcdougall

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::eek:stringstream oss;
oss << i;

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


Jonathan
 
I

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'
 
M

Mike Wahler

David Williams said:
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
 
J

Jonathan Mcdougall

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
 
G

Gavin Deane

Jonathan Mcdougall said:
#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::eek:stringstream oss;
oss << i;

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

??

Why doesn't that print "321"?
 
D

David Williams

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
 
J

J. Campbell

David Williams said:
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
 
D

David Williams

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
 
K

Karl Heinz Buchegger

David said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top