Decimal to Hexadecimal Functions in C++/CLI

2019-07-24 21:51发布

问题:

I have a program that communicates to a device through the serial port. I am having difficulties with one section however. I am getting what looks like a right angle symbol when I run through the calculated hexadecimal commands. Here are the two sections of code (main section and function).

Main Section:

private: System::Void poll_Click(System::Object^  sender, System::EventArgs^  e)
     {
            int i, end;
            double a = 1.58730159;
            String^ portscan = "port";
            String^ translate;
            std::string portresponse [65];
            std::fill_n(portresponse, 65, "Z");

            for (i=0;i<63;i++)
            {
                if(this->_serialPort->IsOpen)
                {
                    // Command 0 generator
                    int y = 2;
                    y += i;
                    std::string command0[10] = {"0xFF", "0xFF", "0xFF", "0xFF", "0xFF", "0x02", dectohex(i), "0x00", "0x00", dectohex(y)};

                    // The two "dectohex" values above is where I get the odd symbol

                    for (end=0;end<10;end++)
                    {
                        portscan = marshal_as<String^>( command0[end] );
                        this->_serialPort->WriteLine(portscan);
                    }

                    translate = (this->_serialPort->ReadLine());
                    MarshalString(translate, portresponse [i]);
                    if(portresponse [i] != "Z")
                    {
                        comboBox7->Items->Add(i);
                    }
                    this->progressBar1->Value=a;
                    a += 1.58730159;
                }
            }


     }

Function:

string dectohex(int i)
     {
        string hexidecimal = "";
        char hex_string[10] = "";
        hexidecimal = sprintf (hex_string, "0x%02X", i);
        return hexidecimal;
     }

I am assuming that there is something fairly simple that I am missing. Any and all help is appreciated.


The solution: Per David Yaw's guidance.

    string dectohex(int i)
         {
            char hex_array[10];
            sprintf (hex_array, "0x%02X", i);
            string hex_string(hex_array);
            return string(hex_string);
         }

回答1:

The return value of sprintf isn't a string, as you have coded currently. It's the number of characters written to the char* that was passed in as the first parameter. Use the character buffer that you passed in, and convert the char* to a string object.

string dectohex(int i)
{
    char hex_string[10];
    sprintf (hex_string, "0x%02X", i);
    return string(hex_string);
}


回答2:

Don't use sprintf ....
Use a stringstream instead.

#include <sstream>

string dectohex(int i)
{
    stringstream str;
    str << std::hex << i;
    return str.str();
}

To get the exact same text as you had before:

 str << "0x" << std::setfill('0') << std::setw(2) << std::hex << i;


标签: c++ c++-cli