Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Cast/Conversation

Cast/Conversation

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Irish_GUI
    wrote on last edited by
    #1

    I have an IP Address in CString format i need it in the BYTE format below. //BYTE localIp[] = {127,0,0,1}; could anyone show me how to do this? Thanks

    D T A 3 Replies Last reply
    0
    • I Irish_GUI

      I have an IP Address in CString format i need it in the BYTE format below. //BYTE localIp[] = {127,0,0,1}; could anyone show me how to do this? Thanks

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      CString str("127.0.0.1");
      struct in_addr ia;
      ia.S_addr = inet_addr(str);
      // now look at the four members of ia.S_un_b


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      I 1 Reply Last reply
      0
      • D David Crow

        CString str("127.0.0.1");
        struct in_addr ia;
        ia.S_addr = inet_addr(str);
        // now look at the four members of ia.S_un_b


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        I Offline
        I Offline
        Irish_GUI
        wrote on last edited by
        #3

        Hey, i am familar with that its a cstring to a byte[] im looking to do

        T 1 Reply Last reply
        0
        • I Irish_GUI

          I have an IP Address in CString format i need it in the BYTE format below. //BYTE localIp[] = {127,0,0,1}; could anyone show me how to do this? Thanks

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          maybe a standard fonction does this (inet_addr() i think, but i don't remember), the folowing works fine, even if it can seem "heavy" :

          #include <stdlib.h> // atoi()
          #include <ctype.h> // isdigit()
          #include <string.h> // strlen()

          enum IPValidate {
          IP_ERR = 0,
          IP_OK = 1
          };

          int str2bytesIPAddr (char pcDestTab[], const char* pcIPAddr) {
          // An IP address is between 0.0.0.0 and 255.255.255.255
          int iCptCharIntoIPAddr = 0;
          int iCptCharIntoByte = 0;
          int iResult = IP_OK;
          short sNbBytes = 0;
          char pstrByte[16]; // To prevent argument size error
          if (strlen(pcIPAddr) > 15) {// the max length of an IP address can't exceed 15 caracters
          iResult = IP_ERR;
          }
          else {
          do {
          if (isdigit(pcIPAddr[iCptCharIntoIPAddr])) {
          // A byte of an IP address mustn't begin with a '0', except if '0' is alone
          if ((iCptCharIntoByte == 0) && (pcIPAddr[iCptCharIntoIPAddr] == '0') && (isdigit(pcIPAddr[iCptCharIntoIPAddr+1]))) {
          iResult = IP_ERR;
          }
          else {
          pstrByte[iCptCharIntoByte] = pcIPAddr[iCptCharIntoIPAddr];
          iCptCharIntoIPAddr++;
          iCptCharIntoByte++;
          }
          }
          else {
          if (pcIPAddr[iCptCharIntoIPAddr] == '.') {
          // If we are at the end of a byte
          pstrByte[iCptCharIntoByte] = '\0';
          iCptCharIntoByte = 0;
          iCp

          I T 2 Replies Last reply
          0
          • I Irish_GUI

            Hey, i am familar with that its a cstring to a byte[] im looking to do

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            ooops, sory, i didn't see either it was a CString... (and i didn't see David's posts 'cause i was writing mine). well, by passing it to the function, just cast it before with the (LPCTSTR) operator defined in CString.


            TOXCCT >>> GEII power

            1 Reply Last reply
            0
            • T toxcct

              maybe a standard fonction does this (inet_addr() i think, but i don't remember), the folowing works fine, even if it can seem "heavy" :

              #include <stdlib.h> // atoi()
              #include <ctype.h> // isdigit()
              #include <string.h> // strlen()

              enum IPValidate {
              IP_ERR = 0,
              IP_OK = 1
              };

              int str2bytesIPAddr (char pcDestTab[], const char* pcIPAddr) {
              // An IP address is between 0.0.0.0 and 255.255.255.255
              int iCptCharIntoIPAddr = 0;
              int iCptCharIntoByte = 0;
              int iResult = IP_OK;
              short sNbBytes = 0;
              char pstrByte[16]; // To prevent argument size error
              if (strlen(pcIPAddr) > 15) {// the max length of an IP address can't exceed 15 caracters
              iResult = IP_ERR;
              }
              else {
              do {
              if (isdigit(pcIPAddr[iCptCharIntoIPAddr])) {
              // A byte of an IP address mustn't begin with a '0', except if '0' is alone
              if ((iCptCharIntoByte == 0) && (pcIPAddr[iCptCharIntoIPAddr] == '0') && (isdigit(pcIPAddr[iCptCharIntoIPAddr+1]))) {
              iResult = IP_ERR;
              }
              else {
              pstrByte[iCptCharIntoByte] = pcIPAddr[iCptCharIntoIPAddr];
              iCptCharIntoIPAddr++;
              iCptCharIntoByte++;
              }
              }
              else {
              if (pcIPAddr[iCptCharIntoIPAddr] == '.') {
              // If we are at the end of a byte
              pstrByte[iCptCharIntoByte] = '\0';
              iCptCharIntoByte = 0;
              iCp

              I Offline
              I Offline
              Irish_GUI
              wrote on last edited by
              #6

              cheers guys tis working struct in_addr ia; ia.s_addr = inet_addr(strAdapterInfo); BYTE localIp[3]; localIp[0] = ia.S_un.S_un_b.s_b1; localIp[1] = ia.S_un.S_un_b.s_b2; localIp[2] = ia.S_un.S_un_b.s_b3; localIp[3] = ia.S_un.S_un_b.s_b4; Thanks again

              1 Reply Last reply
              0
              • I Irish_GUI

                I have an IP Address in CString format i need it in the BYTE format below. //BYTE localIp[] = {127,0,0,1}; could anyone show me how to do this? Thanks

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                Hi I Hope this will work 1. CString str = "127,0,0,1"; int length = str .GetLength(); BYTE localIp[length] ; memcpy(localIp,str,length); or do it for(int i = 0;i

                T 1 Reply Last reply
                0
                • A Anonymous

                  Hi I Hope this will work 1. CString str = "127,0,0,1"; int length = str .GetLength(); BYTE localIp[length] ; memcpy(localIp,str,length); or do it for(int i = 0;i

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  ah ah ahh, do you just know what is an IP ? what you are suggesting will do so : ['1', '2', '7', '.', '0', '.', '.', '1'] that's not what we want. a coded IP Address is coded on 4 bytes, which represent each blocks of values. it will be either : [0x7F, 0x00, 0x00, 0x01].


                  TOXCCT >>> GEII power

                  1 Reply Last reply
                  0
                  • T toxcct

                    maybe a standard fonction does this (inet_addr() i think, but i don't remember), the folowing works fine, even if it can seem "heavy" :

                    #include <stdlib.h> // atoi()
                    #include <ctype.h> // isdigit()
                    #include <string.h> // strlen()

                    enum IPValidate {
                    IP_ERR = 0,
                    IP_OK = 1
                    };

                    int str2bytesIPAddr (char pcDestTab[], const char* pcIPAddr) {
                    // An IP address is between 0.0.0.0 and 255.255.255.255
                    int iCptCharIntoIPAddr = 0;
                    int iCptCharIntoByte = 0;
                    int iResult = IP_OK;
                    short sNbBytes = 0;
                    char pstrByte[16]; // To prevent argument size error
                    if (strlen(pcIPAddr) > 15) {// the max length of an IP address can't exceed 15 caracters
                    iResult = IP_ERR;
                    }
                    else {
                    do {
                    if (isdigit(pcIPAddr[iCptCharIntoIPAddr])) {
                    // A byte of an IP address mustn't begin with a '0', except if '0' is alone
                    if ((iCptCharIntoByte == 0) && (pcIPAddr[iCptCharIntoIPAddr] == '0') && (isdigit(pcIPAddr[iCptCharIntoIPAddr+1]))) {
                    iResult = IP_ERR;
                    }
                    else {
                    pstrByte[iCptCharIntoByte] = pcIPAddr[iCptCharIntoIPAddr];
                    iCptCharIntoIPAddr++;
                    iCptCharIntoByte++;
                    }
                    }
                    else {
                    if (pcIPAddr[iCptCharIntoIPAddr] == '.') {
                    // If we are at the end of a byte
                    pstrByte[iCptCharIntoByte] = '\0';
                    iCptCharIntoByte = 0;
                    iCp

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    i forgot to say (if it was interresting you) that it was previously a isIPAddr() that i added two lines (twice the following : pcDestTab[sNbBytes] = atoi(pstrByte);). i hope that can be helpful to understand the way an IP Address is analysed, and so on.


                    TOXCCT >>> GEII power

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups