• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Convert multibyte Hex string to LONG

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
rmillerSST
Asked:
rmillerSST
  • 4
  • 3
  • 2
  • +3
1 Solution
 
djbusychildCommented:
strtol bascially takes a buffer ( in your cas a buffer of hex string)

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

0
 
jhanceCommented:
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
 
peterchen092700Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
djbusychildCommented:
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
 
peterchen092700Commented:
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
 
djbusychildCommented:
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
 
rmillerSSTAuthor Commented:
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
 
peterchen092700Commented:
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
 
rmillerSSTAuthor Commented:
It's an MFC ActiveX
0
 
peterchen092700Commented:
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
 
griesshCommented:
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
 
ComTechCommented:
Accepted recommended answer,


Regards,
ComTech
CS Admin @ EE
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now