Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy Hex from String

Posted on 2004-03-22
5
Medium Priority
?
708 Views
Last Modified: 2008-02-01
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_SRCMAC1, 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*)&szDestMac[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
 
0
Comment
Question by:auk_ie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 23

Assisted Solution

by:chensu
chensu earned 200 total points
ID: 10651279
0x30 is the ASCII value of character '0'. If you want to print out the character,

TRACE(_T("Got Source MAC from user: %c\n"), szDestMac[0]);
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 10651307
Try

swprintf((TCHAR*)&szDestMac[0], _T("%S"), m_szDestMAC);  // note the capital 'S'

Are you sure you don't want to use sth. like

UCHAR szDestMac[7] = {0,0,0,0,0,0,0};

<loop from 0 to 6>
    TCHAR m_szDestMAC[3];
    GetDlgItemText(IDC_EDIT_SRCMAC1, m_szDestMAC, sizeof(m_szDestMAC));
    wchar_t wszTmp  [3];
    wsprintf ( wszTmp, "%S", m_szDestMac);
    wcscat ( szDestMac, wszTmp);
<end loop>





0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 10651327
swprintf() is not what you want you use as this point.
You want to use strtol() instead:

szDestMac[0] = (UCHAR) strtol(m_szDestMAC);


0
 
LVL 23

Expert Comment

by:chensu
ID: 10651395
The code should be like this,

TCHAR szDestMac[6] = { 0 };

TCHAR m_szDestMAC[3];
GetDlgItemText(IDC_EDIT_SRCMAC1, 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.
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 1600 total points
ID: 10651548
> szDestMac[0] = (UCHAR) strtol(m_szDestMAC);

Oops, my bad.  that should be:

szDestMac[0] = (UCHAR) strtol(m_szDestMAC, NULL, 16);

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question