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


i have written program sets client/server tcp socket on user sends integer value server through use of terminal interface. on server side executing byte commands need hex values stored in array.

sprint(mychararray, %x, myintvalue);  

this code takes integer , prints hex value char array. problem when use array set commands registers ascii char. example if send integer equal 3000 converted 0x0bb8 , stored 'b''b''8' corresponds 42 42 38 in hex. have looked on place solution, , have not been able come one.

finally came solution problem. first created array , stored hex values 1 - 256 in it.

char m_list[256]; //array defined in class m_list[0] = 0x00; //set first array index 0 int count = 1; //count variable step through array , set members while (count < 256) {     m_list[count] = m_list[count -1] + 0x01; //populate array hex 0x00 - 0xff     count++; } 

next created function lets me group hex values individual bytes , store array processing 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 string being used in stoi: "<<hex_values; //statement testing         idx = stoul(hex_values, nullptr, 16);         ans_array[j] = m_list[idx];         n = n + 2;         j++;     } } 

this function called right after previous code.

sprint(mychararray, %x, myintvalue);  void parse_input(arraya, size of arraya, arrayb)  

example: arraya = 8byte char array, , arrayb 4byte char array. arraya should double size of arrayb since taking 2 ascii values , making byte pair. e.g 'a' 'b' = 0xab

while trying understand question realized needed more single variable. needed class, because wished have string represents hex code printed out , number in form of unsigned 16 bit integer, deduced unsigned short int. created class did named hexset (i got idea 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 = "";         (int = (length > 1 ? int_length(decimal) - 1 : length); >= 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; // number in forms technically, hex way represent number.     hexset hex(number_from_file);     std::cout << hex.get_hexstring() << ' ' << hex.get_hexnum() << std::endl;     return 0; } 

i assume you'll want operator overloading make can add , subtract number or assign new numbers or kind of mathematical or bit shift operation.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -