我已经看到了很多这方面的答案,但我似乎无法开始工作。我想我在变量类型之间感到困惑。我有一个来自NetworkStream的输入,它将一个十六进制代码放入一个String ^中。我需要接受这个字符串的一部分,将其转换为数字(可能是int),这样我就可以添加一些arithemetic,然后在表单上输出reult。我到目前为止的代码:
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");
对此的任何帮助将不胜感激 克里斯
答案 0 :(得分:1)
这有帮助吗?
String^ hex = L"5A";
int converted = System::Convert::ToInt32(hex, 16);
转换静态方法的documentation位于MSDN上。
您需要停止考虑将标准C ++库与托管类型一起使用。 .Net BCL非常好......
答案 1 :(得分:0)
希望这会有所帮助:
/*
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();
}