Ziema
Ziema

Reputation: 45

Converting memory hex string to an int

I'm working on serial port communication and I have some info in a bat file which is encoded. I need to extract the file size which I translated to hex but it's flipped(something to do with memory) and i need to get the correct size.

Here is the hex I have in my bat file(converted to decimal it's : 1178534144) So I'm having alot of problems converting it...

and here is the hex number I need to get(int decimal it's 81734)

**EDIT

Here's 64 bytes out of the bat file which I converted to hex cause in ASCII it's unreadable. Focus on the part marked with red(whole hex) and part in blue(it's the hex number I need to convert from 46 3f 01 00 to 0013f46

Upvotes: 0

Views: 287

Answers (2)

om77
om77

Reputation: 36

The decimal number 1178534144 is 0x463F0100. To get decimal 81734 you need to rotate 4 bytes to get 0x00013F46. Under Windows you can include winsock.h and use function ntohl. https://learn.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-ntohl

Upvotes: 0

fubo
fubo

Reputation: 45947

Use Convert.ToInt32-Methode: (String, Int32) with the base as parameter

The base of the number in value, which must be 2, 8, 10, or 16.

So the code would be (16 for base 16 aka hex)

int result = Convert.ToInt32("463F0100", 16); // 1178534144

Upvotes: 1

Related Questions