What is the best way to convert unsigned integers to their octal representations and vice versa in C++?

Multi tool use


What is the best way to convert unsigned integers to their octal representations and vice versa in C++?
Currently I'm using while
loops:
while
std::string to_octal(unsigned int num)
{
int place = 1, remainder, octal = 0;
while (num != 0)
{
remainder = num % 8;
decimal /= 8;
octal += remainder * place;
place *= 10;
}
return std::to_string(octal);
}
unsigned int to_num(std::string octal)
{
unsigned int octal_n = std::stoi(octal);
int place = 1, remainder, num = 0;
while (num != 0)
{
remainder = octal_n % 10;
octal_n /= 10;
num += remainder * place;
place *= 8;
}
return num;
}
Which seems inefficient. Is there a better way to do this?
Better way is to keep it in native format (binary) and convert to string as necessary
– Slava
5 hours ago
integer values are always stored as binary. There is no way of changing this. You can, of course, display these binary values in whatever way you choose.
– Neil Butterworth
5 hours ago
A number is a number is a number - doesn't matter how it is stored, it is still the same number. But you can display it in different bases easily if you like. See en.cppreference.com/w/cpp/io/manip/hex
– Jesper Juhl
5 hours ago
numbers are stored as binary in memory, but you shouldn't have to deal with it most of the time. are you asking how to display them in an other base or how to set constant from an other base ?
– Tyker
5 hours ago
2 Answers
2
There is no such thing as decimal unsigned int
, hexadecimal unsigned int
or octal unsigned int
. There is only one unsigned int
. There is a difference only when you want to print an object of that type to the terminal or a file. From that point of view, the function
unsigned int
unsigned int
unsigned int
unsigned int
unsigned int decimal_to_octal(unsigned int decimal);
does not make sense at all. It makes sense to use:
struct decimal_tag {};
struct hexadecimal_tag {};
struct octal_tag {};
// Return a string that represents the number in decimal form
std::string to_string(unsigned int number, decimal_tag);
// Return a string that represents the number in hexadecimal form
std::string to_string(unsigned int number, hexadecimal_tag);
// Return a string that represents the number in octal form
std::string to_string(unsigned int number, octal_tag);
and their counterparts.
// Extract an unsigned number from the string that has decimal representation
unsigned int to_number(std::string const& s, decimal_tag);
// Extract an unsigned number from the string that has hexadecimal representation
unsigned int to_number(std::string const& s, hexadecimal_tag);
// Extract an unsigned number from the string that has octal representation
unsigned int to_number(std::string const& s, octal_tag);
Here's demonstrative program:
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
struct decimal_tag {};
struct hexadecimal_tag {};
struct octal_tag {};
// Return a string that represents the number in decimal form
std::string to_string(unsigned int number, decimal_tag)
{
std::ostringstream str;
str << std::dec << number;
return str.str();
}
// Return a string that represents the number in hexadecimal form
std::string to_string(unsigned int number, hexadecimal_tag)
{
std::ostringstream str;
str << std::hex << number;
return str.str();
}
// Return a string that represents the number in octal form
std::string to_string(unsigned int number, octal_tag)
{
std::ostringstream str;
str << std::oct << number;
return str.str();
}
// Extract an unsigned number from the string that has decimal representation
unsigned int to_number(std::string const& s, decimal_tag)
{
std::istringstream str(s);
unsigned int number;
str >> std::dec >> number;
return number;
}
// Extract an unsigned number from the string that has hexadecimal representation
unsigned int to_number(std::string const& s, hexadecimal_tag)
{
std::istringstream str(s);
unsigned int number;
str >> std::hex >> number;
return number;
}
// Extract an unsigned number from the string that has octal representation
unsigned int to_number(std::string const& s, octal_tag)
{
std::istringstream str(s);
unsigned int number;
str >> std::oct >> number;
return number;
}
int main()
{
unsigned int n = 200;
std::cout << "200 in decimal: " << to_string(n, decimal_tag()) << std::endl;
std::cout << "200 in hexadecimal: " << to_string(n, hexadecimal_tag()) << std::endl;
std::cout << "200 in octal: " << to_string(n, octal_tag()) << std::endl;
std::cout << "Number from decimal form (200): " << to_number("200", decimal_tag()) << std::endl;
std::cout << "Number from hexadcimal form (c8): " << to_number("c8", hexadecimal_tag()) << std::endl;
std::cout << "Number from octal form (310): " << to_number("310", octal_tag()) << std::endl;
}
and its output:
200 in decimal: 200
200 in hexadecimal: c8
200 in octal: 310
Number from decimal form (200): 200
Number from hexadcimal form (c8): 200
Number from octal form (310): 200
Thanks for the correction; this is what I'm really trying to do (see edited post above). Question still remains: what is the best way to convert to and from an octal string representation of an integer in C++?
– Patrick M
23 mins ago
@PatrickM, I've updated my answer to offer one solution. it's hard to say what's "best". It depends on what you are looking for. If you are looking for easy to understand and maintain code, my implementation should be good. If you are looking for something that can be used millions of times without it being a bottleneck, you will probably need to use other methods.
– R Sahu
8 mins ago
Printing numbers in different bases:
#include <iostream>
int main () {
int n = 123;
std::cout << std::dec << n << 'n';
std::cout << std::hex << n << 'n';
std::cout << std::oct << n << 'n';
return 0;
}
An X-Y answer, but likely the best route for this question.
– user4581301
5 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
cplusplus.com/reference/ios/oct
– skeller
5 hours ago