Any in-built C++ functions for base conversions??

Hey guys,
I wrote a program that used recursion to convert between the number bases some time ago. I am currently working on some pointer values (for class objects) in hex and would like to convert these to integers for use in an array. Instead of having to include a header file for the base-conversion program I have, I am just wondering if the C++ library has in-built base-conversion functions I can readily use; I couldn't find any when I searched through it!
The I/O stream library has decimal, octal and hexadecimal output. It's not difficult to write a general base conversion once you decide what characters are going to be used in the version.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
	std::string s1( "A" );
	std::string s2( "1010" );

	std::cout << s1 << " ===> " << std::stoi( s1, 0, 16 ) << std::endl;
	std::cout << s2 << " ===> " << std::stoi( s2, 0, 2 ) << std::endl;
}
I know kbw; but what I'm asking is if in-built, system-defined functions exist that perform base conversions. Like I wrote earlier, I already did write a recursive program to do just that; but, in my pursuit of minimalism in coding, I just would like to exploit the C++ library's provisions if such base-conversion functions are available. I'm guessing there aren't.
Last edited on
Hi vlad,
I didn't see your post before my last post! Is that 'stoi' an in-built C++ function? I'm guessing it stands for 'string-to-integer', right? Are there any you know of for conversions between the number bases??
Last edited on
It is a standard C++ function declared in header <string>
I'm guessing that stoi outputs the decimal equivalent representation? Correct me if this is wrong please, I may need it myself in future.
I'm guessing i = integer, and integer != decimal/floating point.

So, from what I'm getting, no. It converts it into an integer.
> I'm guessing that stoi outputs the decimal equivalent representation?

Do not use std::stoi() to convert a string holding an address to an integral type. There is no certainty that int would be capable of holding the numeric value of an address.

In practice, std::stoull() (if it is available) should result in conversion with no loss of information.
The pedantically correct integral type to use for this is std::uintptr_t

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdint>
#include <string>
#include <sstream>
#include <iostream>

template < typename T > std::uintptr_t ptr_to_number( T* p )
{ return reinterpret_cast<std::uintptr_t>(p) ; }

std::uintptr_t hex_ptr_str_to_number( const std::string& hex_address_str )
{
    if( sizeof(unsigned long long) >= sizeof(std::uintptr_t) ) 
        return std::uintptr_t( std::stoull( hex_address_str, 0, 16 ) ) ;

    std::istringstream stm(hex_address_str) ;
    std::uintptr_t n = 0 ;
    if( stm >> std::hex >> n >> std::ws && stm.eof() ) return n ;
    else /* error */ return reinterpret_cast<std::uintptr_t>(nullptr) ;
}

int main()
{
    const int i = 0 ;
    std::cout << ptr_to_number(&i) << '\n' ;

    const std::string address = "0x12abcdef" ;
    std::cout << hex_ptr_str_to_number(address) << '\n' ;
}


I am currently working on some pointer values (for class objects) in hex

A quick check.

How are you storing your pointer values?

As strings? (or char* / char arrays)

Or as integer/pointer values (e.g. int, double*)

Andy
Last edited on
@andywestken,
I am not entering the pointer values as data; the system generated them(in hex) as the addresses of the class objects I'm working on, in response to the manipulation, using pointers, of a particular data member across all the objects. To ascertain the correctness of the data processing, I printed a list of the addresses of the objects - the sequence I obtained indicated that the desired computation was achieved.

However, I need to find a way of converting these addresses into integers to be stored in an array through the use of an in-built, base-conversion function, if available. At least, that was my initial motive! But now, I'm just thinking probably I should just store the addresses as string constants and access the array later as such. Moreso now that no one seems to be able to come up with such relevant in-built function! Currently, I have declared that array as of type integer.


@JLBorges, Three questions for you:

#1. Am I right to infer from your post that there is no in-built function to do what I want; I just have to write a conversion function myself?
#2. What is that stoull() function you invoked in your code?
#3. I see unsigned long long in your code; does that duplication serve to augment the maximum size of the integer allowed? In other words, is it correct to deduce that unsigned long long can store a much larger integer than unsigned long(which I'm more used to)?

> #1. #2.

std::stoull() is a standard function that would do what you want.
http://en.cppreference.com/w/cpp/string/basic_string/stoul

However, you may find that std::stoull() and friends are missing from your library. Some implementations (for instance the GNU libstdc++) have not yet implemented this function. In which case, you have to write a function yourself as a temporary work around.


> #3

The types long long and unsigned long long were added in C++11. These are expected to be at least 64 bits wide, and on almost all implementations have a wider range than long / unsigned long.
See the table at the end of this page: http://en.cppreference.com/w/cpp/language/types
the system generated them(in hex)

When you say "the system" "generated them", do you mean the you have called an API function and it has returned a string containing an address (or addresses) in hexadecimal format?

If you are starting out with a string in hexadecimal format and want a string in decimal format, you could use stoull in conjunction with to_string, which formats integers as decimal strings.
http://www.cplusplus.com/reference/string/to_string/

Andy


Last edited on
Topic archived. No new replies allowed.