/ Forside / Teknologi / Udvikling / C/C++ / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
C/C++
#NavnPoint
BertelBra.. 2425
pmbruun 695
Master_of.. 501
jdjespers.. 500
kyllekylle 500
Bech_bb 500
scootergr.. 300
gibson 300
molokyle 287
10  strarup 270
Konvertering af int til string
Fra : "Public


Dato : 25-07-01 19:12

Hvordan konvertere jeg en integer til en string? (I c++ med borland c++ builder 5)



---
This message did not originate from the Sender address above.
It was posted with the use of anonymizing software at
http://www.xganon.com
---



 
 
SuneF (25-07-2001)
Kommentar
Fra : SuneF


Dato : 25-07-01 20:22

> Hvordan konvertere jeg en integer til en string? (I c++ med borland c++ builder 5)

Du kan bruge fx. sprintf:

#include <stdio.h>

void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;

/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n", s );
j += sprintf( buffer + j, "\tCharacter: %c\n", c );
j += sprintf( buffer + j, "\tInteger: %d\n", i );
j += sprintf( buffer + j, "\tReal: %f\n", fp );

printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}

Output




Output:
String: computer
Character: l
Integer: 35
Real: 1.732053

character count = 71




Igor V. Rafienko (26-07-2001)
Kommentar
Fra : Igor V. Rafienko


Dato : 26-07-01 15:10

[ remailer@anon.xg.nu ]


> Hvordan konvertere jeg en integer til en string? (I c++ med borland
> c++ builder 5)


[quote Dietmar Kuehl, <news:alt.comp.lang.learn.c-c++>]

This question is asked - and answered! - quite frequently. Here is one
of the more interesting answers (requires that you are using C++ and
that you have not the bad compiler):

#include <sstream>

template <typename To, typename From>
To stream_cast(From const& from) {
std::stringstream stream;
stream << from;
To to;
stream >> to;
return to;
}

It is used like any other of the new-style casts:

#include <iostream>
#include <string>
int main(int ac, char* av[]) {
std::string str = stream_cast<std::string>(ac);
std::cout << str << "\n";
}

[/quote]





ivr
--
I'm normally not caring about writing memory management stuff in C++:
This is done by the classes I'm using.
   -- Dietmar Kuehl, <news:comp.lang.c++.moderated>

Susanne Gade (29-07-2001)
Kommentar
Fra : Susanne Gade


Dato : 29-07-01 23:12


"Public <Anonymous_Account>" <remailer@anon.xg.nu> skrev i en meddelelse
news:f1c8543389bb2499f88c106ee3430687@anon.xg.nu...
> Hvordan konvertere jeg en integer til en string? (I c++ med borland c++
builder 5)

Med funktionen IntToStr()
giv Funktionen din integer som parameter
Eks.

int value = 5;
String strvalue = IntToStr(value);


Med venlig hilsen
Michael Schwartz



Søg
Reklame
Statistik
Spørgsmål : 177559
Tips : 31968
Nyheder : 719565
Indlæg : 6408938
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste