MFC c++ convert a CString to BYTE

Discussion in 'Programmer's Corner' started by TBayBoy, Sep 27, 2013.

  1. TBayBoy

    Thread Starter Member

    May 25, 2011
    148
    19
    So if I understand correctly a BYTE is an unsigned character so here is the code I:

    Code ( (Unknown Language)):
    1. [FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]
    2. void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] CEL505_Lab002Dlg::OnBnClickedButtonInput()
    3. {
    4. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// TODO: Add your control notification handler code here
    5. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]CString sBuffer;
    6. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]unsigned[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]char[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] temp_num;
    7. InputDlg input;
    8. input.m_sHexInput.Format( _T([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"%02X"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]),m_bNumber);
    9. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2](input.DoModal() ==IDOK)
    10. {
    11.  
    12. sBuffer.Format(_T([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"%s"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]), input.m_sHexInput);
    13. temp_num.Format( _T([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"%u"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]),input.m_sHexInput);
    14.  
    15.  
    16.  
    17. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]//MessageBox(sBuffer,_T("Custom Dialog Result"),MB_OK | MB_ICONINFORMATION);
    18. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]}
    19.  
    20. }
    21. [/SIZE][/FONT][/SIZE][/FONT]
    I push a BYTE to an input box as a Hex, and then after the user updates the number and presses OK the number updated value is returned as a CString.
    sBuffer will hold the string, but I want temp_num to hold the returned string as a BYTE or unsigned character.

    the problem seems to be "temp_num.Format( _T("%u"),input.m_sHexInput);"

    wither I haven't given temp_num a class properly or I am going about this all wrong.

    Help is greatly appreciated ;)
     
  2. TBayBoy

    Thread Starter Member

    May 25, 2011
    148
    19
    I guess I'm not the only one who is stumped by this, makes me feel not so bad.. :)
     
  3. Tesla23

    Active Member

    May 10, 2009
    318
    67
    You seem to have confused a few things. Without seeing the declaration of InputDlg, is seems that m_sHexInput is a CString. In this case,

    Code ( (Unknown Language)):
    1.  
    2. [FONT=Consolas][SIZE=2][SIZE=2]sBuffer.Format(_T([/SIZE][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"%s"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][SIZE=2]), input.m_sHexInput);[/SIZE][/SIZE][/FONT][SIZE=2]
    3. [/SIZE]
    can be simply
    Code ( (Unknown Language)):
    1.  
    2. [FONT=Consolas][SIZE=2][SIZE=2]sBuffer = [/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][SIZE=2]input.m_sHexInput;[/SIZE][/SIZE][/FONT][SIZE=2]
    3. [/SIZE]
    and

    Code ( (Unknown Language)):
    1.  
    2. [FONT=Consolas][SIZE=2][SIZE=2]temp_num.Format( _T([/SIZE][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"%u"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][FONT=Consolas][SIZE=2]),input.m_sHexInput);[/SIZE][/FONT]
    3. [/SIZE]
    should be something like:

    Code ( (Unknown Language)):
    1.  
    2. [FONT=Consolas][SIZE=2][SIZE=2]temp_num = atoi(input[/SIZE][/SIZE][/FONT][SIZE=2][FONT=Consolas][SIZE=2].m_sHexInput);[/SIZE][/FONT]
    3. [/SIZE]
    This is poor code though as there is no error checking (the user could enter anything), and atoi will convert to an int and you are truncating to an unsigned char. This works for decimal input format, if you expect the input to be in hex format (0x...) then you need to investigate further.
     
    TBayBoy likes this.
  4. TBayBoy

    Thread Starter Member

    May 25, 2011
    148
    19
    The final solution was (entered value was checked for errors proir to being passed back here):

    Code ( (Unknown Language)):
    1. [FONT=Consolas][SIZE=2][COLOR=#0000ff]
    2. [SIZE=2][FONT=Consolas][COLOR=#0000ff][SIZE=2][FONT=Consolas][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] CEL505_LabsDlg::OnBnClickedInput()[/COLOR]
    3. {
    4. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// TODO: Add your control notification handler code here
    5. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]CString sBuffer;[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]
    6. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]InputDlg input;
    7. input.m_sHexInput.Format( _T([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"%02X"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]),m_bNumber);
    8. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2](input.DoModal() ==IDOK)
    9. {
    10. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]
    11. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]sBuffer.Format(_T([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"%s"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]), input.m_sHexInput);
    12. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// convert the unicode to ASCII text
    13. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]CT2A text (input.m_sHexInput);
    14. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// cast the ASCII HEX text to BYTE
    15. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]m_bNumber = (BYTE) strtoul (text.m_szBuffer,NULL,16);
    16. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]//copy temp to global byte and update values.
    17. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]//m_bNumber=bTempNum;
    18. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]UpdateLEDs();
    19. UpdateNumericEquivalents();
    20. UpdateExternalLEDs();
    21.  
    22. [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// //MessageBox(sBuffer,_T("Custom Dialog Result"),MB_OK | MB_ICONINFORMATION);
    23. [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]}
    24. }
    25. [/SIZE][/FONT][/SIZE][/FONT][/COLOR][/FONT][/SIZE][/COLOR][/FONT][/SIZE][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2]
    26. [/SIZE][/FONT]
     
Loading...