¿Cómo se escriben los valores hexadecimales de un carácter en ASCII en un archivo de texto?

StackOverflow https://stackoverflow.com/questions/623784

  •  05-07-2019
  •  | 
  •  

Pregunta

Esto es lo que tengo actualmente hasta ahora:

void WriteHexToFile( std::ofstream &stream, void *ptr, int buflen, char *prefix )
{
    unsigned char *buf = (unsigned char*)ptr;

    for( int i = 0; i < buflen; ++i ) {
        if( i % 16 == 0 ) {
            stream << prefix;
        }

        stream << buf[i] << ' ';
    }
}

He intentado hacer stream.hex, stream.setf (std :: ios :: hex), así como buscar en Google un poco. También he intentado:

stream << stream.hex << (int)buf[i] << ' ';

Pero eso tampoco parece funcionar.

Este es un ejemplo de algunos resultados que produce actualmente:

Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 

Me gustaría que la salida se pareciera a la siguiente:

FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
¿Fue útil?

Solución

#include <iostream>
using namespace std;
int main() {
    char c = 123;
    cout << hex << int(c) << endl;
}

Editar: con relleno cero:

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    char c = 13;
    cout << hex << setw(2) << setfill('0') << int(c) << endl;
}

Otros consejos

char upperToHex(int byteVal)
{
    int i = (byteVal & 0xF0) >> 4;
    return nibbleToHex(i);
}

char lowerToHex(int byteVal)
{
    int i = (byteVal & 0x0F);
    return nibbleToHex(i);
}

char nibbleToHex(int nibble)
{
    const int ascii_zero = 48;
    const int ascii_a = 65;

    if((nibble >= 0) && (nibble <= 9))
    {
        return (char) (nibble + ascii_zero);
    }
    if((nibble >= 10) && (nibble <= 15))
    {
        return (char) (nibble - 10 + ascii_a);
    }
    return '?';
}

Más código aquí .

Prueba:

#include <iomanip>
....
stream << std::hex << static_cast<int>(buf[i]);

También puedes hacerlo usando algo un poco más antiguo:

char buffer[3];//room for 2 hex digits and \0
sprintf(buffer,"%02X ",onebyte);

Generalmente hago una función que devuelve los dígitos y simplemente la uso:

void CharToHex(char c, char *Hex)
{
   Hex[0]=HexDigit(c>>4);
   Hex[1]=HexDigit(c&0xF);
}

char HexDigit(char c)
{
   if(c<10)
     return c;
   else
     return c-10+'A';
}

CHAR a wchar_t (Unicode) cadena HEX

wchar_t* CharToWstring(CHAR Character)
{
wchar_t TargetString[10];
swprintf_s(TargetString, L"%02X", Character);

// then cut off the extra characters
size_t Length = wcslen(TargetString);

wchar_t *r = new wchar_t[3];
r[0] = TargetString[Length-2];
r[1] = TargetString[Length-1];
r[2] = '\0';
return r;
}

Simplemente necesitas configurar tu flujo una vez:

stream << std::hex << std::setfill('0') << std::setw(2)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top