473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Converting from and INT back to ASCII character

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
12 70502
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
> 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
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
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

"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
> 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
"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
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
> > 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
"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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Peter Wilkinson | last post by:
Hello tlistmembers, I am using the encoding function to convert unicode to ascii. At one point this code was working just fine, however, now it has broken. I am reading a text file that has is...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
8
by: Dan | last post by:
In C#, how would you loop through each character in a string and convert them to their ascii values?
7
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the...
2
by: Gidi | last post by:
Hi, I'm writing a C# win application program, and i need to transfer my hebrew letters from unicode to ascii, now if i use the ascii encoding it writes me ??? instead of the hebrew letter i've...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
6
by: davetelling | last post by:
I am a total newbie, trying to slog through the Visual C# Express application. I need to be able to convert a single ASCII character (can be anything from 0 to 255) to an int for use in other...
9
by: Michael Goerz | last post by:
Hi, I am writing unicode stings into a special text file that requires to have non-ascii characters as as octal-escaped UTF-8 codes. For example, the letter "Í" (latin capital I with acute,...
107
by: bmshivaraj | last post by:
Hi, Could any one tell me how to convert a unsigned long value into string (char *) ? In C++ there is a function _ultoa so wanted a similar one in C . Regards, Shivaraj
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.