본문 바로가기

Native/C++

base36 encode/decode

[code]
/**
* @file CBase36.h
* @author cheol-dong choi <aucd29@gmail.com>
* @version 1.0
* @date 2011-02-15 (09:34:12)
* @section LICENSE
*
* Copyright (c) 2003-2011, cheol-dong choi, (http://www.sarangnamu.net)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @section DESCRIPTION
* @see http://en.wikipedia.org/wiki/Base_36
*
* @section CHANGE_LOG
* - 주석 추가. 2011-02-15 (09:50:14)
*/

#ifndef __CD_CBASE36_H__
#define __CD_CBASE36_H__

#include <string>


const static std::string baseKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


/**
* @class CBase36
* @brief Base36 으로 인코딩/디코딩 하는 클래스
*/
class CBase36
{
public:

    /*
     * Construction
     */
    CBase36() {}

    /*
     * Destructor
     */
    virtual ~CBase36() {}

public:

    /*
     * Getter methods
     */


    /**
     * 임의에 숫자를 base36 형태로 변환해서 리턴한다.
     *
     * @param num 임의의 숫자.
     * @return base36 결과 값
     */
    std::string encode(long num)
    {
        long remainder;
        std::string temp, result;

        if (num < 1)
        {
            return "0";
        }

        do
        {
            remainder = num % _base;
            temp = baseKey.substr(remainder, 1);
            result = temp + result;

            num = (num - remainder) / _base;

        } while(num > 0);

        return result;
    }


    /**
     * base36 에 문자열을 원래의 숫자로 복원한다.
     *
     * @param base36String 변경할 base36 형태의 문자열
     * @return 입력된 base36 문자열을 숫자로 변경한 값
     */
    long decode(std::string base36String)
    {
        if (base36String.size() == 0)
        {
            return false;
        }

        long res = 0;
        std::string temp;

        do
        {
            temp = base36String.substr(0, 1);
            base36String = base36String.substr(1, base36String.size() - 1);

            size_t pos = baseKey.find(temp);
            if (pos == std::string::npos)
            {
                return false;
            }

            res = (res * _base) + pos;
        } while(base36String.size() > 0);

        return res;
    }


protected:

    /*
     * Internal methods
     */

protected:

    /*
     * Attributes
     */

    int _base;
};

#endif
[/code]

base 값만 변경해서 2진수/8진수/16진수 로 만들 수 있다....

'Native > C++' 카테고리의 다른 글

I64x  (0) 2013.10.02
namespace  (0) 2013.10.02
Very very useful: eBooks compiled from top StackOverflow topics/answers  (0) 2013.10.02
c++0x closure  (0) 2013.10.02
c++0x lamda  (0) 2013.10.02