Problem 1003

Summary: wrong conversion via (G4String)
Product: Geant4 Reporter: Robert Freudenberg <robert.freudenberg>
Component: globalAssignee: Gabriele Cosmo <Gabriele.Cosmo>
Status: CLOSED INVALID    
Severity: minor    
Priority: P4    
Version: 9.1   
Hardware: All   
OS: All   

Description Robert Freudenberg 2008-02-26 14:47:11 CET
Hello,

my problem is very trivial and easy to handle, but it can cause some trouble.
When I try to convert a G4int to a G4String, everything works fine - except with the number 47. The output of the commands:

>> G4int i = 47;
>> G4cout << "Here is the result of (G4String)(i): " << (G4String)(i) << G4endl;

looks

>> Here is the result of (G4String)(i): /

As you can see, for Integer 47 I get the ASCII-character "/" which has a decimal code of 47.

My system is Scientific Linux, Kernel 2.6.18, Geant4.9.1.

Best regards,

Robert Freudenberg
Comment 1 Gabriele Cosmo 2008-02-26 15:00:01 CET
Indeed forcing a cast of an integer to a string is NOT supposed to happen, and G4String is
not supposed to convert a number to its corresponding string representation.
To achieve this you may want to use the tools that C++ provides you.
I.e, a simple function like the following would do the job:

G4String ItoS(G4int i)
{
  std::ostringstream os;
  os << i;
  return G4String(os.str());
}
Comment 2 Robert Freudenberg 2008-02-27 08:40:33 CET
Dear Gabriele Cosmo,

I'm sorry for my wrong assumption. I supposed that I had read this way of conversion in one of the examples - but it wasn't so...
Your small function does the job very well :-)