プログラミングのメモ帳(C/C++/HSP)

日々のプログラミングで気づいた点や小技集を紹介します。(Windows 10/XP/Vista、VC2017、HSP)

整数値から文字列に変換(まとめ)

2012年04月22日 10時18分07秒 | アルゴリズム関連

整数値から文字列に変換する関数群をまとめたページです。(戻る)

アルゴリズム

整数値から文字列に変換する場合は、整数値の下位桁から[0]~[35]の余りを解読してきます。
余りは普通に36で割って求めます。(%演算)
なお、基数が2の倍数である 2進数、8進数、16進数はビット論理積を使って余りを求めます。
この方がちょっとだけ高速になります。(最新のパソコンでは高速化してるのか良く分からないね)

この余りから[0]~[9]、[A]~[Z]、[a]~[z]の文字に変換して文字列バッファ(lpBuff)にコピーします。
しかし、このままでは文字列が左右反転してるので戻すために左右並びを反転させます。

//------------------------------------------------
// 関数のプロトタイプ宣言
//------------------------------------------------
extern LPTSTR funcIntToBin( LPTSTR lpBuff, UINT nValue );
extern LPTSTR funcIntToOct( LPTSTR lpBuff, UINT nValue );
extern LPTSTR funcIntToHex( LPTSTR lpBuff, UINT nValue );
extern LPTSTR funcIntToDec( LPTSTR lpBuff, UINT nValue );
extern LPTSTR funcIntToStr( LPTSTR lpBuff, UINT nValue, LONG nRadix );

サンプル

//------------------------------------------------------------------------------
// 整数値から2進/8進/10進/16進文字列に変換
//------------------------------------------------------------------------------
#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <Windows.h>

//------------------------------------------------
// 文字列の左右並びを反転
//------------------------------------------------
static VOID funcStrReverse( LPTSTR lpHead, LPTSTR lpTail )
{
    TCHAR tcSwap[ 1 ];
    
    for ( *lpTail-- = TEXT('\0') ; lpHead < lpTail ; lpHead++, lpTail-- ){
        tcSwap[ 0 ] = lpHead[ 0 ];
        lpHead[ 0 ] = lpTail[ 0 ];
        lpTail[ 0 ] = tcSwap[ 0 ];
    }
}

//------------------------------------------------
// 整数値から2進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToBin( LPTSTR lpBuff, UINT nValue )
{
    LPTSTR lpTail;
    
    if ( nValue == 0 ){
        lpBuff[0] = TEXT('0');
        lpBuff[1] = TEXT('\0');
        return lpBuff;
    }
    for ( lpTail = lpBuff ; nValue ; nValue >>= 1 ){
        *lpTail++ = ((nValue & 0x1) + TEXT('0'));
    }
    funcStrReverse( lpBuff, lpTail );
    return lpBuff;
}

//------------------------------------------------
// 整数値から8進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToOct( LPTSTR lpBuff, UINT nValue )
{
    LPTSTR lpTail;
    
    if ( nValue == 0 ){
        lpBuff[0] = TEXT('0');
        lpBuff[1] = TEXT('\0');
        return lpBuff;
    }
    for ( lpTail = lpBuff ; nValue ; nValue >>= 3 ){
        *lpTail++ = ((nValue & 0x7) + TEXT('0'));
    }
    funcStrReverse( lpBuff, lpTail );
    return lpBuff;
}

//------------------------------------------------
// 整数値から16進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToHex( LPTSTR lpBuff, UINT nValue )
{
    LPTSTR lpTail;
    
    if ( nValue == 0 ){
        lpBuff[0] = TEXT('0');
        lpBuff[1] = TEXT('\0');
        return lpBuff;
    }
    for ( lpTail = lpBuff ; nValue ; nValue >>= 4 ){
        *lpTail++ = TEXT("0123456789ABCDEF")[ nValue & 0xF ];
    }
    funcStrReverse( lpBuff, lpTail );
    return lpBuff;
}

//------------------------------------------------
// 整数値から10進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToDec( LPTSTR lpBuff, UINT nValue )
{
    LPTSTR lpTail;
    
    if ( nValue == 0 ){
        lpBuff[0] = TEXT('0');
        lpBuff[1] = TEXT('\0');
        return lpBuff;
    }
    for ( lpTail = lpBuff ; nValue ; nValue /= 10 ){
        *lpTail++ = ((nValue % 10) + TEXT('0'));
    }
    funcStrReverse( lpBuff, lpTail );
    return lpBuff;
}

//------------------------------------------------
// 整数値からn進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToStr( LPTSTR lpBuff, UINT nValue, LONG nRadix )
{
    LPTSTR lpTable;
    LPTSTR lpTail;
    
    if ( nValue == 0 ){
        lpBuff[0] = TEXT('0');
        lpBuff[1] = TEXT('\0');
        return lpBuff;
    }
    if ( nRadix < 0 ){
        nRadix = -nRadix;
        lpTable = TEXT("0123456789abcdefghijklmnopqrstuvwxyz");
    }
    else{
        lpTable = TEXT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }
    for ( lpTail = lpBuff ; nValue ; nValue /= nRadix ){
        *lpTail++ = lpTable[ nValue % nRadix ];
    }
    funcStrReverse( lpBuff, lpTail );
    return lpBuff;
}

//------------------------------------------------
// メイン関数
//------------------------------------------------
int _tmain( void )
{
    TCHAR   szBuff[ 256 ];
    LONG    nValue;
    
    _tsetlocale( LC_ALL, TEXT("") );
    _tprintf( TEXT("\n// 整数値から2進文字列に変換\n") );
    for ( nValue = 0 ; nValue < 32 ; nValue++ ){
        _tprintf( TEXT("%2ld = [%s]\n"), nValue, funcIntToBin(szBuff,nValue) );
    }
    _tprintf( TEXT("\n// 整数値から8進文字列に変換\n") );
    for ( nValue = 0 ; nValue < 32 ; nValue++ ){
        _tprintf( TEXT("%2ld = [%s]\n"), nValue, funcIntToOct(szBuff,nValue) );
    }
    _tprintf( TEXT("\n// 整数値から10進文字列に変換\n") );
    for ( nValue = 0 ; nValue < 32 ; nValue++ ){
        _tprintf( TEXT("%2ld = [%s]\n"), nValue, funcIntToDec(szBuff,nValue) );
    }
    _tprintf( TEXT("\n// 整数値から16進文字列に変換\n") );
    for ( nValue = 0 ; nValue < 32 ; nValue++ ){
        _tprintf( TEXT("%2ld = [%s]\n"), nValue, funcIntToHex(szBuff,nValue) );
    }
    _tprintf( TEXT("\n// 整数値からn進文字列に変換(大文字)\n") );
    for ( nValue = 0 ; nValue < 32 ; nValue++ ){
        _tprintf( TEXT("%2ld = [%s]\n"), nValue, funcIntToStr(szBuff,nValue,+16) );
    }
    _tprintf( TEXT("\n// 整数値からn進文字列に変換(小文字)\n") );
    for ( nValue = 0 ; nValue < 32 ; nValue++ ){
        _tprintf( TEXT("%2ld = [%s]\n"), nValue, funcIntToStr(szBuff,nValue,-16) );
    }
    return 0;
}

//------------------------------------------------------------------------------
// End of funcIntToStrLib.cpp
//------------------------------------------------------------------------------

実行結果

// 整数値から2進文字列に変換
 0 = [0]
 1 = [1]
 2 = [10]
 3 = [11]
 4 = [100]
 5 = [101]
 6 = [110]
 7 = [111]
 8 = [1000]
 9 = [1001]
10 = [1010]
11 = [1011]
12 = [1100]
13 = [1101]
14 = [1110]
15 = [1111]
16 = [10000]
17 = [10001]
18 = [10010]
19 = [10011]
20 = [10100]
21 = [10101]
22 = [10110]
23 = [10111]
24 = [11000]
25 = [11001]
26 = [11010]
27 = [11011]
28 = [11100]
29 = [11101]
30 = [11110]
31 = [11111]

// 整数値から8進文字列に変換
 0 = [0]
 1 = [1]
 2 = [2]
 3 = [3]
 4 = [4]
 5 = [5]
 6 = [6]
 7 = [7]
 8 = [10]
 9 = [11]
10 = [12]
11 = [13]
12 = [14]
13 = [15]
14 = [16]
15 = [17]
16 = [20]
17 = [21]
18 = [22]
19 = [23]
20 = [24]
21 = [25]
22 = [26]
23 = [27]
24 = [30]
25 = [31]
26 = [32]
27 = [33]
28 = [34]
29 = [35]
30 = [36]
31 = [37]

// 整数値から10進文字列に変換
 0 = [0]
 1 = [1]
 2 = [2]
 3 = [3]
 4 = [4]
 5 = [5]
 6 = [6]
 7 = [7]
 8 = [8]
 9 = [9]
10 = [10]
11 = [11]
12 = [12]
13 = [13]
14 = [14]
15 = [15]
16 = [16]
17 = [17]
18 = [18]
19 = [19]
20 = [20]
21 = [21]
22 = [22]
23 = [23]
24 = [24]
25 = [25]
26 = [26]
27 = [27]
28 = [28]
29 = [29]
30 = [30]
31 = [31]

// 整数値から16進文字列に変換
 0 = [0]
 1 = [1]
 2 = [2]
 3 = [3]
 4 = [4]
 5 = [5]
 6 = [6]
 7 = [7]
 8 = [8]
 9 = [9]
10 = [A]
11 = [B]
12 = [C]
13 = [D]
14 = [E]
15 = [F]
16 = [10]
17 = [11]
18 = [12]
19 = [13]
20 = [14]
21 = [15]
22 = [16]
23 = [17]
24 = [18]
25 = [19]
26 = [1A]
27 = [1B]
28 = [1C]
29 = [1D]
30 = [1E]
31 = [1F]

// 整数値からn進文字列に変換(大文字)
 0 = [0]
 1 = [1]
 2 = [2]
 3 = [3]
 4 = [4]
 5 = [5]
 6 = [6]
 7 = [7]
 8 = [8]
 9 = [9]
10 = [A]
11 = [B]
12 = [C]
13 = [D]
14 = [E]
15 = [F]
16 = [10]
17 = [11]
18 = [12]
19 = [13]
20 = [14]
21 = [15]
22 = [16]
23 = [17]
24 = [18]
25 = [19]
26 = [1A]
27 = [1B]
28 = [1C]
29 = [1D]
30 = [1E]
31 = [1F]

// 整数値からn進文字列に変換(小文字)
 0 = [0]
 1 = [1]
 2 = [2]
 3 = [3]
 4 = [4]
 5 = [5]
 6 = [6]
 7 = [7]
 8 = [8]
 9 = [9]
10 = [a]
11 = [b]
12 = [c]
13 = [d]
14 = [e]
15 = [f]
16 = [10]
17 = [11]
18 = [12]
19 = [13]
20 = [14]
21 = [15]
22 = [16]
23 = [17]
24 = [18]
25 = [19]
26 = [1a]
27 = [1b]
28 = [1c]
29 = [1d]
30 = [1e]
31 = [1f]
←前へ] [目次] [次へ→

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« 文字列から整数値に変換(まとめ) | トップ | 整数値からn桁文字列に変換(... »
最新の画像もっと見る

コメントを投稿

アルゴリズム関連」カテゴリの最新記事