简体   繁体   中英

Using c++ is it possible to convert an Ascii character to Hex?

I have written a program that sets up a client/server TCP socket over which the user sends an integer value to the server through the use of a terminal interface. On the server side I am executing byte commands for which I need hex values stored in my array.

sprint(mychararray, %X, myintvalue); 

This code takes my integer and prints it as a hex value into a char array. The only problem is when I use that array to set my commands it registers as an ascii char. So for example if I send an integer equal to 3000 it is converted to 0x0BB8 and then stored as 'B''B''8' which corresponds to 42 42 38 in hex. I have looked all over the place for a solution, and have not been able to come up with one.

Finally came up with a solution to my problem. First I created an array and stored all hex values from 1 - 256 in it.

char m_list[256]; //array defined in class
m_list[0] = 0x00; //set first array index to zero
int count = 1; //count variable to step through the array and set members
while (count < 256)
{
    m_list[count] = m_list[count -1] + 0x01; //populate array with hex from 0x00 - 0xFF
    count++;
}

Next I created a function that lets me group my hex values into individual bytes and store into the array that will be processing my command.

void parse_input(char hex_array[], int i, char ans_array[])
{
    int n = 0;
    int j = 0;
    int idx = 0;
    string hex_values;
    while (n < i-1)
    {
        if (hex_array[n] = '\0')
        {
            hex_values = '0';
        }
        else
        {
            hex_values = hex_array[n];
        }
        if (hex_array[n+1] = '\0')
        {
            hex_values += '0';
        }
        else
        {
            hex_values += hex_array[n+1];
        }
        cout<<"This is the string being used in stoi: "<<hex_values; //statement for testing
        idx = stoul(hex_values, nullptr, 16);
        ans_array[j] = m_list[idx];
        n = n + 2;
        j++;
    }
}

This function will be called right after my previous code.

sprint(mychararray, %X, myintvalue); 
void parse_input(arrayA, size of arrayA, arrayB) 

Example: arrayA = 8byte char array, and arrayB is a 4byte char array. arrayA should be double the size of arrayB since you are taking two ascii values and making a byte pair. eg 'A' 'B' = 0xAB

While I was trying to understand your question I realized what you needed was more than a single variable. You needed a class, this is because you wished to have a string that represents the hex code to be printed out and also the number itself in the form of an unsigned 16 bit integer, which I deduced would be something like unsigned short int . So I created a class that did all this for you named hexset (I got the idea from bitset ), here:

#include <iostream>
#include <string>

class hexset {
 public:
     hexset(int num) {
         this->hexnum = (unsigned short int) num;
         this->hexstring = hexset::to_string(num);
    }

    unsigned short int get_hexnum() {return this->hexnum;}
    std::string get_hexstring() {return this->hexstring;}

 private:
    static std::string to_string(int decimal) {
        int length = int_length(decimal);
        std::string ret = "";
        for (int i = (length > 1 ? int_length(decimal) - 1 : length); i >= 0; i--) {
            ret = hex_arr[decimal%16]+ret;
            decimal /= 16;
        }
        if (ret[0] == '0') {
            ret = ret.substr(1,ret.length()-1);
        }
        return "0x"+ret;
    }

    static int int_length(int num) {
        int ret = 1;
        while (num > 10) {
            num/=10;
            ++ret;
        }
        return ret;
    }

    static constexpr char hex_arr[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    unsigned short int hexnum;
    std::string hexstring;
};

constexpr char hexset::hex_arr[16];

int main() {
    int number_from_file = 3000; // This number is in all forms technically, hex is just another way to represent this number.
    hexset hex(number_from_file);
    std::cout << hex.get_hexstring() << ' ' << hex.get_hexnum() << std::endl;
    return 0;
}

I assume you'll probably want to do some operator overloading to make it so you can add and subtract from this number or assign new numbers or do any kind of mathematical or bit shift operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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