str.c
来自「本程序是一个RS232转网口的。是一个透明传输的模块」· C语言 代码 · 共 281 行
C
281 行
/*
*****************************************************************************************************
*
* File name: string.c
*****************************************************************************************************
*/
#include "..\head\includes.h"
/*
*****************************************************************************************************
*Func: N位的ASCII码化为HEX
*Note:
* 1: count = 5 *data++ (12345)
* 2: x=1*10000/1 x=x+2*10000/10 x=x+3*10000/100 ...
*****************************************************************************************************
*/
unsigned long DecToHex(unsigned char xdata *data_ptr, unsigned char count)
{
unsigned char i;
unsigned long xdata count_value_temp=0;
unsigned long xdata count_value=0;
unsigned long xdata mod=1;
switch(count) {
case 5:
for (i=0; i<count; i++) {
count_value_temp = (*(data_ptr + i)-0x30) ;
count_value = count_value + count_value_temp * (10000 / mod );
mod *= 10;
}
break;
case 4:
for (i=0; i<count; i++) {
count_value_temp = (*(data_ptr + i)-0x30) ;
count_value = count_value + count_value_temp * (1000 / mod );
mod *= 10;
}
break;
case 3:
for (i=0; i<count; i++) {
count_value_temp = (*(data_ptr + i)-0x30) ;
count_value = count_value + count_value_temp * (100 / mod );
mod *= 10;
}
break;
case 2:
for (i=0; i<count; i++) {
count_value_temp = (*(data_ptr + i)-0x30) ;
count_value = count_value + count_value_temp * (10 / mod );
mod *= 10;
}
break;
case 1:
count_value = *data_ptr - 0x30;
break;
default :
count_value = 0;
break;
}
return (count_value);
}
/*
*****************************************************************************************************
*FUNC:
*NOTE:
*****************************************************************************************************
*/
#if 1
unsigned char xdata Data_Length[6];
void HexToDec(unsigned int H_value )
{
if ((H_value >= 10000) && (H_value <= 65535)) {
Data_Length[0] = H_value / 10000 + 0x30;
Data_Length[1] = (H_value % 10000) / 1000 + 0x30;
Data_Length[2] = (H_value % 1000) / 100 + 0x30;
Data_Length[3] = (H_value % 100) / 10 + 0x30;
Data_Length[4] = H_value % 10 + 0x30;
}
if ((H_value >= 1000) && (H_value <= 9999)) {
Data_Length[0] = 0x30;
Data_Length[1] = H_value / 1000 + 0x30;
Data_Length[2] = (H_value % 1000) / 100 + 0x30;
Data_Length[3] = (H_value % 100) / 10 + 0x30;
Data_Length[4] = H_value % 10 + 0x30;
} else if ((H_value >= 100) && (H_value <= 999)) {
Data_Length[0] = 0x30;
Data_Length[1] = 0x30;
Data_Length[2] = H_value / 100 + 0x30;
Data_Length[3] = (H_value % 100) / 10 + 0x30;
Data_Length[4] = H_value % 10 + 0x30;
} else if ((H_value >= 10) && (H_value <= 99)) {
Data_Length[0] = 0x30;
Data_Length[1] = 0x30;
Data_Length[2] = 0x30;
Data_Length[3] = H_value / 10 + 0x30;
Data_Length[4] = H_value % 10 + 0x30;
} else if ((H_value >= 0) && (H_value <= 9)) {
Data_Length[0] = 0x30;
Data_Length[1] = 0x30;
Data_Length[2] = 0x30;
Data_Length[3] = 0x30;
Data_Length[4] = H_value + 0x30;
}
Data_Length[5] = '\0';
}
/*
*****************************************************************************************************
*FUNC:
*NOTE: "1235455555555555555555555555555555 AT OK" "AT OK"
*****************************************************************************************************
*/
unsigned char *StrCompare(unsigned char *mother, unsigned char *son, unsigned int len)
{
unsigned int i;
unsigned char *son_temp;
unsigned char *mother_temp;
unsigned char *return_ptr=0;
for (i=0; i<len; i++) {
return_ptr = mother_temp = mother+i;
son_temp = son;
if (*mother_temp == *son_temp) { // 查找母串第一个与字串匹配
while (*son_temp) { //从第一个位置开始,查找子串所有匹配
if (*son_temp++ == *mother_temp) {
if (mother_temp < (mother+len)) {
mother_temp++;
} else {
return 0;
}
} else {
goto loop;
}
}
return return_ptr;
}
loop:
;
}
return 0;
}
/*
*****************************************************************************************************
*FUNC:
*NOTE:
*****************************************************************************************************
*/
#if 1
void ReplaceTag(unsigned char xdata *start, char *replace, unsigned char replace_len)
{
unsigned char idata i=0;
if (start == NULL) return;
while(*replace) {
*start++ = *replace++;
if (i < replace_len) {
i++;
} else {
return;
}
}
for (; i<replace_len; i++) { //
*start++ = 0x20; // 空格
}
/*
for (i=0; i < 8; i++) {
if (i < 5) {
ptr[i] = sub[i];
} else {
ptr[i] = 0x20; // 空格
}
}
*/
}
#endif
/*
*****************************************************************************************************
*FUNC:
*NOTE: 192.168.0.100 00192 5位
*****************************************************************************************************
*/
unsigned char xdata IpString[16];
void IpToStr(union IP_address sourceip)
{
unsigned char i,j,count,start,bit_num;
j = 0;
count=0;
start=0;
bit_num=0;
for (i=0; i<4; i++) {
if (sourceip.bytes[i] >= 100) {
bit_num = 3;
} else if (sourceip.bytes[i] >= 10){
bit_num = 2;
} else {
bit_num = 1;
}
start = 5-bit_num;
HexToDec(sourceip.bytes[i]); //将IP化为字符串后存在Data_Length里
for (j=0; j<bit_num; j++) {
IpString[count++] = Data_Length[start+j];
}
if (i == 3) {
IpString[count] = 0; //结束符
} else {
IpString[count++] = '.';
}
}
}
/*
*****************************************************************************************************
*FUNC:
*NOTE: 7200秒 24小时50分钟50秒
*****************************************************************************************************
*/
char xdata TimeString[20];
void TimeToStr(unsigned long seconds)
{
unsigned char time[3]={0,0,0};
unsigned char i,j,count,start,bit_num;
j = 0;
count=0;
start=0;
bit_num=0;
time[0] = seconds/3600; // hours
time[1] = (seconds%3600)/60; // mins
time[2] = seconds%60; // secs
for (i=0; i<3; i++) {
if (time[i] >= 100) {
bit_num = 3;
} else if (time[i] >= 10){
bit_num = 2;
} else {
bit_num = 1;
}
start = 5-bit_num;
HexToDec(time[i]); //将IP化为字符串后存在Data_Length里
for (j=0; j<bit_num; j++) {
TimeString[count++] = Data_Length[start+j];
}
if (i == 0) {
TimeString[count++] = 0xd0; //"小"
TimeString[count++] = 0xa1;
TimeString[count++] = 0xca;
TimeString[count++] = 0xb1; //"时"
} else if (i == 1) {
TimeString[count++] = 0xb7; //"分"
TimeString[count++] = 0xd6;
TimeString[count++] = 0xd6;
TimeString[count++] = 0xd3; //"钟"
} else {
TimeString[count++] = 0xc3; //"秒"
TimeString[count++] = 0xeb;
TimeString[count++] = 0; // 结束符
}
}
}
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?