473,889 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert an integer to ASCII character ?

What is the most easiest way to convert an integer value to ASCII
character format ?
I tried with sprintf(). It works.
Is there any other way to do that ?

Objective::
I like to convert an integer value of 3 and write into a string buffer.

What I did:
.....
.....
char myStr[];
int myInt = 3;
sprintf(myStr, %d,myInt);
.....
.....

Please comment.

Feb 19 '06
16 117293
"Martin Jørgensen" <un*********@sp am.jay.net> wrote in message
news:pk******** ****@news.tdc.d k...
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

shichongdong wrote:
int i = 3;
char c = i + '0';

???

int i = 300;
char c = i + '0' ; /* nope. not an ascii character */


It works for single digits, right?


Assuming ASCII it does.
Feb 20 '06 #11
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

stathis gotsis wrote:
"Martin Jørgensen" <un*********@sp am.jay.net> wrote in message
news:pk******** ****@news.tdc.d k...
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

shichongdong wrote:

int i = 3;
char c = i + '0';

???

int i = 300;
char c = i + '0' ; /* nope. not an ascii character */

It works for single digits, right?


Assuming ASCII it does.


Assuming any conforming C implementation, it does. The C standard guarantees it.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFD+QbaagV FX4UWr64RAkS4AJ 9H8kT8tck4HFxxh C2f+xmDPRRu5QCg nbor
SX0i8pvlRNTifgv wU0h9Od0=
=YdQ6
-----END PGP SIGNATURE-----
Feb 20 '06 #12
Lew Pitcher <lp******@sympa tico.ca> writes:
stathis gotsis wrote:
"Martin Jxrgensen" <un*********@sp am.jay.net> wrote in message
news:pk******** ****@news.tdc.d k...
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

shichongdong wrote:

> int i = 3;
> char c = i + '0';

???

int i = 300;
char c = i + '0' ; /* nope. not an ascii character */
It works for single digits, right?


Assuming ASCII it does.


Assuming any conforming C implementation, it does. The C
standard guarantees it.


The C standard guarantees that decimal digits are sequential and
in the proper order. The C standard doesn't guarantee that the
execution character set is ASCII. The OP asked to convert an
integer value to *ASCII* character format specifically.

Here's a portable way to get a single ASCII digit: 48 + num.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Feb 20 '06 #13
stathis gotsis wrote:
"Martin Jørgensen" <un*********@sp am.jay.net> wrote in message
news:pk******** ****@news.tdc.d k...
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

shichongdong wrote:

int i = 3;
char c = i + '0';

???

int i = 300;
char c = i + '0' ; /* nope. not an ascii character */

It works for single digits, right?


Assuming ASCII it does.


Assuming an implementation that conforms to the C standard it does,
whether it is ASCII or not. It's one of the few things the C standard
guarantees about the execution character set.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
Feb 20 '06 #14
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:2n******** ****@news.flash-gordon.me.uk...
stathis gotsis wrote:
"Martin Jørgensen" <un*********@sp am.jay.net> wrote in message
news:pk******** ****@news.tdc.d k...
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

shichongdong wrote:

> int i = 3;
> char c = i + '0';

???

int i = 300;
char c = i + '0' ; /* nope. not an ascii character */
It works for single digits, right?


Assuming ASCII it does.


Assuming an implementation that conforms to the C standard it does,
whether it is ASCII or not. It's one of the few things the C standard
guarantees about the execution character set.


I was not aware of that, thanks for the correction.
Feb 20 '06 #15
Flash Gordon wrote:
stathis gotsis wrote:
"Martin Jørgensen" <un*********@sp am.jay.net> wrote in message
news:pk******** ****@news.tdc.d k...
Lew Pitcher wrote:
shichongdong wrote:

> int i = 3;
> char c = i + '0';

int i = 300;
char c = i + '0' ; /* nope. not an ascii character */

It works for single digits, right?


Assuming ASCII it does.


Assuming an implementation that conforms to the C standard it does,
whether it is ASCII or not.


Check Ben's point elsewhere in the thread. The OP defined "works" as
producing ASCII. While the code in question produces the corresponding
digit character in the execution set, it only produces the correct ASCII
character if the execution set is ASCII.

--
Thad
Feb 20 '06 #16
On Sun, 19 Feb 2006 01:33:41 +0100, serrand <xa************ @free.fr>
wrote:
serrand wrote:
#define DIGILEN log10 (MAX_INT) +2

char buf[DIGILEN];
sprintf(buf, "%d", int_var);

oops... sorry

#define DIGILEN (int)(log10 (MAX_INT) +3)
In C89 an array bound must be a constant expression, and no function
call, even to the standard library, qualifies. In C99 this is still
true for an object with static duration, but if your code snippet is
entirely within a function (not shown) and thus is automatic, this is
legal, though rather inefficient. A C89-legal and (probably) much more
efficient method is to approximate the digits needed for the maximum
value that could be represented in the object size:
sizeof(int)*CHA R_BIT * 10/3 + slop_as_needed
Your way seems to be the simpliest...

sprintf is doing the same job as printf : wheras printf outputs in stdin
stdout. Frequently stdin stdout and stderr are all the/an interactive
terminal or console or window or whatever, but they need not be.
sprintf outputs in its first argument, which have to be an allocated string


- David.Thompson1 at worldnet.att.ne t
Mar 3 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6120
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something like this: $buf = array(0x41, 0x42, 0x43); Anyone know how? I haven't been able to find a way.
7
41388
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the nmea checksum for gps data. e.g. everything between $ and * needs to be xor: $GPGSV,3,1,10,06,79,187,39,30,59,098,40,25,51,287,00,05,25,103,44* to get the checksum.
11
17430
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 internal string StringToAscii(string S) { byte strArray = Encoding.UTF7.GetBytes(S); string NewString = Encoding.UTF7.GetString(strArray);
5
37361
by: Mika M | last post by:
Hi! I've made little code to convert string into hex string... Public ReadOnly Property ToHexString(ByVal text As String) As String Get Dim arrBytes As Integer() = CharsToBytes(text) Dim sb As StringBuilder = New StringBuilder For i As Integer = 0 To arrBytes.Length - 1
3
32111
by: Director - Minvent | last post by:
Hi, I am reading from a serial port from a device which sends over an ascii character. It uses the full extended 256 character set and therefore has non-printing characters too. So what i want to do is convert the input to the associated decimal. I.E ASCII Char 'NUL' will become 0 ASCII Char 'SOH' will become 1
8
14355
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does ifstream. Something I found shows that, I need to have a proper codecvt to set it. I need more information, maybe a small piece of code sample. Thank you!
232
13434
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
4
25083
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. Anybody please help in c language. Thanks in Advance.
19
5357
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9805
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11187
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10887
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5825
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6021
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.