The difference between single and double quotes in C language (solve the puzzle of extracting IP address from string) _c language

Source: Internet
Author: User
Tags strlen

Problem:
After converting std:string to char* from a C + + file, the char* containing the IP address needs to be extracted from the IP address in the string char*;

Solution:
1. Solution Idea:
The maximum IP address is 12 characters + 3 periods = 15 characters, the general expression is 192.168.111.111;
Can be parsed from the first character when it is not '. ' , save all the characters and convert all the saved characters to 16.

2. The procedure is as follows:

Copy Code code as follows:

typedef struct {
Char addr_ipv4[4];
} Ipaddr_ipv4;
#include <stdlib.h>
Function:to extract HEX value of IP address from string expression.
Ipaddr_ipv4 parse_ipstring (const char *STR, U8 strlen) {
Ipaddr_ipv4 IPv4;
Char Tmpbuf[4] = {0};
U8 K;
U8 inx = 0;
U8 j = 0;
dprintf ("The Ch_ip is:%s\n", ch_ip);
For (k=0 k<strlen; k++) {
if (Str[k]!= '. ') {
dprintf ("K-inx:%d\n", K-inx);
memcpy (&tmpbuf[k-inx], &str[k], 1);
}
else {
Inx = k + 1;
dprintf ("Inx:%d, Tmpbuf:%s\n", Inx, Tmpbuf);
Ipv4.addr_ipv4[j++] = Strtol (Tmpbuf, NULL, 10);
memset (tmpbuf, 0, sizeof (TMPBUF));
Continue
}
dprintf ("Tmpbuf:%s\n", tmpbuf);
IPV4.ADDR_IPV4[J] = Strtol (Tmpbuf, NULL, 10);
}
dprintf ("IPv4 is:");
for (k=0; k<sizeof (Ipv4.addr_ipv4); k++) {
dprintf ("%d", ipv4.addr_ipv4[k]);
// }
dprintf ("\ n");
return IPv4;
}

The application is as follows:
Copy Code code as follows:

char* ch_ip = NULL;
Ipaddr_ipv4 ipv4addr;
Ch_ip = (char*) malloc (16);
memset (ch_ip, 0, 16);

Parse IP Address
Ch_ip = Getipaddr ();

Convert IP address from string to HEX expression
IPV4ADDR = parse_ipstring (Ch_ip, strlen (CH_IP));

3. Attention MATTERS:
The original character is compared with the IF (Str[k]!= "." Instead of if (Str[k]!= '. '), the result has been wrong.
Then repeatedly find the reason, only to know the difference between single quotes ' and double quotes ' ":

(1). Meaning is different.
A character enclosed in single quotes represents an integer that corresponds to the sequence value of the character in the ASCII character set;
Double quotes result in strings, even if only one character is a string, and a character in double quotes represents a pointer to the starting character of an unnamed array;
For example: '. ' Represents decimal 46, hexadecimal 0x2e;
and "." Represents a pointer to the starting character "." A pointer to an array of;

(2). Different sizes.
Single quotes cause a character size to be a byte;
A character size that is caused by double quotes is two bytes, because a string of double quotes adds a 0x00 at the end of the string as the end identity of the string.

The IP address is correctly taken in my program.
Solve the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.