Solved

Convert multibyte Hex string to LONG

Posted on 2001-06-26
12
363 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now