开发者

Convert hex to int

I've seen lots of answers to this, but I cannot seem to get any to work. I think I'm getting confused between variable types. I have an input from NetworkStream that is put a hex code into a String^. I need to take part of this string, convert it to a number (presumably int) so I can add some arith开发者_StackOverflow社区emetic, then output the reult on the form. The code I have so far:

String^ msg; // gets filled later, e.g. with "A55A6B0550000000FFFBDE0030C8"
String^ test;

//I have selected the relevant part of the string, e.g. 5A
test = msg->Substring(2, 2);

//I have tried many different routes to extract the numverical value of the
//substring. Below are some of them:

std::stringstream ss;
hexInt = 0;
//Works if test is string, not String^ but then I can't output it later.
ss << sscanf(test.c_str(), "%x", &hexInt);

//--------
sprintf(&hexInt, "%d", test);

//--------
//And a few others that I've deleted after they don't work at all.

//Output:
this->textBox1->AppendText("Display numerical value after a bit of math");

Any help with this would be greatly appreciated.

Chris


Does this help?

String^ hex = L"5A";
int converted = System::Convert::ToInt32(hex, 16);

The documentation for the Convert static method used is on the MSDN.

You need to stop thinking about using the standard C++ library with managed types. The .Net BCL is really very good...


Hope this helps:

/*
the method demonstrates converting hexadecimal values,
which are broken into low and high bytes.
*/
int main(){ 
//character buffer
char buf[1];
buf[0]= 0x06; //buffer initialized to some hex value
buf[1]= 0xAE; //buffer initialized to some hex value
int number=0;  

//number generated by binary shift of high byte and its OR with low byte
number = 0xFFFF&((buf[1]<<8)|buf[0]);

printf("%x",number);              //this prints AE06
printf(“%d”,number);              //this prints the integer equivalent

getch();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜