scrap book

プログラミングとか

null文字のないchar[]の変換メモ

null文字のないchar[]の変換メモ。面倒

code.cpp

#include <iostream>
#include <sstream>
#include <iomanip>
#include <boost/lexical_cast.hpp>

using namespace std;
using namespace boost;


// print 'lexical_cast'ed cstr
void casttest( const char* cstr )
{
    cout << "[" << cstr << "]" << endl;

    try
    {
        cout << "    ->" << lexical_cast<int>(cstr) << endl;
    }
    catch(std::exception& e)
    {
        cout << "    ->" << e.what() << endl;
    }
}


// int -> char[]
void int2zerochars(int val, char* cstr, size_t size)
{
    stringstream ss;
    ss.fill('0');
    ss << setw(size) << val;

    ss.str().copy(cstr, size);
}

// string -> char[]
void str2chars(string str, char* cstr, size_t size)
{
    // string::copy(dst, size, start_pos = 0);

    str.copy(cstr, size, 0);
}


// char[] -> string
string chars2str(char chars[], size_t size)
{
    stringstream ss;

    for(size_t i = 0; i < size; i++)
    {
        ss << chars[i];
    }

    return ss.str();
}


int main(void)
{
    char src[2] = { '9', '2' };

    cout << chars2str( src, sizeof(src) ) << endl;

    int val = lexical_cast<int>(chars2str( src, sizeof(src) ));
    cout << val << endl;


    //------------------------------------------------------
    casttest(" ");
    casttest("");
    casttest("a");
    casttest("0xDD");
    casttest("000");
    casttest("010");
    casttest("-1");
    casttest("--1");
    casttest("1-1");


    //------------------------------------------------------
    char dst[4] = { 'x', 'x', 'x', 'x' };

    string abc("abc");
    string defg("defg");
    string hijkl("hijkl");

    // 3characters (size-1)
    str2chars(abc, dst, sizeof(dst));
    cout << dst << endl;
    for(size_t i=0; i<sizeof(dst); i++){ cout<<"  "<<i<<":"<<dst[i]; cout<<endl; }

    // 4characters (size)
    str2chars(defg, dst, sizeof(dst));
    cout << dst << endl;
    for(size_t i=0; i<sizeof(dst); i++){ cout<<"  "<<i<<":"<<dst[i]; cout<<endl; }

    // 5characters (size+1)
    str2chars(hijkl, dst, sizeof(dst));
    cout << dst << endl;
    for(size_t i=0; i<sizeof(dst); i++){ cout<<"  "<<i<<":"<<dst[i]; cout<<endl; }

    //------------------------------------------------------
    char xxxx[4] = { 'x', 'x', 'x', 'x' };

    int2zerochars( 2, xxxx, sizeof(xxxx) );
    cout << xxxx << endl;  cout.flush() << endl;

    int2zerochars( 888, xxxx, sizeof(xxxx) );
    cout << xxxx << endl;  cout.flush() << endl;

    int2zerochars( 9999, xxxx, sizeof(xxxx) );
    cout << xxxx << endl;  cout.flush() << endl;

    int2zerochars( 77777, xxxx, sizeof(xxxx) );
    cout << xxxx << endl;  cout.flush() << endl;

    cout << 9 << endl;

    return 0;
}


result

92
92
[ ]
    ->bad lexical cast: source type value could not be interpreted as target
[]
    ->bad lexical cast: source type value could not be interpreted as target
[a]
    ->bad lexical cast: source type value could not be interpreted as target
[0xDD]
    ->bad lexical cast: source type value could not be interpreted as target
[000]
    ->0
[010]
    ->10
[-1]
    ->-1
[--1]
    ->bad lexical cast: source type value could not be interpreted as target
[1-1]
    ->bad lexical cast: source type value could not be interpreted as target
abcx
  0:a
  1:b
  2:c
  3:x
defg
  0:d
  1:e
  2:f
  3:g
hijk
  0:h
  1:i
  2:j
  3:k
0002<

0888<

9999<

7777<

9