473,800 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer to characters

I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx


Nov 13 '05 #1
38 6944


Alan wrote:

I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"


You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );

Also: When posting to alt.comp.lang.l earn.c-c++ always
indicate which language you need a solution for. There
we discuss C and C++. The C++ solution would be completely
different.

--
Karl Heinz Buchegger
kb******@gascad .at
Nov 13 '05 #2
Alan wrote:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx


char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);
Rick

Nov 13 '05 #3

"Karl Heinz Buchegger" <kb******@gasca d.at> ???
news:3F******** *******@gascad. at ???...

Alan wrote:

I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );


Is there any function same as sprintf() but write to file?
because I can't find a function called "fsprintf() "
thx

Also: When posting to alt.comp.lang.l earn.c-c++ always
indicate which language you need a solution for. There
we discuss C and C++. The C++ solution would be completely
different.


sorry, next time I will do so
Nov 13 '05 #4


Alan wrote:

"Karl Heinz Buchegger" <kb******@gasca d.at> ???
news:3F******** *******@gascad. at ???...

Alan wrote:

I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"


You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );


Is there any function same as sprintf() but write to file?
because I can't find a function called "fsprintf() "
thx


What for?
Either fprintf() does what you need
or you first print to a character buffer and then
write that character buffer to the file.

In any case: there is no need for fsprintf

--
Karl Heinz Buchegger
kb******@gascad .at
Nov 13 '05 #5
On Wed, 22 Oct 2003 19:12:21 +1000, Rick <rrquick@nosp am-com> wrote:
Alan wrote:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx


char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);


What is the plan if the three-digit integer is negative?

Best wishes,

Bob
Nov 13 '05 #6
Hi,
"Rick" <rrquick@nosp am-com> wrote in message
news:3f******** @clarion.carno. net.au...
Alan wrote:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx


char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);


this fails the OPs spec.

Why not use the standard library for things it was
made for?

Regards,
Stefan.
Nov 13 '05 #7

"Stefan Höhne" <do************ ********@127.0. 0.1> ¦b¶l¥ó
news:bn******** **@news1.wdf.sa p-ag.de ¤¤¼¶¼g...
Hi,

"Rick" <rrquick@nosp am-com> wrote in message
news:3f******** @clarion.carno. net.au...
Alan wrote:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx
char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);


this fails the OPs spec.


I don't understand, in what situation will it fail ?

Why not use the standard library for things it was
made for?


what is the standard library and how to do that ?
thx

Nov 13 '05 #8
Hi,

"Alan" <al************ *@hotmail.com> wrote in message
news:bn******** **@news.hgc.com .hk...

"Stefan Höhne" <do************ ********@127.0. 0.1> ¦b¶l¥ó
news:bn******** **@news1.wdf.sa p-ag.de ¤¤¼¶¼g...
Hi,

"Rick" <rrquick@nosp am-com> wrote in message
news:3f******** @clarion.carno. net.au...
Alan wrote:

> I want to change a 3 digits integer to characters, how can i do that? >
> the 3 digits integer maybe 123, 23 or 3
> I want to change the integer to "123", " 23" or " 3"
>
> thx

char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);


this fails the OPs spec.


I don't understand, in what situation will it fail ?


for val=3. This solution would compute "003", the OP
(=you) explicitely wanted " 3". Of course this is easily
adopted, but needs extra logic.
Why not use the standard library for things it was
made for?


what is the standard library and how to do that ?
thx


The standard library consists of all those basic
functionality you use everyday: malloc(), printf(),
rand(), sin(), is_alpha(), ...

In this case, sprintf() would be the tool to use, as
Karl suggested.

HTH,
Stefan.
Nov 13 '05 #9
Stefan Höhne wrote:
....
The standard library consists of all those basic
functionality you use everyday: malloc(), printf(),
rand(), sin(), is_alpha(), ...


I never heard of or used is_alpha().
Well, I never used isalpha as well. :-)

Jirka

Nov 13 '05 #10

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

Similar topics

9
38665
by: Lord Merlin | last post by:
Sorry for the dumb question, but what is the function in ASP to see if an object / string is an integer? I want to check the contents of a form, and if it's an integer, i.e a number, do something, else if it's a word do something else, and I'm not sure howto? --
5
2928
by: sathyashrayan | last post by:
Group, I have some doubts in the following program. ------------------program--------------------- /* ** Make an ascii binary string into an integer. */ #include <string.h> unsigned int bstr_i(char *cptr)
38
2179
by: Maarten | last post by:
hi all, how can i retrieve the type if a value inserted in an inputbox. what i want to do is make shure people only insert an integer type, if they don't they get an error message. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
12
8466
by: obdict | last post by:
Hello, I have a simple C question regarding scanf() /* What happens if user inputs a char when he/she is supposed * to input an integer? */ #include<stdio.h> int main(void){ int n;
17
3484
by: Yogi_Bear_79 | last post by:
I have the following code: sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0; It only partially works. If the user types a character other than 0-9 to start the string it fails. However as long as the first character is an integer it will allow letters in the following places for Example:
8
6875
by: jobo | last post by:
Hello, If I'm given a string that always starts with the word "free" followed by an integer ("free xx"). How do I parse through the characters and get to the integer value? Thanks.
13
1860
by: Mantorok | last post by:
Hi Is it possible to convert 2 char values to an integer and be able to reverse it? I'm sure there must be some algorithm to achive this. Any ideas? Thanks Kev
11
2384
by: panther | last post by:
Hi, I need to read part of an integer by another variable. As example, if user enter 761120921, then, i = 761120921 j = 76 k = 112 l = 0921 as that. can you explain how do i acheive this?
130
6825
by: euler70 | last post by:
char and unsigned char have specific purposes: char is useful for representing characters of the basic execution character set and unsigned char is useful for representing the values of individual bytes. The remainder of the standard integer types are general purpose. Their only requirement is to satisfy a minimum range of values, and also int "has the natural size suggested by the architecture of the execution environment". What are the...
0
9694
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
9553
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
10509
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
10256
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
5477
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
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4152
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
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2953
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.