auk_ie
asked on
Copy Hex from String
Hello
I have 6 Edit controls in which the user enters a hex value (for example the string "00" in edit 1 "e1" in edit 2 etc)
UCHAR szDestMac[6] = {NULL}; //This is the varaible in which I want to store all my hex numbers
//I get the text from the edit box as follows
TCHAR m_szDestMAC[3];
GetDlgItemText(IDC_EDIT_SR CMAC1, m_szDestMAC, sizeof(m_szDestMAC));
//So now m_szDestMAC = "00" and I want to copy this as a hex number into the first byte of my unicode array, szDestMac[0].
//I tried both the following
swprintf((TCHAR*)&szDestMa c[0], _T("%s"), m_szDestMAC);
//memcpy(&szDestMac[0], m_szDestMAC, 1);
//But the TRACE statment returns as "Got Source MAC from user: 30"
TRACE(_T("Got Source MAC from user: %02x\n"), szDestMac[0]);
Anyone know why szDestMac[0] contains the value 30 instead of what was typed in the edit control "00" and how to resolve the problem
Regards
I have 6 Edit controls in which the user enters a hex value (for example the string "00" in edit 1 "e1" in edit 2 etc)
UCHAR szDestMac[6] = {NULL}; //This is the varaible in which I want to store all my hex numbers
//I get the text from the edit box as follows
TCHAR m_szDestMAC[3];
GetDlgItemText(IDC_EDIT_SR
//So now m_szDestMAC = "00" and I want to copy this as a hex number into the first byte of my unicode array, szDestMac[0].
//I tried both the following
swprintf((TCHAR*)&szDestMa
//memcpy(&szDestMac[0], m_szDestMAC, 1);
//But the TRACE statment returns as "Got Source MAC from user: 30"
TRACE(_T("Got Source MAC from user: %02x\n"), szDestMac[0]);
Anyone know why szDestMac[0] contains the value 30 instead of what was typed in the edit control "00" and how to resolve the problem
Regards
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The code should be like this,
TCHAR szDestMac[6] = { 0 };
TCHAR m_szDestMAC[3];
GetDlgItemText(IDC_EDIT_SR CMAC1, m_szDestMAC, sizeof(m_szDestMAC) / sizeof(TCHAR));
_stprintf(szDestMac, _T("%s"), m_szDestMAC);
TRACE(_T("Got Source MAC from user: %c\n"), szDestMac[0]);
Note that the MFC documentation on CWnd::GetDlgItemText() is wrong.
"nMaxCount
Specifies the maximum length (in bytes) of the string to be copied to lpStr. If the string is longer than nMaxCount, it is truncated."
It should be in characters rather than bytes.
TCHAR szDestMac[6] = { 0 };
TCHAR m_szDestMAC[3];
GetDlgItemText(IDC_EDIT_SR
_stprintf(szDestMac, _T("%s"), m_szDestMAC);
TRACE(_T("Got Source MAC from user: %c\n"), szDestMac[0]);
Note that the MFC documentation on CWnd::GetDlgItemText() is wrong.
"nMaxCount
Specifies the maximum length (in bytes) of the string to be copied to lpStr. If the string is longer than nMaxCount, it is truncated."
It should be in characters rather than bytes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You want to use strtol() instead:
szDestMac[0] = (UCHAR) strtol(m_szDestMAC);