Programming/MFC [데이터형 변환] CStirng -> Hex, Ascii 값 푸어맨 2016. 9. 9. 13:58 BYTE SerialDlg::HexTo8Bits(CString aHex) { BYTE a8Bits = 0; if ( aHex == _T("0") ) a8Bits = 0; else if ( aHex == _T("1") ) a8Bits = 1; else if ( aHex == _T("2") ) a8Bits = 2; else if ( aHex == _T("3") ) a8Bits = 3; else if ( aHex == _T("4") ) a8Bits = 4; else if ( aHex == _T("5") ) a8Bits = 5; else if ( aHex == _T("6") ) a8Bits = 6; else if ( aHex == _T("7") ) a8Bits = 7; else if ( aHex == _T("8") ) a8Bits = 8; else if ( aHex == _T("9") ) a8Bits = 9; else if ( aHex == _T("A") || aHex == _T("a") ) a8Bits = 10; else if ( aHex == _T("B") || aHex == _T("b") ) a8Bits = 11; else if ( aHex == _T("C") || aHex == _T("c") ) a8Bits = 12; else if ( aHex == _T("D") || aHex == _T("d") ) a8Bits = 13; else if ( aHex == _T("E") || aHex == _T("e") ) a8Bits = 14; else if ( aHex == _T("F") || aHex == _T("f") ) a8Bits = 15; return a8Bits; } BYTE SerialDlg::HexToByte(CString aHex) { BYTE aByte = 0; if ( aHex.GetLength() == 1 ) { aByte = HexTo8Bits(aHex.Left(1)); } else { aByte = (HexTo8Bits(aHex.Left(1)) * 16) + HexTo8Bits(aHex.Right(1)); } return aByte; } BYTE SerialDlg::AsciiToByte(CString aAscii) { BYTE aByte = 0; aByte = aAscii[0]; return aByte; } void SerialDlg::OnBnClickedButtonSnd() { // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다. int sndSize = 0, idx = 0; CString sndData, sndByte; BYTE *pbDataBuff; // 포트를 여는데 성공했다면 if ( m_ComuPort.m_bConnected == TRUE ) { UpdateData(TRUE); m_edit_serial_snd.GetWindowTextW(sndData); sndData.Replace(_T(" "), NULL); sndData.Replace(_T("\r\n"), NULL); sndSize = sndData.GetLength(); if ( m_combo_type_snd.GetCurSel() == 0 ) { if ( sndSize%2 == 0 ) sndSize = sndSize/2; else sndSize = sndSize/2 + 1; pbDataBuff = (BYTE *)malloc(sizeof(BYTE) * sndSize); for (idx = 0; idx < sndSize; idx++) { pbDataBuff[idx] = HexToByte(sndData.Mid(idx*2, 2)); } } else if ( m_combo_type_snd.GetCurSel() == 1 ) { pbDataBuff = (BYTE *)malloc(sizeof(BYTE) * sndSize); for (idx = 0; idx < sndSize; idx++) { pbDataBuff[idx] = AsciiToByte(sndData.Mid(idx, 1)); } } // F0 AA 54 41 48 55 01 00 00 00 00 00 12 41 F0 55 디스플레이 확인용 m_ComuPort.WriteComm(pbDataBuff, sndSize); free(pbDataBuff); } }