
我需要把第二个红框内容转换为IP地址,都是string类型,末尾的3B代表着;
能否给予代码参考?感谢
#include <stdio.h>
#include <string.h>
/* "AB" -> 0xAB */
int char_to_byte(const char *str_char, unsigned char *str_byte)
{
unsigned char byte_high = 0x00;
unsigned char byte_low = 0x00;
byte_high = *str_char;
byte_low = *(str_char + 1);
if ((str_char == NULL) || (str_byte == NULL))
return -1;
/* byte High */
if ((byte_high >= '0') && (byte_high <= '9'))
{
byte_high = byte_high - '0';
}
else if ((byte_high >= 'a') && (byte_high <= 'f'))
{
byte_high = byte_high - 'a' + 0x0a;
}
else if ((byte_high >= 'A') && (byte_high <= 'F'))
{
byte_high = byte_high - 'A' + 0x0a;
}
else
{
return -1;
}
/* byte Low */
if ((byte_low >= '0') && (byte_low <= '9'))
{
byte_low = byte_low - '0';
}
else if ((byte_low >= 'a') && (byte_low <= 'f'))
{
byte_low = byte_low - 'a' + 0x0a;
}
else if ((byte_low >= 'A') && (byte_low <= 'F'))
{
byte_low = byte_low - 'A' + 0x0a;
}
else
{
return -1;
}
*str_byte = (byte_high << 4) + byte_low;
return 0;
}
/* "ABCD..." -> [0xAB, 0xCD, ...] */
int hex_str_convert(const char *str_in, char *str_out)
{
int i = 0;
unsigned char tmp = 0;
size_t len = strlen(str_in);
for (i = 0; i < len / 2; i++)
{
if (char_to_byte(&str_in[i * 2], &tmp) == 0x00)
*(str_out + i) = tmp;
else
return -1;
}
return 0;
}
int main()
{
char str_out[64] = { 0 };
const char *str_in = "3131372E37342E3133362E33343B";
hex_str_convert(str_in, str_out);
printf(str_out);
return 0;
}
#include <stdio.h>
#include <string.h>
char conv_to_upper_case(char ch)
{
if(ch >= 'a' && ch <= 'z')
{ return (ch - 32); }
else
{ return ch; }
}
char ascii_to_hex_nib(char ch)
{
ch = conv_to_upper_case(ch);
if(('0' <= ch) && (ch <= '9')) {
return (ch - '0');
}
else if(('A' <= ch) && (ch <= 'F')) {
return (ch - 'A' + 10);
}
else {
return 0xff;
}
}
void convert(char *str_in, char *str_out)
{
int i;
int len = strlen(str_in);
//如果不想要最后的分号,把上面的值 - 2
char tmp;
for(i = 0; i< len/2; i++)
{
tmp = ascii_to_hex_nib(*(str_in + i * 2));
tmp <<= 4;
tmp |= ascii_to_hex_nib(*(str_in + i * 2 + 1));
*(str_out + i) = tmp;
}
*(str_out + len/2) = 0;//end of string
}
int main()
{
char str_in[] = {"3131372E37342E3133362E33343B"} ;
char str_out[100];
convert(str_in, str_out);
puts(str_out);
return 0;
}

#include <stdio.h>
int main()
{
char t[]=
{
"3131372E37342E3133362E33343B\0"
};
int p=0,count=0;
int ascll[150]={
0
};
while(t[p]!='\0')
{
int a,b;
if(t[p]<='Z'&&t[p]>='A')
{
a=(t[p]-'A'+10)*16;
}
else
{
a=(t[p]-48)*16;
}
if(t[p+1]<='Z'&&t[p+1]>='A')
{
b=(t[p+1]-'A'+10);
}
else
{
b=(t[p+1]-48);
}
ascll[count++]=a+b;
p+=2;
}
printf("\n");
for(int i=0;i<count-1;i++)
{
printf("%c",ascll[i]);
}
printf("\n");
return 0;
}

是这样吗
#include <cstring>
#include <cstdio>
using namespace std;
int CharToInt(char c)
{
if(c <= '9') return int(c-'0');
else return int(c-'A'+10);
}
char a[1005];
int main()
{
scanf("%s", a);
int len = strlen(a);
int i=0;
while(i < len) printf("%c", CharToInt(a[i++]) * 16 + CharToInt(a[i++]));
}

#include <stdio.h>
#include <string.h>
//0xc0a82c05 转 192.168.44.5
int intToString(int num , char *buf ,int count)
{
int a = num/100;
int b = (num/10)%10;
int c = num%10;
if( a == 0)
{
if( b != 0)
{
buf[count++] = b + '0';
buf[count++] = c + '0';
}
else
{
buf[count++] = c + '0';
}
}
else
{
buf[count++] = a + '0';
buf[count++] = b + '0';
buf[count++] = c + '0';
}
return count;
}
char *intToChar(unsigned int number)
{
static char buf[20] = {0};
int count = 0;
int idx = 24;
while( idx >= 0 )
{
int num = (number>>idx) & 0xff;
count = intToString(num , buf , count);
if( idx != 0)
{
buf[count++] = '.';
}
idx -= 8;
}
return buf;
}
int main()
{
int number = 0xc0a82c05;
char *ret = intToChar( number );
printf("ret = %s \n", ret);
return 0;
}

#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<assert.h>
int hex_char_to_dec(char hex_ch)
{
if (isdigit(hex_ch))return hex_ch - '0';
if ('A' <= hex_ch && hex_ch <= 'F')return 10 + hex_ch - 'A';
assert(0);
return 0;
}
void convert(const char* str, char ip[16])
{
memset(ip, 0, sizeof(ip));
int l = strlen(str);
int addr_len = 0;
for (int i = 0; i < l-1; i += 2)
{
char ch = hex_char_to_dec(str[i]) * 16 + hex_char_to_dec(str[i+1]);
ip[addr_len++] = ch;
}
}
int main()
{
const char* hex_str = "3131372E37342E3133362E33343B";
char ip[16] = { 0 };
convert(hex_str, ip);
printf("16进制字符串:%s\n", hex_str);
printf("ip地址:%s\n", ip);
return 0;
}