简体   繁体   English

如何在BCB XE中将十六进制字符串编码为整数

[英]How to encode a Hex string to Integer in BCB XE

I want to convert a hex string to a 16 bit Decimal in RAD Studio C++ Builder XE. 我想在RAD Studio C ++ Builder XE中将十六进制字符串转换为16位十进制。

For example, I have the hex string "8FC". 例如,我有一个十六进制字符串“ 8FC”。 The Binary representation of this is 100011111100. The Decimal representation of this is: 2300. 其二进制表示形式为100011111100。其十进制表示形式为:2300。

How to do this conversion in C++ Builder XE? 如何在C ++ Builder XE中进行此转换?

Finally, I find the correct way how to do this conversion on this article . 最后,我在本文上找到了正确的转换方法。 It just try to call the StrToInt() procedure but prepend a " $ " like this: 它只是尝试调用StrToInt()过程,但是像这样预先加上一个“ $ ”:

s1 = "8FC";
int i = StrToInt(UnicodeString("$") + s1);
Edit1->Text = IntToStr(i);

One easy way is to use std:stringstream 一种简单的方法是使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM