[英]How to encode a Hex string to Integer in BCB XE
我想在RAD Studio C ++ Builder XE中將十六進制字符串轉換為16位十進制。
例如,我有一個十六進制字符串“ 8FC”。 其二進制表示形式為100011111100。其十進制表示形式為:2300。
如何在C ++ Builder XE中進行此轉換?
最后,我在本文上找到了正確的轉換方法。 它只是嘗試調用StrToInt()
過程,但是像這樣預先加上一個“ $ ”:
s1 = "8FC";
int i = StrToInt(UnicodeString("$") + s1);
Edit1->Text = IntToStr(i);
一種簡單的方法是使用std:stringstream
#include <ios>
#include <sstream>
#include <ostream>
#include <iostream> // MS & Borland seem to be deficient in requiring this
int main()
{
unsigned short val;
std::stringstream st("8FC");
st >> std::hex >> val;
// convert it back to text as decimal
st.clear();
st << std::dec << val;
std::cout << "Decimal value " << st.str() << std::endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.