TString::IsBin(), TString::IsOct(), TString::IsDec()

Hi there,

Here are three new methods to test if a TString is in a binary, octal or decimal format.
All are based on the TString::IsHex() method.
Could you please add them to the trunk (diff files below are given against root 5.32.00)?

TString.diff.tgz (536 Bytes)


thanks I’ve applied your patch to the trunk.

Cheers, Fons.

Thanks Fons :wink:

Here are some extensions:

  • a set of itoa functions acting as enhanced sprintfs i.e. converting an integer to a string with respect to the base specified (2-36). This is an adaptation of versions 0.4 of jb.man.ac.uk/~slowe/cpp/itoa.html.

Usage /// - Conversion to binary format (not possible with sprintf) /// (the statements below produce the same output, namely "1111") cout << TString::Itoa(15,2) << endl ; cout << TString::Itoa(0xF,2) << endl ; /// 0x prefix to handle hex cout << TString::Itoa(017,2) << endl ; /// 0 prefix to handle oct /// - Conversion to any bases between 2 and 36 (not possible with sprintf) cout << TString::Itoa(1000,36) << endl ;

  • a function to convert a string from one base to another (using the above itoa functions). Supported bases are 2-36. The input string must be unsigned. For the time being, only 32 bit data can be converted (the 64 bit upgrade is commented inside the code). The input string format is checked through the new function TString::IsInBaseN(Int_t base).

Usage cout << TString::BaseConvert ( "15",10, 2) << endl ; cout << TString::BaseConvert ( "F",16, 2) << endl ; cout << TString::BaseConvert ( "17", 8, 2) << endl ; cout << TString::BaseConvert ("1111", 2, 16) << endl ;

Everything is attached below as diff files against the trunk.
Could you please add these extensions to the trunk?

TString.diff.tgz (2.2 KB)

Hi again,

A small fix/enhancement for TString::BaseConvert(): hex prefix (0x), leading zeros and signs (+/-) are now taken into account.
The archive below replaces the previous one.

TString.diff.tgz (2.4 KB)


your code is now in the trunk. Thanks.

Cheers, Fons.


Why does Itoa reserve 35 bytes? From the code:

To my understanding, the worst case would 32 bytes, if one converts a very large 32-bit number to a TString in binary representation.