[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Convert multibyte Hex string to LONG

Posted on 2001-06-26
12
Medium Priority
?
371 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
[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
  • 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

650 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