473,320 Members | 1,868 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,320 software developers and data experts.

How to convert long to string in c++

kirubagari
158 100+
Dear Expert,

Kindly need help on conversion of long to string.
Expand|Select|Wrap|Line Numbers
  1. long i = 123444788 to convert to string i.str();
  2.   cout<< i.str();
This is not functioning.Kindly help me
Jun 8 '11 #1
5 17934
Banfa
9,065 Expert Mod 8TB
Long is a POD type not a class so it has no methods.

But you don't need to convert to a string, the cout object knows how to handle longs so you can just

Expand|Select|Wrap|Line Numbers
  1.     cout << i;
  2.  
Jun 8 '11 #2
whodgson
542 512MB
a question to Banfa:
But assuming the OP wants to convert the long to a string - how would you do that?
Jun 9 '11 #3
johny10151981
1,059 1GB
you can follow several ways,
like

1. ltoa
2.
Expand|Select|Wrap|Line Numbers
  1. char temp[20];
  2. long ld;
  3. sprintf(temp,"%ld",ld);
  4.  
Jun 9 '11 #4
Banfa
9,065 Expert Mod 8TB
Yes or you could do it the C++ way using a string stream. An ostringstream acts exactly like the cout object so if you know how to use one you know how to use both.

Expand|Select|Wrap|Line Numbers
  1. #include <sstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     long i = 123444788;
  10.     ostringstream oss;
  11.  
  12.     oss << i.str();
  13.  
  14.     string myString = oss.str();
  15.  
  16.     cout << myString;
  17.  
  18.     return 0;
  19. }
  20.  

In our systems I have started making methods to dump an objects status (mainly for debugging) that takes ostream& as an input. ostream is the base class of cout, cerr, clog, ofstream and ostringstream (see http://www.cplusplus.com/reference/iostream/) and an ostream& can be used exactly as you would use cout. In this way I can send the object dump directly to the screen, directly to a file or into a string for the code to do something else with (transmit over a network for example).

The objects dumping there status do not need to know (on the whole) where the out is going, only that output is required. This is a fine example of the polymorphic nature of C++.
Jun 9 '11 #5
whodgson
542 512MB
Thanks Banfa...I need to become more familiar with this subject.
Jun 10 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chua Wen Ching | last post by:
I have a problem. But on .NET 1.1 My Scenario: Actually I will have a string of hexadecimals read from a xml file. Then from the hexadecimals, i will add 1 value whenever i made any...
5
by: Alan Silver | last post by:
Hello, <dumb question> I am trying to do something similar to the following... string str = "Hello"; char c = (char)str.Substring(2,1); but the compiler complains that it cannot convert...
6
by: MackS | last post by:
Hello everyone I am faced with the following problem. For the first time I've asked myself "might this actually be easier to code in C rather than in python?", and I am not looking at device...
13
by: HNT20 | last post by:
Hello All i am new to python language. i am working on a gnuradio project where it uses python as the primary programming language. i am trying to convert a message, text, or numbers into binary...
7
by: wenmang | last post by:
what is format for sprintf to convert long long integer (64 bits) to string?
3
by: bussiere maillist | last post by:
i've got a very long string and i wanted to convert it in binary like string = """Monty Python, or The Pythons, is the collective name of the creators of Monty Python's Flying Circus, a British...
1
by: TheDude5B | last post by:
Hi, I have a function which returns a string value, but the string value is actually XML, so I want to be able to then convert this string value into an actual XML document. Is this posible?...
7
by: dan w01 | last post by:
Hi, I'm reading in a string of about 420 characters, the string is in binary. How do I either: - convert a string of this length into an integer variable without getting a runtime error 6...
12
by: manishsharma1 | last post by:
Hi, I have one long String like this: 015EnvironmentData1........ I want to read first three character from it and convert them to integer. This will be the length of the Name of the field...
2
by: AndreasL | last post by:
Hi! I have a string passed to a methos via a string&. I would like to convert this to an unsigned long. How do I convert the string iterator to a char*? with all the error checking code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.