如何将cstring IP地址转换为DWORD? [英] How to convert cstring IP address to DWORD?

查看:160
本文介绍了如何将cstring IP地址转换为DWORD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将IpAddress转换为dword变量...



char IpAddress [4]; //表示255.255.255.255

DWORD dwIPAddress;

CString CstringIP = _T();



CstringIP.Format(%u%u%u%u,IpAddress [0],IpAddress [1],IpAddress [2],IpAddress [3]);



所以我从IpAddress值得到CString IP地址。



但接下来,

我不知道这个CstringIP到dwIPAddress。 br />
谁知道方法?





还有其他更方便的方法吗?



我尝试了什么:



这个问题浪费了3个多小时......

解决方案

从理论上讲,你可以强制转换它 - 但这样做有风险,因为char数组或char指针不必与地址边界对齐,而DWORD也是如此。并且你不应该使用char,因为它是一个有符号的值 - 你真的想要字节。

所以我这样做:

 DWORD DwordFromBytes(byte * b)
{
return (b [ 0 ])| (b [ 1 ]<< 8 )| (b [ 2 ]<< 16 )| (b [ 3 ]<< 24 );
}


您必须解析字符串以获取四个元素,将这些元素转换为数字,并将数字组合为DWORD(假设为IPv4)地址用虚线表示法;你的 CString.Format 示例错过了它中的句号。)



有各种方法执行解析:

使用 sscanf,_sscanf_l,swscanf,_swscanf_l [ ^ ]



 无符号字节[ 4  ]。 
int fields = _stscanf(str.GetString(),_ T( %u。%u。%u。%u),
bytes + 3,bytes + 2,bytes + 1,bytes);
// 错误检查:如果fields == 4且所有bytes []值有效(< 256)



使用 strtoul,_strtoul_l,wcstoul,_wcstoul_l [ ^ ]



  unsigned   long  bytes [ 4 ]; 
LPCTSTR parse = str.GetString();
LPTSTR endptr;
int i;
for (i = 3 ; parse!= NULL&& i> = 0 ; i--)
{
bytes [i] = _tcstoul(parse,& endptr, 10 );
// endptr指向停止转换的字符
// 当这是一段时间后,在其后面的字符处继续
if (* endptr == _T(' 。'))
parse = endptr + 1 ;
else
parse = NULL;
}
// 错误检查:如果i< 0和所有bytes []值有效(< 256)





除上述示例外,还有更多功能可用于解析字符串,如 CStringT :: Tokenize [ ^ ]和正则表达式(< regex> [ ^ ])。



最后将四个数字合并为一个DWORD:

 DWORD dwIP = bytes [ 0 ] +(bytes [ 1 ]<<  8 )+(bytes [ 2 ]<<  16 )+(bytes [ 3 ]<<  24 ); 


Hi,
I want to convert IpAddress to dword variable...

char IpAddress[4]; // means 255.255.255.255
DWORD dwIPAddress;
CString CstringIP = _T("");

CstringIP.Format("%u%u%u%u",IpAddress[0],IpAddress[1], IpAddress[2], IpAddress[3]);

So I get the CString IP Address from IpAddress values.

But next,
I have no idea this CstringIP to dwIPAddress.
Who know the method ?

or
Any other easier method?

What I have tried:

3 more hours wasted on this problem...

解决方案

In theory, you can just cast it - but that's risky, because a char array or char pointer doesn't have to be aligned to an address boundary, and a DWORD does. And you shouldn't use char anyway, as it's a signed value - you really want byte instead.
So I'd do this:

DWORD DwordFromBytes(byte *b)
    {
    return (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
    }


You must parse the string to get the four elements, convert these to numbers, and combine the numbers to a DWORD (assuming an IPv4 address in dotted notation; your CString.Format example misses the periods in it).

There are various methods to perform the parsing:

Using sscanf, _sscanf_l, swscanf, _swscanf_l[^]


unsigned bytes[4];
int fields = _stscanf(str.GetString(), _T("%u.%u.%u.%u"), 
    bytes+3, bytes+2, bytes+1, bytes);
// Error check: Valid if fields == 4 and all bytes[] values are valid (< 256)


Using strtoul, _strtoul_l, wcstoul, _wcstoul_l[^]


unsigned long bytes[4];
LPCTSTR parse = str.GetString();
LPTSTR endptr;
int i;
for (i = 3; parse != NULL && i >= 0; i--)
{
    bytes[i] = _tcstoul(parse, &endptr, 10);
    // endptr points to the character that stops the conversion
    // When this is a period, resume at the character behind it
    if (*endptr == _T('.'))
        parse = endptr + 1;
    else
        parse = NULL;
}
// Error check: Valid if i < 0 and all bytes[] values are valid (< 256)



Besides the above examples there are more functions that can be used to parse the string like CStringT::Tokenize[^] and regular expressions (<regex>[^]).

Finally combine the four numbers to a DWORD:

DWORD dwIP = bytes[0] + (bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24);


这篇关于如何将cstring IP地址转换为DWORD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆