|
Hi all,
I'm a complete novice to C++.net and I'm in a spot of bother. I have some values that require certain calculations and conversions which I have no problem with at all. My problem arises when I try to store the values as hex strings. So, for example, I have a byte holding the value (in dec) 252. I want to hold the value as its hex equivalent in a string which will later be displayed in a textbox.
Does vs.net provide any built in functions for this task or, if not, how would I go about the conversion manually?
Any help gratefully recieved.
|
|
|
|
|
char c = 252;
CString str;
str.Format("0x%X", c);
there, str contains the string "0xFC"
you could also use sprintf() and so on...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
I tried that already and just get the following error,
error C2664: 'System::String __gc *System::String::Format(System::String __gc *,System::Object __gc *)' : cannot convert parameter 2 from 'int' to 'System::Object __gc *'
Conversion from a built-in type or a value type to 'System::Object __gc*' requires boxing
|
|
|
|
|
oh, your programming managed code.
well, what about this ?
char c = 252;
char str[100];
ssprintf(str, "0x%X", c);
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
What I need to do is store the hex value as a string so I can set a textbox text element to that string. So I'm looking to do something like
<br />
System::Byte b = 252;<br />
System::String* s = b (formatted as hex value);<br />
then
<br />
textBox1->Text = s;<br />
Does any of that make sense??
|
|
|
|
|
toxcct wrote:
ssprintf(str, "0x%X", c);
Shouldn't that be:
sprintf(str, "%#X", c);
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
for ssprintf -> sprintf, yes, i made a mistake
for 0x%X -> %#X, it is as you prefer...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Hi Dear,
Just do the following
byte _val;
char _data[4]="";
sscanf(_data,"%x",&_val);
Regards
~Kid
|
|
|
|
|
itkid wrote:
sscanf(_data,"%x",&_val);
Perhaps you meant sprintf() instead of sscanf() .
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Sorry still not working, getting the following,
error C3861: 'sprintf': identifier not found, even with argument-dependent lookup
if I use sscanf, same error with obvious replacements.
|
|
|
|
|
The sprintf() function is part of stdio.h. I do not know if that set of APIs is available with VS.Net.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Can't seem to get it to work at all tbh. But eh, what do I know?
Surely .net must provide some way to format a string to dispay value in hex. Or you would think so!!
|
|
|
|
|
|
Christian Graus might have a few articles in this regard.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I'll give it a go.
Cheers everyone for the help!
|
|
|
|
|
Hi,
I have to create a CMap and store N number of (CString,int)
and i want to look up using CString and return value would be int.
If any body have gone through this requirement pls let me know.
i am new to CMap.If anyother method exist also welcome.
regards
Chezhian
|
|
|
|
|
<br />
#include <afxtempl.h><br />
<br />
CMap<CString, LPCTSTR, int, int> myMap;<br />
<br />
myMap.SetAt("one", 1);<br />
myMap.SetAt("two", 2);<br />
myMap.SetAt("three", 3);<br />
<br />
int nValue = 0;<br />
if (myMap.Lookup("two", nValue))<br />
{<br />
TRACE(_T("value is %d\n"), nValue);<br />
}<br />
Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
|
|
|
|
|
hi,
I tried this.
It gives the following errors
'myMap':UnKnown size
'myMap':Cannot be destroyed
'CMap':no appropriate default constructor available
Is any header has to include for this.
I am using vc++ 7.1 version?
Regards
Chezhian
|
|
|
|
|
rgchezhian wrote:
Is any header has to include for this.
How about afxtempl.h .
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
It doesn't give any change in error
|
|
|
|
|
I copied the code into 7.1 and it compiled fine.
I don't get those errors even if I don't include afxtempl.h. It almost sounds like you've created your own CMap class within the project. If so, that would be a problem.
Here's what I did to test the code:
1) File -> New -> VC++ Projects -> Win32 -> OK
2) Application Type = Console Application
3) Make sure Add support for MFC is checked
4) Finish
5) Add the #include <afxtempl.h> statement below stdafx.h
6) Add CMap code to _tmain
Compiles fine.
Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
|
|
|
|
|
i followed the same sequence and declared CMap variable.
CMap myMap;
But still it shows the following error
'myMap':UnKnown size
'myMap':Cannot be destroyed
'CMap':no appropriate default constructor available
'CMap' : use of class template requires template argument list
|
|
|
|
|
The map class is templated, so you need to declare the key and value types in the declaration.
CMap<CString, LPCTSTR, int, int> myMap;
-> maps string keys to integer values
CMap<BYTE, BYTE, void*, void*> myMap;
-> maps BYTE keys to void pointer values...
CMap<int, int, CPoint, CPoint> myMap;
-> maps int keys to CPoint values
etc.
More info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_MFC_CMap.asp
Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
|
|
|
|
|
I forgot to format the angled brackets in my post, causing the #include statement to screw up.
David posted the correct #include
Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
|
|
|
|
|
but i didn't inclucde #include
But it shows error
|
|
|
|