user1545923
user1545923

Reputation:

Convert String to hex little endian format in C++

I have a string 2290348 I need it to display as ACF22200 (little endian)

Because the input 2290348 is passed in via a form in textbox, I have tried to read it as string (eg. this->textBox1->Text) and convert it to int (eg. Convert::ToInt32(this->textBox1->Text)). afterwhich, i converted it to hex via ToString("x") which i manage to get 22F2AC I appended 00 to 22F2AC and gotten 0022F2AC still as string now i'm stuck in converting 0022F2AC to ACF22200

Upvotes: 0

Views: 2359

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409206

While still an int, you could use e.g. htonl to convert it to "network" byte order.

#include <winsock2.h>

int main()
{
    unsigned int x = 0x22F2AC;
    printf("x = 0x%08x\n", x);
    printf("htonl(x) = 0x%08x\n", htonl(x));
    return 0;
}

The program above prints:

x = 0x0022f2ac
htonl(x) = 0xacf22200

Upvotes: 1

Related Questions