MFC UI에서 입력 받은 여러개의 CString 으로 받은 다음에 하나의 char arry로 바꾸는 작업을 위해서 



방법 !

CString을 선언하고

strncpy로 복사하면 된다.


CString g_postSerialnum;

char temp_sn[12];

g_postSerialnum= _T("A0111010asdas");

strncpy(temp_sn, g_postSerialnum, 12);

만일 길이를 지정하지 않고 strcpy를 쓴다면?

strcpy(temp_sn, g_postSerialnum);

환경에 따라서 다르겠지만 MFC C++ / 2008에서는 함수 콜이 끝나면 죽는다

디버그에서는 안 죽고 릴리스에서만!!!

주의해서 써야 한다.


후썰...


이짓을 하게된 배경


<전임자코드>--------------------------------------------------------------

CString set_product_code1, set_product_code2, set_made_factory, set_made_date, set_made_year, set_product_num;


CString str;


//이 부분은 UI에서 얻어오는 부분이기때문에 일단 보류

m_sn_model_list.GetLBText(m_sn_model_list.GetCurSel(), set_product_code1);

m_sn_model_list1.GetLBText(m_sn_model_list1.GetCurSel(), set_product_code2);

m_mcnt_list.GetLBText(m_mcnt_list.GetCurSel(), set_made_factory);

GetDlgItemText(20012, set_made_date);

m_version_list.GetLBText(m_version_list.GetCurSel(), set_made_year);

GetDlgItemText(20017, set_product_num);


////////////////////////////////////////////////////////////////////////////////////////

//SN char 배열에 삽입


char model_code[2], factory_code[2], date[2], year[2], count1[2], count2[2];

CString set_made_date1, set_made_date2, set_made_date3;

CString set_product_num1, set_product_num2, set_product_num3, set_product_num4, set_product_num5;


//CString을 한자씩 짤라서 CString 한 글자씩으로 만든다.

//한글자씩 잘라서

set_made_date1 = set_made_date.Mid(0,1);

set_made_date2 = set_made_date.Mid(1,1);

set_made_date3 = set_made_date.Mid(2,1);

set_product_num1 = set_product_num.Mid(0,1);

set_product_num2 = set_product_num.Mid(1,1);

set_product_num3 = set_product_num.Mid(2,1);

set_product_num4 = set_product_num.Mid(3,1);

set_product_num5 = set_product_num.Mid(4,1);


//각 한자의 CString을 sctcpy로 복사한다.

strcpy(&model_code[0], (LPSTR)(LPCTSTR)set_product_code1);

strcpy(&model_code[1], (LPSTR)(LPCTSTR)set_product_code2);

strcpy(&factory_code[0], (LPSTR)(LPCTSTR)set_made_factory);

strcpy(&factory_code[1], (LPSTR)(LPCTSTR)set_made_date1);

strcpy(&date[0], (LPSTR)(LPCTSTR)set_made_date2);

strcpy(&date[1], (LPSTR)(LPCTSTR)set_made_date3);

strcpy(&year[0], (LPSTR)(LPCTSTR)set_made_year);//year의 경우에는 심지어 한자인데도 2개로 선언하고 처리함.

strcpy(&year[1], (LPSTR)(LPCTSTR)set_product_num1);

strcpy(&count1[0], (LPSTR)(LPCTSTR)set_product_num2);

strcpy(&count1[1], (LPSTR)(LPCTSTR)set_product_num3);

strcpy(&count2[0], (LPSTR)(LPCTSTR)set_product_num4);

strcpy(&count2[1], (LPSTR)(LPCTSTR)set_product_num5);


//한자씩 넣은 것을 또 char arry에 하나씩 넣는다.

//이때 변수명도 제각각 이라 순환문을 못 쓰고 다 따로 넣어줘야 해서 리팩토링이 절실하다.

send_packet[11] = model_code[0];

send_packet[12] = model_code[1];

send_packet[13] = factory_code[0];

send_packet[14] = factory_code[1];

send_packet[15] = date[0];

send_packet[16] = date[1];

send_packet[17] = year[0];

send_packet[18] = year[1];

send_packet[19] = count1[0];

send_packet[20] = count1[1];

send_packet[21] = count2[0];

send_packet[22] = count2[1];


<나의 코드>

//CString의 값을 하나로 붙인다.

g_postSerialnum = set_p_group1+ set_p_group2 + set_p_model1+set_p_model2 + set_made_date + set_made_year + set_product_num;

//한방에 복사 끝. 어차피 하나로 붙인 char 모음들을 묶어서 선언하고 복사한다.

//이때 반드시 strncpy를 쓴다.

strncpy(temp_sn, g_postSerialnum, 12);

//당연히 하나의 char array 이므로 순환문으로 넣는 것이 가능하다

for(int i = 11; i < 23; i++)

{

send_packet[i] = temp_sn[i - 11];

}




by 무위자연 2015. 2. 3. 10:30
| 1 |