Converting integer to string

Dear ROOTers

I have a simple question. Currently, I use TString to convert integers to char, e.g. in the following function:

const char *GetID(Int_t type)
{
   TString name = ""; name += type;
   return name.Data();
}

Is there a better way?

Thank you in advance.
Best regards
Christian

Hi Christian,

Have a look at TString::Form(…), so your routine would look like

const char *GetID(Int_t type) 
{ 
   TString name; name.Form("%d",type); 
   return name.Data(); 
} 

Eddy

You can also just

const char *GetID(Int_t type) 
{
   return Form ("%d", type);
}

If possible, however, don’t use const char* for character strings (you’re just asking for problems…). Use C++ standard string or Root’s TString.

Cheers,
Charles

Hi Christian,

Please note that you can not use the code you shown:const char *GetID(Int_t type) { TString name = ""; name += type; return name.Data(); };. The problem is that the TString object ‘name’ is being deleted at the end of the function (since it is declared on the stack) but you are returning part of it content (name.Data()). Hence when you use the result of GetID you are using already deleted memory which lead to random behavior (which sometimes includes apparent success :slight_smile:).

Instead you should use either TString GetID(int type) {...}; but this is somewhat slow since in the general case it involves the creation and deletion on an intermediary object on the stack. Or void GetID(TString &output, int type) { // Fill the 'output' TString object with the type output.Form("%d",type); }

Cheers,
Philippe.

Of course a good old sprintf does also the job, but we all know that:

char val[20];
sprintf(val,"%d",i);

[quote=“couet”]Of course a good old sprintf does also the job, but we all know that:

char val[20]; sprintf(val,"%d",i); [/quote]

Yes, but

char *val; // or val[3]
sprintf (val, "%d", i);  // i = 100

Does very bad things that may be quite hard to track down. Use Form and TStrings or strings.

Charles

Dear ROOTers

Thank you all for your replies, I am really happy that I have chosen ROOT as my framework almost 10 years ago!

Some remarks:

  • Before asking this question I was googling around but did not find a satisfactory solution.
  • Somehow I missed that Form() is a TString method, my mistake.
  • I prefer to use TString and not string (In contrast to others I am glad, that ROOT did “reinvent” many things.)

I need to call this function in a for-loop about one million times, so it should be fast.
Thank you also for pointing me to my mistake having “name” on the stack.

Best regards
Christian

Hi,

[quote]- Somehow I missed that Form() is a TString method, my mistake.[/quote]For the sake of clarity :slight_smile:. There is 3 related methods all declared in TString.h:1: void TString::Form(const char *fmt, ...); 2: static TString TString::Format(const char *fmt, ...); 3: extern char *Form(const char *fmt, ...); // format in circular buffer(so Form is both a member function of TString and a free standing function.

The fastest of the option is 3. However you must use the result before any other (indirect) call to ::Form is made (essentially this means that you should not pass the result of ::Form as the parameter to any other functions).

Where performance is not critical, 2. is the simpliest to use:tree->Draw(TString::Format("mybranch_%d",index_of_branch);.

  1. is a tradeoff between the 2 other options since it allows to keep the result without incurring the cost of recreating the TString object:TString buffer; for(int i=0; i<len; ++i) { buffer.Form("myvar_%d",i); // use buffer // use buffer again }

Cheers,
Philippe.

Dear Philippe

Thank you for this clarification, I will keep it in mind.

Best regards
Christian

[quote=“cstrato”] . . .
I am glad, that ROOT did “reinvent” many things.)
. . .[/quote]For the record. ROOT did not re-invent “string”. ROOT TString class was available for all platforms 14 years ago (1994) en.wikipedia.org/wiki/Standard_Template_Library

[quote] . . .
The committee’s response was overwhelmingly favorable and led to a request from Koenig for a formal proposal in time for the March 1994 meeting. Despite the tremendous time pressure, Alex and Meng were able to produce a draft proposal that received preliminary approval at that meeting.
. . . [/quote]

See:
doc.trolltech.com/4.4/qstring.html
msdn.microsoft.com/en-us/library/ms174288.aspx
robertnz.net/string.htm#intro

Dear Valeri

I am glad to hear this, which convinces me even more to use the ROOT classes whenever possible.

I made this comment because I was just reading the following post:
root.cern.ch/phpBB2/viewtopic.ph … 881b30723a

Regards
Christian