|
|
Hi I am devloping an application in MFC
I need to convert UCAHR pointer to Cstring and vice versa
such as
UCHAR *pMsg;
pMsg[0] = '1';
pMsg[1] = '2';
pMsg[3] = '3';
pMsg[4] = NULL
to CString and viceversa...
Can anybody help.
|
|
|
|
|
uchar * to cstring?
CString ps;
UCHAR *pMsg=new UCHAR[4];
pMsg[0] = '1';
pMsg[1] = '2';
pMsg[2] = '3';
pMsg[3] = '\0';
ps=CString(pMsg);
|
|
|
|
|
reddy07 wrote: UCHAR *pMsg;
pMsg[0] = '1';
pMsg[1] = '2';
pMsg[3] = '3';
pMsg[4] = NULL
As it stands, there is a access violation on your horizon.
Anyway the CString constructor is probably able to handle your unsigned char array, for instance try
CString s((char*) pMsg);
On the other hand, if you need to convert the CString value to an array of unsigned char s, then the effort required may depend on the type of build you are doing, UNICODE or not.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hello!
If you wanted a deeper understanding of the topic, I'll recommend you this article: CString Management[^].
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Thank you guys, it was very helpful.
|
|
|
|
|
I'm using WMI to get the hardware configuration of a machine. It all works fine but there is something that is bothering me. If, for example I want to find out how much RAM was in a machine I would look in Win32_PhysicalMemory and get the Capacity property, this then gives me back a string. Is there any way to get an actual number out of it?
If I have:
CIMTYPE ableugh;
if(classObj)
{
BSTR strClassProp = StringToBSTR("Capacity");
classObj->Get(strClassProp, 0, &v, &ableugh, 0);
SysFreeString(strClassProp);
}
My expertly named variable ableugh says that the information is a CIM_UINT64 which isn't too far from the truth, but the VARIANT still just contains a BSTR . I can of course just convert the string, but if there's another way I think I would prefer it.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
What does v.ullVal contain?
ULONGLONG ui64Val = v.ullVal;
Best Wishes,
-David Delaune
|
|
|
|
|
I checked most of the numerical values in the VARIANT , a 1Gig stick of ram should be 1073741824, the values contained within ullVal , uiVal , iVal , lVal etc. all contained something like 48756. I don't remember the exact number.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
See if this works for you:
...
VARIANT v;
CIMTYPE ableugh;
hr = classObj->Get(L"Capacity", 0, &v, &ableugh, 0);
VariantChangeType(&v, &v, 0, VT_UI8);
ULONGLONG ui64Val = v.ullVal;
...
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks for the help, VariantChangeType seems to be what I need. All I have to check for now is if the variant is NULL. CIMTYPE might come out as a string, but the variant could still be NULL, hr is okay and VariantChangeType doesn't complain, but when you try to get the BSTR kaboom!
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
VariantChangeType[^] - just a string conversion under the hood, but you don't have to do the work yourself!
Use
::VariantChangeType(&ableugh, &ableugh, 0, VT_UI8);
and then V_UI8(&ableugh) should yield the result you want.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
That works out pretty good, thanks.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Hello,
previously i have posted in wrong forum.
Problem is - User can input any number, program need to take decision based on inputs.
Let suppose input is stored in variable n;
based on value of n some function will be called.
program logic can be -
if(n > 1 && n < 10 )
callfun1();
if(n > 11 && n < 20 )
callfun2();
if(n > 25 && n < 30 )
callfun3();
if(n > 33 && n < 38 )
callfun4();
if(n > 1 && n < 10 )
callfun5();
.
.
.
and so on
if(n > minlimit && n < maxlimit )
callfunX();
Is there any easy way to avoid if else chain to do similar work.
Or is there any way to change the limits in if condition at some central place something using #define.
Please provide your inputs/help to help me finding other ways to optimize programming.
Thank
Vikas
vicky
|
|
|
|
|
You could arrange them in such a way that they effectively do a binary search. That wouldn't bring the total number of tests down but it would run faster (singe every time execution flows through it it can only take 1 path, so O(log n) steps instead of O(n))
Or you could fill a giant array with function pointers for O(1) time.
Or maybe you could change the functions to not need to be selected anymore (using funny bit magic if necessary) but whether that's possible depends on the function of course.
|
|
|
|
|
a little help is the use of the keyword else
if(n > 1 && n < 10 )
{
callfun1();
}
else if(n > 11 && n < 20 )
....
and sort it so that the most likely cases coming first.
AFAIK the compiler resolves switch in a similar way.
Best is the use of enums.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
You ignored the responses you got on this question the first time. Reposting is rude. Perhaps you can explain why you ignored the previous responses and still reposted the same question.
|
|
|
|
|
I am very sorry for my re-post.
I have re-posted my question because at other discussion thread i was getting responses in terms of C# and .Net, but i need response in terms of pure C.
I have not done it intentionally, because of web page movement by mistake i've clicked at C# discussion link and posted the question instead of C/C++ category.
Thank
Vikas
vicky
|
|
|
|
|
vikasvds wrote: I am very sorry for my re-post.
Ok, but have you begun to understand that OOD/OOP has solutions to writing conditional code?
|
|
|
|
|
You probably need to take a step back, and think about what you're trying to achieve, and lay it out in a more logical pattern. Perhaps you can convert the value 'n' into a unique parameter for a switch statement.
There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
There are three kinds of people in the world - those who can count and those who can't...
|
|
|
|
|
molesworth wrote: There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
Here are some quotes from Kent Beck in his book Implementation Patterns[^]
If/then and switch statements are the simplest form of instance –specific behavior….
The more paths through a program the less likely the program is to be correct….The proliferation of conditionals reduces reliability….This problem is compounded when conditionals are duplicated.
These problems can all be eliminated by converting the conditional logic to messages, either with subclasses or delegation.
|
|
|
|
|
led mike wrote: The more paths through a program the less likely the program is to be correct
Aye, that's very true, and anything that can reduce complexity will also reduce the likelihood of bugs, and improve readability and, more importantly, testability.
However, sometimes there's no way to improve on an if/else construct, and when used sensibly they're the right tools for the job. When you get into a morass of nested if/elseif/elseif... blocks, then you certainly do introduce problems.
Sheesh! Next thing you'll be telling me "goto" is a bad idea
There are three kinds of people in the world - those who can count and those who can't...
|
|
|
|
|
I wouldn't subclass a if .
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Why not? It's simple
class MyIf : public if
{
};
IT'S FRIDAY!!!!!
|
|
|
|
|
And now saturday here :pizza: :whisky:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|