Re: ULONG representation conversion



Yeah, looks promising, but this is indeed wrong as it has hidden
endianness bug - only works on little-endian architectures.
Not a big deal I guess if you only care about Windows on
IA32...

The correct code would be:

const char* pIP = (const char*)&uIP;
sprintf(szDottedString, "%d.%d.%d.%d", (int)pIP[0], (int)pIP[1],
(int)pIP[2], (int)pIP[3]);

Note that the constant in the sample code also assumes little-endian
architecture (thus the sample would appear to work), but for real values
the data is properly retrieved in network byte order.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Volodymyr Shcherbyna" <v_scherbina@xxxxxxxxxxxxxxx> wrote in message
news:%23sxxr2LPIHA.4480@xxxxxxxxxxxxxxxxxxxxxxx
I would do that in the following way:

{
ULONG uIP = 0x100007f; /// 127.0.0.1

int a = uIP & 0xFF;
int a1 = (uIP >> 8) & 0xFF;
int a2 = (uIP >> 16) & 0xFF;
int a3 = (uIP >> 24) & 0xFF;

char szDottedString[64] = {0};
sprintf(szDottedString, "%d.%d.%d.%d", a, a1, a2, a3);

}

--
Volodymyr
"Dan" <dan@xxxxxxxxx> wrote in message
news:eX6n$WLPIHA.3816@xxxxxxxxxxxxxxxxxxxxxxx
I have an IP address of some Internet PC in a ULONG representation and I
need a decimal dotted one. Is there a simple conversion code?

Regards

Dan






.