Click here to Skip to main content
15,893,190 members
Articles / Programming Languages / C++

A Literal Converter for Integers

Rate me:
Please Sign up or sign in to vote.
4.18/5 (5 votes)
19 Aug 2009CPOL3 min read 62.6K   1.4K   22  
A helper class to convert integer literals between decimals, octals, binaries and hexadecimals.
/*
  Name: CLiteralConverter
  Author: Tom Gee, Shanghai
  Date: 13/05/05 15:25
  Description: 
               A helper class to convert integer literals between decimals, 
               octals, Binaries and hexadecimals.
               
  Revision: 
    - Veriosn 1.0, 2005-05-13, first release
    - Version 1.1, 2009-08-17, fixed an overflow bug with long type,  use size_t instead.
  */

#ifndef _LITERAL_CONVERTER_H_
#define _LITERAL_CONVERTER_H_

#include <string>
#include <fstream>
#include <vector>

using std::string;
using std::ifstream;
using std::vector;

class CLiteralConverter
{
public:
	typedef enum
	{
		DEC = 0,
		HEX = 1,
		OCT = 2,
		BIN = 3
	} _FORMAT_;
public:
    CLiteralConverter();
private:
	_FORMAT_ m_inFormat;
	vector<size_t> m_vValue;
    string m_sIn;
	string m_sInDelim, m_sOutDelim;

private:
    void Convert2Value();
    string Convert2Str(_FORMAT_ format);
public:
    void SetString(const string& s, _FORMAT_ format, const string& sDelim = ",; :h");
    void SetString(ifstream& f, _FORMAT_ format, const string& sDelim = ",; :h");

    string GetDec(const string& sDelim = "; ");
    string GetHex(const string& sDelim = "; ");
    string GetOct(const string& sDelim = "; ");
    string GetBin(const string& sDelim = "; ");
	string GetString(_FORMAT_ format, const string& sDelim = "; ");
	void GetValue(vector<size_t>& vValue);
};
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions