Problem 1003 - wrong conversion via (G4String)
Summary: wrong conversion via (G4String)
Status: CLOSED INVALID
Alias: None
Product: Geant4
Classification: Unclassified
Component: global (show other problems)
Version: 9.1
Hardware: All All
: P4 minor
Assignee: Gabriele Cosmo
URL:
Depends on:
Blocks:
 
Reported: 2008-02-26 14:47 CET by Robert Freudenberg
Modified: 2008-02-27 08:40 CET (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this problem.
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 :-)