1

Suppose we have a string s = "453acd0f". Now I want to do something like this:

uint32_t res = 0;
for (size_t i = 0; i < 8; ++i) {
    res |= ConvertCharToHexNumber(s[8 - i - 1]) << (i * 4);
}

But how I should do this if I have a uint32_t var variable which I read from a binary file?

Waqar
  • 8,558
  • 4
  • 35
  • 43
dasfex
  • 1,148
  • 2
  • 12
  • 30
  • 1
    `sizeof(uint32_t)` is usually `4`. Are you sure you want `char[8]`? Your title and code doesn't seem to match. The code seems to convert a `char[]` to an `uint32_t`. – Ted Lyngmo Jul 21 '20 at 12:46
  • 1
    Does this answer your question? [Converting an int into a 4 byte char array (C)](https://stackoverflow.com/questions/3784263/converting-an-int-into-a-4-byte-char-array-c) – Asteroids With Wings Jul 21 '20 at 12:48
  • 1
    @TedLyngmo a hexadecimal digit is half a byte. – eerorika Jul 21 '20 at 12:51
  • @eerorika Yes, but OP talked about a binary file too. I'm not sure which way the conversion is supposed to go. :) – Ted Lyngmo Jul 21 '20 at 12:53
  • @dasfex You show how you convert a string to `uint32_t`, and ask how to do this with a `uint32_t` variable. The question is unclear to me. – eerorika Jul 21 '20 at 12:55
  • 1
    Look up the standard (C++11 and later) function `std::stoul()` - specified in standard header ``. – Peter Jul 21 '20 at 12:58

3 Answers3

2

I believe iomanip and stream operations may help you.

#include <sstream>
#include <iomanip>


std::string s="453acd0f";
std::stringstream ss(s);
uint32_t res;
ss >> std::setbase(16) >> res;    // hex. string to int. res==1161481487

ss.str(string());
ss.clear();
s.clear();

ss << std::setbase(16) <<res;    // dec. int to hex. string.
s = ss.str();    // s==453acd0f
John Park
  • 1,644
  • 1
  • 12
  • 17
2

The simplest way to convert a 4 byte uint32_t into char[8]:

    uint32_t var = 100;
    char res[8]{};
    res[0] = (var >> 28) & 0XF;
    res[1] = (var >> 24) & 0XF;
    res[2] = (var >> 20) & 0XF;
    res[3] = (var >> 16) & 0XF;
    res[4] = (var >> 12) & 0XF;
    res[5] = (var >> 8)  & 0XF;
    res[6] = (var >> 4)  & 0XF;
    res[7] = (var >> 0)  & 0XF;

You can reduce this to a loop:

    int shiftby = 32;
    for (int i = 0; i < 8; i++) {
        res[i] = (var >> (shiftby -= 4)) & 0xF;
    }
Waqar
  • 8,558
  • 4
  • 35
  • 43
1

If the uint32 being read from the binary file is, in fact, a uint32 then it's not in any format aside from being interpreted as an unsigned 32-bit integer. In that case, you read in the number then convert it to a hexadecimal representation.

So for example:

std::ifstream ifs("file.dat", ios_base::binary);

char buffer[64];
uint32_t n;

ifs.read(buffer, sizeof(uint32_t));
memcpy(&n, buffer, sizeof(uint32_t));

std::cout << std::hex << n << endl;

There's more than one way to skin a cat here. You can read it directly:

uint32_t n = 0xABC123;

// Get string representation of n
std::stringstream ss;
ss << std::hex << n;

// Pull out string, copy ptr to char array if you like
std::string s = ss.str();

// s.c_str() for example, for the C style string
std::cout << s << std::endl;

I hope that answers the question, if it's reading the uint32_t as 4 bytes then displaying or saving the char* string hexadecimal representation.