Converting an int to a 2 byte hex value in C

后端 未结 15 1402
不知归路
不知归路 2020-12-05 18:17

I need to convert an int to a 2 byte hex value to store in a char array, in C. How can I do this?

15条回答
  •  無奈伤痛
    2020-12-05 18:53

    For dynamic fixed length of hex

    string makeFixedLengthHex(const int i, const int length)
    {
        std::ostringstream ostr;
        std::stringstream stream;
        stream << std::hex << i;
        ostr << std::setfill('0') << std::setw(length) << stream.str();
        return ostr.str();
    }
    

    But negative you have to handle it yourself.

提交回复
热议问题