I need to convert CString Hex ip address

Mar 17, 2016 at 12:46am
I am relatively new to programming and could use some assistance. I am reading in a CString of c0a85c45 which is an IP address. I need to break it down into its four parts and convert to decimal in order to send its data to a windows IP dialog box. I do not need to change the order of the Bytes. I have tried several solutions I have found on the web; all with poor results. Any solutions would be appreciated.

Mar 17, 2016 at 12:57am
I am going to say you could start by showing us some of your code, at least to the point where it seems to be working the way you expect. Then ask questions about where you get stuck.
Mar 17, 2016 at 1:03am
Ok, maybe I should start smaller now that you know what I am attempting. The part I am getting stuck on is how do I parse the Cstring into four values. From there I may be able to figure the rest out. Unfortunately I have deleted all the previous attempts. Most attempts using functions from the internet I end up with problems due to char type used with CString
Last edited on Mar 17, 2016 at 1:05am
Mar 17, 2016 at 2:38am
here is working code that I was able to get working. I am sure there are much easier ways to do this but it works for now.
Any comments would be appreciated

Anything with m_*** was declared earlier in code.

m_strKeyName = _T("locaddr");
m_strSectionName = _T("AnnexJ");
m_strIP = m_IniReader.getKeyValue(m_strKeyName, m_strSectionName);

CString a1 = m_strIP[0];
CString a2 = m_strIP[1];
CString b1 = a1 + a2;
m_strIPtext1 = b1;

CString a3 = m_strIP[2];
CString a4 = m_strIP[3];
CString b2 = a3 + a4;
m_strIPtext2 = b2;

CString a5 = m_strIP[4];
CString a6 = m_strIP[5];
CString b3 = a5 + a6;
m_strIPtext3 = b3;


CString a7 = m_strIP[6];
CString a8 = m_strIP[7];
CString b4 = a7 + a8;
m_strIPtext4 = b4;

long myNumber1 = strtol((LPCSTR)m_strIPtext1, NULL, 16);
long myNumber2 = strtol((LPCSTR)m_strIPtext2, NULL, 16);
long myNumber3 = strtol((LPCSTR)m_strIPtext3, NULL, 16);
long myNumber4 = strtol((LPCSTR)m_strIPtext4, NULL, 16);



nField0 = myNumber1;
nField1 = myNumber2;
nField2 = myNumber3;
nField3 = myNumber4;
m_strIP2 = LPARAM MAKEIPADDRESS(nField0, nField1, nField2, nField3);


Last edited on Mar 17, 2016 at 2:40am
Mar 17, 2016 at 3:06am
Found this
http://tricky.cz/how-to-convert-hexadecimal-number-to-ip-address/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
static char* hex_to_ip(const char *input)
{
    char *output = (char*)malloc(sizeof(char) * 16);
    unsigned int a, b, c, d;

    if (sscanf(input, "%2x%2x%2x%2x", &a, &b, &c, &d) != 4)
        return output;
    sprintf(output, "%u.%u.%u.%u", a, b, c, d);
    return output;
}


using namespace std;
int main () {
    
    cout << hex_to_ip("c0a85c45")<< endl;

    return 0;

}
Mar 17, 2016 at 3:32am
I found the same one and it gave issues with char type cast to CString. I am not familiar enough to know what that was about.
Thanks.
Any Idea what a better Conversion than"strtol" would be?
I get warnings because Windows control is looking for Byte, Or I could be confused about that.
Topic archived. No new replies allowed.