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

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

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

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

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

アルゴリズム

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

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

このときに出力桁数分だけスペース文字などを文字列バッファ(lpBuff)にコピーします。
こうすることで文字列バッファ(lpBuff)の先頭に出力桁数に見合うスペース文字で埋められます。
なお、スペース文字の代わりに[0]の文字で埋めたり、接頭語(0b、0o、0d、0x)もコピーするように改良も可能です。

接頭語(0b、0o、0d、0x)を出力するときには[x][0]の順にコピーしたり、出力桁数から2文字分だけ引いた残りが
スペース文字(ゼロ文字)で埋める出力桁数になることに気をつけましょう。
今回は接頭語(0b、0o、0d、0x)は出力しません。

//------------------------------------------------
// 関数のプロトタイプ宣言
//------------------------------------------------
extern LPTSTR funcIntToBinEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace );
extern LPTSTR funcIntToOctEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace );
extern LPTSTR funcIntToHexEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace );
extern LPTSTR funcIntToDecEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace );
extern LPTSTR funcIntToStrEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace, LONG nRadix );

サンプル

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

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

//------------------------------------------------
// 整数値からn桁2進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToBinEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace )
{
    LPTSTR lpTail = lpBuff;
    
    if ( nValue == 0 ){
        nWidth--; *lpTail++ = TEXT('0');
    }
    else for ( ; nValue ; nValue >>= 1 ){
        nWidth--; *lpTail++ = ((nValue & 0x1) + TEXT('0'));
    }
    funcStrReverseEx( lpBuff, lpTail, nWidth, tcSpace );
    return lpBuff;
}

//------------------------------------------------
// 整数値からn桁8進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToOctEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace )
{
    LPTSTR lpTail = lpBuff;
    
    if ( nValue == 0 ){
        nWidth--; *lpTail++ = TEXT('0');
    }
    else for ( ; nValue ; nValue >>= 3 ){
        nWidth--; *lpTail++ = ((nValue & 0x7) + TEXT('0'));
    }
    funcStrReverseEx( lpBuff, lpTail, nWidth, tcSpace );
    return lpBuff;
}

//------------------------------------------------
// 整数値からn桁16進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToHexEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace )
{
    LPTSTR lpTail = lpBuff;
    
    if ( nValue == 0 ){
        nWidth--; *lpTail++ = TEXT('0');
    }
    else for ( ; nValue ; nValue >>= 4 ){
        nWidth--; *lpTail++ = TEXT("0123456789ABCDEF")[ nValue & 0xF ];
    }
    funcStrReverseEx( lpBuff, lpTail, nWidth, tcSpace );
    return lpBuff;
}

//------------------------------------------------
// 整数値からn桁10進文字列に変換
//------------------------------------------------
extern LPTSTR funcIntToDecEx( LPTSTR lpBuff, UINT nValue, LONG nWidth, TCHAR tcSpace )
{
    LPTSTR lpTail = lpBuff;
    
    if ( nValue == 0 ){
        nWidth--; *lpTail++ = TEXT('0');
    }
    else for ( ; nValue ; nValue /= 10 ){
        nWidth--; *lpTail++ = ((nValue % 10) + TEXT('0'));
    }
    funcStrReverseEx( lpBuff, lpTail, nWidth, tcSpace );
    return lpBuff;
}

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

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

//------------------------------------------------------------------------------
// End of funcIntToStrLibEx.cpp
//------------------------------------------------------------------------------

実行結果

// 整数値から2進文字列に変換
 0 = [00000000]
 1 = [00000001]
 2 = [00000010]
 3 = [00000011]
 4 = [00000100]
 5 = [00000101]
 6 = [00000110]
 7 = [00000111]
 8 = [00001000]
 9 = [00001001]
10 = [00001010]
11 = [00001011]
12 = [00001100]
13 = [00001101]
14 = [00001110]
15 = [00001111]
16 = [00010000]
17 = [00010001]
18 = [00010010]
19 = [00010011]
20 = [00010100]
21 = [00010101]
22 = [00010110]
23 = [00010111]
24 = [00011000]
25 = [00011001]
26 = [00011010]
27 = [00011011]
28 = [00011100]
29 = [00011101]
30 = [00011110]
31 = [00011111]

// 整数値から8進文字列に変換
 0 = [00000000]
 1 = [00000001]
 2 = [00000002]
 3 = [00000003]
 4 = [00000004]
 5 = [00000005]
 6 = [00000006]
 7 = [00000007]
 8 = [00000010]
 9 = [00000011]
10 = [00000012]
11 = [00000013]
12 = [00000014]
13 = [00000015]
14 = [00000016]
15 = [00000017]
16 = [00000020]
17 = [00000021]
18 = [00000022]
19 = [00000023]
20 = [00000024]
21 = [00000025]
22 = [00000026]
23 = [00000027]
24 = [00000030]
25 = [00000031]
26 = [00000032]
27 = [00000033]
28 = [00000034]
29 = [00000035]
30 = [00000036]
31 = [00000037]

// 整数値から10進文字列に変換
 0 = [00000000]
 1 = [00000001]
 2 = [00000002]
 3 = [00000003]
 4 = [00000004]
 5 = [00000005]
 6 = [00000006]
 7 = [00000007]
 8 = [00000008]
 9 = [00000009]
10 = [00000010]
11 = [00000011]
12 = [00000012]
13 = [00000013]
14 = [00000014]
15 = [00000015]
16 = [00000016]
17 = [00000017]
18 = [00000018]
19 = [00000019]
20 = [00000020]
21 = [00000021]
22 = [00000022]
23 = [00000023]
24 = [00000024]
25 = [00000025]
26 = [00000026]
27 = [00000027]
28 = [00000028]
29 = [00000029]
30 = [00000030]
31 = [00000031]

// 整数値から16進文字列に変換
 0 = [00000000]
 1 = [00000001]
 2 = [00000002]
 3 = [00000003]
 4 = [00000004]
 5 = [00000005]
 6 = [00000006]
 7 = [00000007]
 8 = [00000008]
 9 = [00000009]
10 = [0000000A]
11 = [0000000B]
12 = [0000000C]
13 = [0000000D]
14 = [0000000E]
15 = [0000000F]
16 = [00000010]
17 = [00000011]
18 = [00000012]
19 = [00000013]
20 = [00000014]
21 = [00000015]
22 = [00000016]
23 = [00000017]
24 = [00000018]
25 = [00000019]
26 = [0000001A]
27 = [0000001B]
28 = [0000001C]
29 = [0000001D]
30 = [0000001E]
31 = [0000001F]

// 整数値からn進文字列に変換(大文字)
 0 = [00000000]
 1 = [00000001]
 2 = [00000002]
 3 = [00000003]
 4 = [00000004]
 5 = [00000005]
 6 = [00000006]
 7 = [00000007]
 8 = [00000008]
 9 = [00000009]
10 = [0000000A]
11 = [0000000B]
12 = [0000000C]
13 = [0000000D]
14 = [0000000E]
15 = [0000000F]
16 = [00000010]
17 = [00000011]
18 = [00000012]
19 = [00000013]
20 = [00000014]
21 = [00000015]
22 = [00000016]
23 = [00000017]
24 = [00000018]
25 = [00000019]
26 = [0000001A]
27 = [0000001B]
28 = [0000001C]
29 = [0000001D]
30 = [0000001E]
31 = [0000001F]

// 整数値からn進文字列に変換(小文字)
 0 = [00000000]
 1 = [00000001]
 2 = [00000002]
 3 = [00000003]
 4 = [00000004]
 5 = [00000005]
 6 = [00000006]
 7 = [00000007]
 8 = [00000008]
 9 = [00000009]
10 = [0000000a]
11 = [0000000b]
12 = [0000000c]
13 = [0000000d]
14 = [0000000e]
15 = [0000000f]
16 = [00000010]
17 = [00000011]
18 = [00000012]
19 = [00000013]
20 = [00000014]
21 = [00000015]
22 = [00000016]
23 = [00000017]
24 = [00000018]
25 = [00000019]
26 = [0000001a]
27 = [0000001b]
28 = [0000001c]
29 = [0000001d]
30 = [0000001e]
31 = [0000001f]
←前へ] [目次] [次へ→]

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

コメントを投稿

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