Convert an array of Integers to an array of Characters with hexadecimal format

1

I want to convert an integer array to an array of characters by formatting the integers in hexadecimal, then I leave an example of how I do it now, however, I would like an easier way to do it with sprintf, this is because when there are many values instead of two the sprintf function would be very long and poorly optimized:

int int_array[2];
char char_array[2];
unsigned char uchar_array1[2];
unsigned char uchar_array2[2];

uchar_array1[0] = 0x31;
uchar_array1[1] = 0x32;

uchar_array2[0] = 0x33;
uchar_array2[1] = 0x34;

int_array[0] = uchar_array1[0] ^ uchar_array2[0]; //Funcion XOR
int_array[1] = uchar_array1[1] ^ uchar_array2[1]; //Funcion XOR

sprintf(char_array, "%02X%02X", int_array[0], int_array[1]); //Función a Mejorar

I tried to do this:

for(int aux = 0; aux < 2; aux++)
{
    sprintf(char_array[aux], "%02X", int_array[aux]);
}

But he sends me the following error:

argument of type "char" is incompatible with parameter of type "char *"
    
asked by Noe Cano 23.10.2018 в 00:12
source

2 answers

3
  

I would like an easier way to do it with sprintf .

Well, forget the idea, sprintf belongs to the functions of C console writing, in C ++ the stream objects are used as std::cout , also the way to work with character strings in C ++ is using objects of type std::string not with formations 1 of characters.

Proposal.

Use a stream text :

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    int valores[]{250, 186, 218};
    std::stringstream ss;
    ss.width(2);
    ss.fill('0');

    for (int valor : valores)
        ss << std::hex << valor;

    std::cout << ss.str();

    return 0;
}

The above code shows the hexadecimal string fabada , you are using the std::hex to pass the received integer data to hexadecimal and save it in a stream text; The resulting stream can be asked for its current content as a string by using the str function % .

  • Also known as arrays or in English array .
  • answered by 23.10.2018 / 08:05
    source
    2

    Your program has several problems:

    • The representation of each character occupies two characters, then to store the representation of 2 characters you need a buffer minimum of 5 characters ... and you are using one of 2. You are stepping memory that belongs to other variables and that will sooner or later cause erratic behavior in the application.

    • It would be convenient to use std::string before char* . char* is working at very low level and you have to be very careful at all times ... std::string allows you to abstract from some problems.

    • The error is logical, sprintf expects a pointer to char as the first parameter and you are passing it a char .

    Your code, corrected, could look like this:

    int int_array[2];
    char char_array[5]; // <<--- Se incrementa el tamaño del buffer
    unsigned char uchar_array1[2];
    unsigned char uchar_array2[2];
    
    uchar_array1[0] = 0x31;
    uchar_array1[1] = 0x32;
    
    uchar_array2[0] = 0x33;
    uchar_array2[1] = 0x34;
    
    int_array[0] = uchar_array1[0] ^ uchar_array2[0]; //Funcion XOR
    int_array[1] = uchar_array1[1] ^ uchar_array2[1]; //Funcion XOR
    
    for(int aux = 0; aux < 2; aux++)
    {
      sprintf(&char_array[aux*2], "%02X", int_array[aux]);
      //      ^ Se le pasa una referencia (aqui puntero) al caracter que nos interesa
    }
    
        
    answered by 23.10.2018 в 10:13