-3

I have an integer vector.

vector <int> myvect;

I want to convert vector data into hex format and store it into an char array.

How do I convert an integer value into hex in form of (0x..) and store it into char buffer?

Biffen
  • 6,249
  • 6
  • 28
  • 36

3 Answers3

2

You can use such function to convert any type to hex formatted std::string, which in turn you can convert and store to anything you like.

Templated function and it's depending includes:

#include <string>
#include <sstream>
#include <iomanip>

template<typename T>
typename std::enable_if<std::is_integral<T>::value, std::string>::type         
toHex(const T& value) {
    std::stringstream convertingStream;
    convertingStream << "0x" << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << value;
    return convertingStream.str();
}

And function usage:

#include <vector>

int main() {
    std::vector<int> myvect;
    myvect.push_back(2);
    myvect.push_back(33);
    myvect.push_back(66);
    myvect.push_back(99);
    myvect.push_back(-1);
    myvect.push_back(-1024);

    std::vector<std::string> mybuff;

    for (const auto& integer : myvect) {
        mybuff.push_back(toHex(integer));
        printf("%s\n", (*mybuff.rbegin()).c_str());
    }
    return 0;
}

But i strongly suggest that in the future you look for similar questions and try to compose a code, if you still don't know how to solve the problem, then post a new question.

0

try this out.

#include <iostream>
#include <iomanip>

int main()
{
    int input ;
    std::cout << "Enter decimal number: " ;
    std::cin >> input ;
    std::cout << "0x" << std::hex << input << '\n' ;
}
Hiren Jungi
  • 854
  • 8
  • 20
0

I know the user asked for storing it in a char array, however I chose to use an std::string instead for simplicity. Have a look at this function just to see what it is doing.

#include <string>
#include <vector>
#include <stringstream>
#include <iostream>


std::vector<std::string> decimalToHex( std::vector<unsigned> decimalValues ) {
    std::vector<std::string> output;
    for each ( auto value in decimalValues ) {
        std::stringstream hex;
        hex << "0x";
        if ( (value % 16) == 0 ) {
            hex << "0";
            hex << std::hex << decimalValues[value];
        } else {
            hex << std::hex << decimalValues[value];
        }
        output.push_back( hex.str() );
    }
    return output;
}

int main() {
    std::vector<unsigned values>;
    for ( unsigned u = 0; u < 256; u++ ) {
        values.push_back( u );
    }

    std::vector<std::string> results = decimalToHex( values );

    for ( unsigned u = 0; u < results.size(); ++u ) {
        std::cout << results[u] << std::endl;
    }

    return 0;
}

The above function is taking a vector of unsigned integer values and for each value it is converting it to a hex value by using the bit shift or insertion operators on the stringstream class object. It pre-appends the "0x" before all values, and if the value is <= 15 it also pre-appends a 0 for better formatting otherwise it will just fill in the values. This will only work up to 255 or 0xff for the 2 column formatting since this only accounts for 2 columns in the hex numbers, but the function will still convert numbers that exceed 255 or 0xff. This will work for as many digits as you need as long as the data types can support such a number. Now as for saving it into a char array I'm sure you should be able to convert a std::string to a char array.

In the main where the vector of unsigned integers are being populated you can change that to u < 65536 and watch the function work its magic. The last output should be 0xffff which should be correct because 65,535 in decimal is equal to 0xffff in hex.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59