You need to convert a number from Little endian to big-endian .. Initially, a 32-bit number is given in decimal form .. You need to give a finite number, also in 10 form .. Suppose 3496683923 in 16 will look like D06B2993 .. We divide by bytes: D0 6B 29 93 .. Shift with respect to the ends .. 93 29 6B D0. We collect back the whole thing 93296BD0 and translate into 10 view: 2468965328.

If there is a simpler way, can you please explain? There is a sketch of the code that converts to the 16th system .. But I have no idea how to get this data from there and convert it to 10cc. (code is not mine, do not carp too much)

#include <iostream> #include <sstream> #include <string.h> using namespace std; int main() { string normal; int const Value = 3496683923; unsigned char const *pByte = (unsigned char const *)&Value; for (size_t i = 0; i < sizeof (Value); ++i) { cout << hex<< static_cast<int>(pByte[i]) << endl; } cout << endl; return 0; } 

    4 answers 4

    There are many methods - the ntohl function, the ntohl assembler bswap , the packing of bytes in a new order through casting to a byte array ...

    If you push off from your cycle and use bit operations: Select the low byte of the source, copy to the low byte of the receiver.
    Shifting the source to the right (now the second byte will become younger)
    and the receiver to the left (now the low byte will be the second).
    We continue until the initial low byte rises to the highest place.

      int main() { unsigned int Value = 3496683923; unsigned int res = 0; std::cout << Value << " " << std::hex << Value << std::endl; for (size_t i = 0; i < sizeof(Value); ++i) { res = (res << 8) | (Value & 0xFF); Value >>= 8; } std::cout << std::dec << res << " " << std::hex << res; } 3496683923 d06b2993 2468965328 93296bd0 

    Loop loop option that does not require const failure

      { res = (res << 8) | ((Value >> 8 * i) & 0xFF); } 
    • And will be displayed in what form? hex? dec? - tebenkov2222
    • 2
      A number will be received. In what form to output it depends on the output function. - MBo
    • Thanks for the way, nooo .. The number of output is not quite correct (93939393 in hex) .. And can you please explain the meaning of this line? Value >> = 8; - tebenkov2222
    • bitwise shift 8 bits to the right. I'll check now. - MBo
    • the compiler at me swears on this line: main.cpp: 17: 15: error: assignment of variable; variable 'Value' - tebenkov2222

    For example, you can use this function:

     unsigned int bswap (unsigned int v) { return (v >> 24) | ((v >> 8) & 0xff00) | ((v << 8) & 0xff0000) | (v << 24); } 

    Note that the type of the argument is unsigned .

    However, if you insist on the int type, both for the argument and for the result, it is enough to result in unsigned only in one place -
    return ((unsigned)v >> 24) | ....

      Conversion between little-endian and big-endian implies, by definition, a reversal of the byte order. This topic has nothing to do with any "number systems" at all.

      This can be done in a variety of ways ... For example, in terms of the C language

       uint32_t little_endian = 3496683923; union { uint32_t v; uint8_t b[4]; } u = { little_endian }; uint8_t t = ub[0]; ub[0] = ub[3]; ub[3] = t; t = ub[1]; ub[1] = ub[2]; ub[2] = t; uint32_t big_endian = uv; printf("%" PRIu32 "\n", big_endian); 

        You can use boost.endian :

         #include <boost/endian/conversion.hpp> #include <iostream> #include <cstdint> int main() { ::std::int32_t value{}; ::std::cin >> value; ::boost::endian::endian_reverse_inplace(value); ::std::cout << value << ::std::endl; return 0; } 

        online compiler