Question

I want to convert a hex string to a 16 bit Decimal in RAD Studio C++ Builder XE.

For example, I have the hex string "8FC". The Binary representation of this is 100011111100. The Decimal representation of this is: 2300.

How to do this conversion in C++ Builder XE?

Was it helpful?

Solution

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:

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

OTHER TIPS

One easy way is to use 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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top