Solved

Convert multibyte Hex string to LONG

Posted on 2001-06-26
12
365 Views
Last Modified: 2012-06-27
I have an ActiveX control that sends values to a low level serial device with statements like...

readBuffer[0] = 0xC4010010

where "OxC4..." is the value in question.  I can convert the hex to string with sprintf()...  I use the text translation to set a property on the ActiveX to be read by the host program which is in Visual Basic.  The problem is that now I need to have the ActiveX take a string value back ("0xC4010002") and convert it back to hex to be written to the serial device.  I can't figure out how to convert it from string to Hex.  I have looked at functions like strtol and so forth, but I am really new to C++ and I can't seem to figure out the syntax.
someone suggested sscanf, but that appears to work only on single byte strings and I have to deal with 32 bit words

If someone could offer a really specific answer I'd be a happy man!

Thanks for your help, you guys are great.
0
Comment
Question by:rmillerSST
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6228115
strtol bascially takes a buffer ( in your cas a buffer of hex string)

strtol("0xff",NULL,16) will return 255

0
 
LVL 32

Expert Comment

by:jhance
ID: 6228132
If you have:

DWORD dwHex;
char *lpszHex = "0xABCDFE0";

Than you can do:

sscanf(lpszHex, "0x%x", dwHex);

and dwHex will have the value of 0xABCDFE0 in it.

0
 
LVL 7

Accepted Solution

by:
peterchen092700 earned 100 total points
ID: 6228290
The property is probably a BSTR (what do you write the ActiveX control with? MFC, ATL?), so you need to use

wchar_t * endPtr;
unsigned long value;
if (textproperty[0] != '0' || textproperty[1] != 'X') {
  // value does not start with 0x --> should return an error ?!
}

value = wcstoul( m_textproperty+2, &endPtr, 16);
// "+2" skips the "0x" characters

if (endPtr != NULL) {
  // there was a conversion error at position (endPtr-textProperty+2)
  // return error?
}

// otherwise, endPtr is "the" value

Peter

P.S. A design note: You should give your control two properties (one text, one long) to access the same value. The class should have as member only an unsigned long, and the text properties get & put functions should handle the conversion
(i.a.W. don't force the client to use strings, don't store the string in the control)




0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Expert Comment

by:djbusychild
ID: 6228402
well, if you're in Microsoft world, use the TCHASE equivalent of the functions if you want to be able to convert to ASCII mode for compiling under win95 or something.

I think the equivalent is _tcstol ... not sure.. you can look it up
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6228461
dj busy:
a) Visual Basic is a good indicator for Microsoft world ;)
b) _tcsto(u)l compiles as strto(u)l in ANSI, and wcsto(u)l in UNICODE builds. However, COM uses BSTR as strings (which are size-prefixed UNICODE strings). So you need to use wcsto(u)l even in ANSI builds.


Peter
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6228471
yeah, I know... I didn't mean to put a negative connotation to the sentence. --;

well, you could do OLE2T on it, no?

just thought I'd introduce TCHAR to this person in case he/she was interested.
0
 
LVL 1

Author Comment

by:rmillerSST
ID: 6231785
Thanks so far guys, but here's what I've found.
Peterchen's answer isn't working because the function is trying to convert my variable which is a type "CString".  Maybe the workaround would be to convert the CString to BSTR.  Is that possible?

Also, when I tried jhance's sscanf (I copied it precisely into a command button), dwHex always returns 0xcccccccc.

Sorry for my ignorance here but I'm going nuts trying to get this to work!
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6232259
well, if it's a CString (what library do you use for the ActiveX?), you should use
_TCHAR instead of wchar_t
_tcstoul instead of wcstoul

Peter
0
 
LVL 1

Author Comment

by:rmillerSST
ID: 6232383
It's an MFC ActiveX
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6234137
yep, I remember, MFC does convert the params to CString. So it's the TCHAR stuff, with the code above.

Did you try it?
0
 
LVL 11

Expert Comment

by:griessh
ID: 6820061
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "peterchen" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner

0
 

Expert Comment

by:ComTech
ID: 6837758
Accepted recommended answer,


Regards,
ComTech
CS Admin @ EE
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

803 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