데이터 형변환 > MFC Tip

본문 바로가기
사이트 내 전체검색

MFC Tip

데이터 형변환

페이지 정보

profile_image
작성자 이즈
댓글 0건 조회 657회 작성일 06-12-27 00:09

본문

현재 프로그램 상에서 에디터 창으로 입력받은 아스키 데이터 20바이트를 헥사값 10바이트로 바꿔서 저장한 후 전송하려 합니다. 예를 들면 '3', '2' (ASCII - 2 byte)를 입력받아 헥사값 '32'로 저장하려는 것이지요(HEXA - 1 byte).

'3' '2' 'A' '2' '1' ....'9' '9' '0' (화면상에서 20자리 데이터 입력)
'32' 'A2' 1 .... '90' (실제적으로 저장되는 값은 10자리 헥사값의 데이터)

10자리로 정의된 헥사값은 다시 문자열 버퍼에 저장되서 전송돼야 하는데 간단한 문제를 제가 너무 어렵게 생각하고 있는지도 모르겠습니다. 방법을 알려주세요.

다음은 다이얼로그의 에디트 컨트롤에서 헥사값을 입력받아 RS232 포트로 날리는 내용의 코드입니다.

 헥사값 입력받기

다이얼로그의 에디트 박스 컨트롤에서 특정한 문자만 입력받게 합니다.

[1] 다이얼로그 리소스에서 에디트 박스를 하나 만듭니다.

[2] 클래스 위저드에서 에디트 박스 컨트롤 변수를 하나 설정합니다.

[3] 클래스 위저드에서 에디트 박스 컨트롤의 EN_CHANGE 함수를 만들고 다음과 같이 코딩합니다.

void CMyDlg::OnChangeEditAddress()
{
    // send this notification unless you override the CDialog::OnInitDialog()
    // function to send the EM_SETEVENTMASK message to the control
    // with the ENM_CHANGE flag ORed into the lParam mask.

    int i, j, len, nbytes, pos1, pos2;
    char *line, *newline;

    m_EBCaddress.GetSel(pos1, pos2);
    len = m_EBCaddress.GetWindowTextLength();
    line = new char[len+1];
    newline = new char[len+1];
    nbytes = m_EBCaddress.GetWindowText(line, len+1);

    // 아래부분은 헥사값만 체크하는데
    // 필요할 경우 조건문을 바꾼다.
    for(i = j = 0; i <= nbytes; i++) {    // null 포함
          if(isxdigit(line[i]) :: line[i] == NULL)
              newline[j++] = line[i];
    }

    if(strcmp(line, newline) != 0) {
          m_EBCaddress.SetWindowText(newline);    // 여기서 재귀호출
          if(pos1 - (i - j) >= 0) pos1 = pos1 - (i - j);
          if(pos2 - (i - j) >= 0) pos2 = pos2 - (i - j);
          m_EBCaddress.SetSel(pos1, pos2);    // hh1rr
    }

    delete line;
    delete newline;
}

 헥사 데이터 날리기

 void CSerialDlg::OnButtonSendHexa()
{
    int i, n, c;
    CString string, temp;

    UpdateData(TRUE);

    // 헥사 고르기
    n = m_EDsendHexa.GetLength();
    if(n == 0)
          return;
   
    for(i=0; i<n; i++) {
          c = m_EDsendHexa.GetAt(i);
          if(isxdigit(c)) {
              temp.Format("%c", c);
              string += temp;
          }
    }

    m_EDsendHexa = string;
    m_EDsendHexa.MakeUpper();
    UpdateData(FALSE);

    // sendbuf
    BYTE sendbuf[MAX_SEND_SIZE];
    int nSize;

    string = m_EDsendHexa;
    // 2 char 가 하나의 hexa 값으로 날아감
    nSize = string.GetLength()/2;
    for(i=0; i<nSize; i++) {
          temp = string.Left(2);
          string = string.Right(string.GetLength()-2);
         
          sscanf(temp, "%x", &c);
          sendbuf[i] = c;
    }

    if(!m_COM.Write(sendbuf, nSize)) {
          temp.Format("%s Write Error !!", m_port);
          AddListBox(&m_LBCstatus, temp);
          return;
    }
}

댓글목록

등록된 댓글이 없습니다.

Total 172건 8 페이지

검색

회원로그인

회원가입

사이트 정보

컴퓨터 정보,윈도우즈,리눅스,포토샵,3ds
맥스,프로그래밍 강좌팁

접속자집계

오늘
337
어제
558
최대
5,287
전체
630,442
Copyright © www.qdata.co.kr All rights reserved.