473,653 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how many ways to convert a integer to a string

as many as possible.
int i = 333;
char* s;
how to convert i to s;

Mar 9 '06 #1
12 2703

ap***********@g mail.com wrote:
as many as possible.
int i = 333;
char* s;
how to convert i to s;


Your question is not clear enough (and should have been repreated in
the body of the post as well). Do you want to use standard functions
(see `sprintf`), or are you looking for different /algorithmic/ ways?

Mar 9 '06 #2
ap***********@g mail.com wrote:

as many as possible.
int i = 333;
char* s;
how to convert i to s;


#include <stdlib.h>
#include <limits.h>
#include <stdio.h>

s = malloc((size_t) ((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3);
if (s != NULL) {
sprintf(s, "%d", i);
}

--
pete
Mar 9 '06 #3

ap***********@g mail.com 写道:
as many as possible.
int i = 333;
char* s;
how to convert i to s;


Another way is to get every digit from the integer and then convert the
difit to it's corresponding char. Just continue to divide the integer
by 10 until the result is 0. The remainder of each division is the
leftmost digit of the integer.

Mar 9 '06 #4
ap***********@g mail.com wrote:

as many as possible.
int i = 333;
char* s;
how to convert i to s;


You could write some code, or you could use the standard library
that comes with most C systems (all hosted C systems). It's up to
you. If you write the code you get to define 'convert'. One
possibility follows:

void convert(int i) {

if (i < 0) putchar('-');
else i = -i;
while (i++ < 0) putchar('1');
putchar('\n');
}

Note that I took advantage of the ability to define. Some would
consider this a base 1 representation of i. By using a file I
avoided the necessity of providing a possibly very large string
buffer.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Mar 9 '06 #5

<ap***********@ gmail.com> wrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
as many as possible.
int i = 333;
char* s;
how to convert i to s;

I can come up with as many different ways as you would like. Some are not
very efficient, some are.

For example, I can convert it to a string in base 2, base 3, ... base 12,
base 60, whatever.
I can just use sprintf(), or keep dividing by 10 (for base 10), or take the
log and do some fancy, stupid manipulation and finally get a string
representation.

You will have to be more specific about what you want. Or what your
professor wants.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Mar 9 '06 #6
ap***********@g mail.com writes:
as many as possible.
int i = 333;
char* s;
how to convert i to s;


Here's another one (untested, but should work great).

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int i = 333
int size;
int xcode;
char *s;
const char *format = "%d";

/* Get the size we need. */
size = snprintf(NULL, 0, format, i);
s = malloc(size);
if (s == NULL) {
fputs("Couldn't allocate space for the string. Fancy that!\n",stderr );
xcode = EXIT_FAILURE;
} else {
snprintf(s, size, format, i);
printf("Here you go: %s\n", s);
xcode = EXIT_SUCCESS;
}

return xcode;
}
Mar 9 '06 #7
CBFalconer wrote:

void convert(int i) {

if (i < 0) putchar('-');
else i = -i;
while (i++ < 0) putchar('1');
putchar('\n');
}


Tsk tsk, UB if i == INT_MIN and INT_MIN < -INT_MAX.

Mar 9 '06 #8
Old Wolf wrote:
Tsk tsk, UB if i == INT_MIN and INT_MIN < -INT_MAX.


Tsk tsk, Old Wolf is reading-impaired

Mar 9 '06 #9
Old Wolf wrote:
CBFalconer wrote:

void convert(int i) {

if (i < 0) putchar('-');
else i = -i;
while (i++ < 0) putchar('1');
putchar('\n');
}


Tsk tsk, UB if i == INT_MIN and INT_MIN < -INT_MAX.


Look again. That can't happen, which is why I wrote it as I did.
It is entirely possible that -INT_MIN is larger than INT_MAX, but
not the reverse.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Mar 10 '06 #10

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

Similar topics

5
6163
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
3
10269
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
8
4527
by: Carlos | last post by:
I have a strinf with comma separated field. Is is possible to convert it to a Structure ? Thanks
6
43636
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
5
37284
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
5
8032
by: Joergen Bech | last post by:
Basically, I want to convert hex values in the range "00000000" to "FFFFFFFF" to a signed, 32-bit Integer value. In VB6, I could just write lngValue = Val(hexstring$). In VB.Net, I seem to be forced to do something like ---snip--- Private Function HexToInteger(ByVal hexValue As String) As Integer
14
1463
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the integer that was entered. So, if 353 was entered, the output should be 11.
20
3431
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ----> 0001 2 ----> 0002
7
6254
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
0
8283
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
8704
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8470
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
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
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
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2707
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

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.