わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

QuotedPrintableをデコードしてstringで取得する

QuotedPrintableをデコードしてstringで取得します。

#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <string>
#include <iostream>



const static char* g_cpHex = "0123456789ABCDEF";


/*
	QuotedPrintableデコード
*/
std::string DecodeQuotedPrintable
(
	std::string oStr
)
{
	std::string oRet;

	std::string::iterator ite    = oStr.begin();
	std::string::iterator iteEnd = oStr.end();

	// 全データを巡回
	while( ite != iteEnd ) {

		// 1データ取得
		unsigned char bChar = *ite++;

		// 変換
		if ( '=' != bChar ) {
			oRet += bChar;
		}
		else {
			unsigned bHex;
			bChar = *ite++;

			if ( '¥r' == bChar ) {

				// '¥n'読み飛ばし
				bChar = *ite++;
			}
			if ( '¥n' == bChar ) {

				// '¥n'読み飛ばし
				continue;
			}

			if ( '0' <= bChar && '9' >= bChar ) {
				bHex = bChar - '0';
			}
			else if ( 'A' <= bChar && 'F' >= bChar ) {
				bHex = bChar - 'A' + 10;
			}
			else if ( 'a' <= bChar && 'f' >= bChar ) {
				bHex = bChar - 'a' + 10;
			}
			bHex *= 16;
			bChar = *ite++;
			if ( '0' <= bChar && '9' >= bChar ) {
				bHex += bChar - '0';
			}
			else if ( 'A' <= bChar && 'F' >= bChar ) {
				bHex += bChar - 'A' + 10;
			}
			else if ( 'a' <= bChar && 'f' >= bChar ) {
				bHex += bChar - 'a' + 10;
			}
			oRet += bHex;
		}
	}

	// 結果を返す
	return( oRet );
}



int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	// 標準出力にユニコード出力する
	setlocale( LC_ALL, "Japanese" );

	std::string str = "=51=75=6F=74=65=64=50=72=69=6E=74=61=62=6C=65";

	std::cout << "■変換前" << std::endl;
	std::cout << str << std::endl;
	std::cout << std::endl;

	// QuotedPrintableをデコードする
	str = DecodeQuotedPrintable( str );

	// 標準出力へ出力する
	std::cout << "■変換後" << std::endl;
	std::cout << str << std::endl;

	// 正常終了
	return( 0 );
}

実行結果

■変換前
=51=75=6F=74=65=64=50=72=69=6E=74=61=62=6C=65

■変換後
QuotedPrintable






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。