/********************************************my_string.c****************************************************/
#include "includes.h"
/*********************************************************************************************************
十六进制转ASCII表定义
*********************************************************************************************************/
const char ascTable[17] = {"0123456789ABCDEF"};
const char *CONST_RESULT[] = {"OK","ERROR"};
/*********************************************************************************************************
** Function name: utf8_2_iso8859
** Descriptions: 将UTF8字符串转换为ISO-8859字符集
** input parameters: mode: 0:get matrix location in <font.h>, 1:get unicode
** Output parameters:
** Returned value:
*********************************************************************************************************/
void utf8_2_iso8859( unsigned char* src,unsigned char* dest,unsigned char mode )
{
// u8 temp;
u16 unicode;
while( *src != '\0' )
{
if( ( src[0]&0xE0 ) == 0xC0 )
{
unicode = ( (u16)(src[0]&0x1F) << 6 ) | ( src[1]&0x3F );
if( mode == 0 )
{
#if 0
for( temp=0;temp<MY_COUNTOF(struISO8859Matrix);temp++ )
{
/* get the extend matrix location in <asc2_1206><asc2_1608><asc2_2412> */
if( unicode == struISO8859Matrix[temp].iso8859 )
{
unicode = struISO8859Matrix[temp].matrix;
break;
}
}
if( temp == MY_COUNTOF(struISO8859Matrix) ) unicode = '?';
#endif
}
src += 2;
}
else if( isprint(*src) )
{
unicode = *src;
src++;
}
else if( *src == '\n' )
{
/* 鎹㈣?屽垯蹇界??*/
src++;
continue;
}
else
{
unicode = '?';
src++;
}
if( mode == 0 )
{
*dest++ = (u8)unicode;
}
else
{
sprintf( (char*)dest,"%04x",unicode );
dest += 4;
}
}
*dest = 0;
}
/*********************************************************************************************************
** Function name: bit_pos_2_num
** Descriptions: 32bits
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
u8 bit_pos_2_num( u32 bit_pos )
{
u8 ret;
ret = 32;
while( ret-- )
{
if( ( bit_pos&0x80000000 ) != 0 ) break;
bit_pos <<= 1;
}
return ret;
}
/*********************************************************************************************************
** Function name: num_2_bit_pos
** Descriptions: 32bits
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
u32 num_2_bit_pos( u8 num )
{
u32 ret = 0x00000001;
while( num-- ) ret <<= 1;
return ret;
}
/*********************************************************************************************************
** Function name: hex_2_ascii
** Descriptions: 将二进制数组转换为ASCII字符串(注意:输入指针的内容会被输出占用)
** input parameters: len: 需要转换的二进制数组的长度(HEX的长度)
** Output parameters: 输入输出占用同一个数组指针
** Returned value:
*********************************************************************************************************/
void hex_2_ascii( u8 *buf, u16 len )
{
u8 temp;
buf[len*2] = '\0';
while(len--)
{
temp = buf[len];
buf[len*2+0] = ascTable[temp>>4];
buf[len*2+1] = ascTable[temp&0x0f];
}
}
/*********************************************************************************************************
** Function name: toint
*********************************************************************************************************/
u8 toint( char c )
{
if( c >= '0' && c <= '9' ) return c-'0';
else if( c >= 'A' && c <= 'F' ) return c-0x37;
else if( c >= 'a' && c <= 'f' ) return c-0x57;
else return 0x0f;
}
/*********************************************************************************************************
** Function name: ascii_2_hex
** Descriptions: 将ASCII字符串转换为二进制数组
** input parameters: dest: 转换数据的入口指针,<src_len>为字符的长度
** Output parameters: src: 输入输出可以占用同一个数组指针,但输入指针数组中的数据在转换过程中会被修改
** Returned value: 转换后数据长度(HEX的长度)
*********************************************************************************************************/
u16 ascii_2_hex( u8 *dest,char *src,u16 src_len )
{
u16 len=0;
u8 temp;
while( src_len >= 2 )
{
temp = toint(*src++)<<4;
temp |= toint(*src++);
*dest++ = temp;
src_len -= 2;
len++;
}
return len;
}
/*********************************************************************************************************
** Function name: ascii_2_utf16
** Descriptions: 将ASCII字符串转换为UTF16编码,每个字符占用两个字节的UTF16: e.g., '6' -> 0x00 0x36
** input parameters: dest: 转换数据的入口指针,
** Output parameters: 输入输出可以占用同一个数组指针,但输入指针数组中的数据在转换过程中会被修改
** Returned value: total bytes
*********************************************************************************************************/
u16 ascii_2_utf16( u8 *dest,char *src )
{
u16 len,total;
len = strlen(src);
if( len == 0 ) return 0;
// append 2 over characters
#if 0
dest[len*2+0] = '\0';
dest[len*2+1] = '\0';
total = len*2+2;
#endif
total = len*2;
while(len--)
{
dest[len*2+1] = src[len];
dest[len*2+0] = 0x00;
}
return total;
}
/*********************************************************************************************************
** Function name: exchange32
** Descriptions: 32bits exchange
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
u32 exchange32( u32 value )
{
u32 temp;
u8 *index1,*index2;
index1 = (u8 *)&temp;
index2 = (u8 *)&value;
*(index1+0) = *(index2+3);
*(index1+1) = *(index2+2);
*(index1+2) = *(index2+1);
*(index1+3) = *(index2+0);
return temp;
}
/*********************************************************************************************************
** Function name: russian_2_unicode
** Descriptions: 将俄语字符串转换为unicode字符串
** input parameters: dest: 转换数据的出口指针
** Output parameters:
** Returned value:
*********************************************************************************************************/
void russian_2_unicode( unsigned char *dest,unsigned char *src )
{
/*
АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ
0xA7A1~0x0XA7C1
0x0410~0x0415(АБВГДЕ) 0x0401(Ё) 0x0416~0x042F(ЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ)
абвгдеёжзийклмнопрстуфхцчшщъыьэюя
0xA7D1~0XA7F1
0x4030~0x0435(абвгде) 0x0451(ё) 0x0436~0x044F(жзийклмнопрстуфхцчшщъыьэюя)
ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ
0xA6A1~0xA6B8
to unicode:
A6A1~A6B1: 0391~03A1
A6B2~A6B8: 03A3~0x03A9
*/
u16 temp;
while(*src)
{
if( *src == 0xA7 )
{
temp = ((u16)(*src)<<8) + *(src+1);
if( temp <= 0xA7A6 ) temp = (temp-0xA7A1)+0x0410;
else if( temp == 0xA7A7 ) temp = 0x0401;
else if( temp <= 0xA7C1 ) temp = (temp-0xA7A8)+0x0416;
else if( temp <= 0xA7D6 ) temp = (temp-0xA7D1)+0x0430;
else if( temp == 0xA7D7 ) temp = 0x0451;
else if( temp <= 0xA7F1 ) temp = (temp-0xA7D8)+0x0436;
sprintf( (char*)dest,"%04x",temp );
src += 2;
}
else if( *src == 0xA6 )
{
// greek to unicode
temp = ((u16)(*src)<<8) + *(src+1);
if( temp <= 0xA6B1 ) temp = (temp-0xA6A1)+0x0391;
else if( temp <= 0xA6B8 ) temp = (temp-0xA6B2)+0x03A3;
sprintf( (char*)dest,"%04x",temp );
src += 2;
}
else if( isprint(*src) )
{
sprintf( (char*)dest,"00%02x",*src );
src++;
}
else
{
src++;
}
dest += 4;
}
*dest = 0;
}
/*********************************************************************************************************
** Function name: memory_append_byte
** Descriptions:
** input parameters: total bytes of dest in <total>
** Output parameters:
** Returned value: total bytes
*********************************************************************************************************/
u16 memory_append_byte( u8 *dest,u16 total,u8 byte )
{
*(dest+total) = byte;
return total+1;
}
/*********************************************************************************************************
** Function name: memory_append_buffer
** Descriptions:
** input parameters: total bytes of dest in <total>
** Output parameters:
** Returned value: total bytes
*********************************************************************************************************/
u16 memory_append_buffer( u8 *dest,u16 total,u8 *src,u16 len )
{
memcpy( dest+total,src,len );
return total+len;
}
/*********************************************************************************************************
** Function name: getsubstr
** Descriptions: 根据逗号位置获得子字符串(未对输出字符串做越界检验,输出字符串有可能会越界)
** input parameters:
** Output parameters: substr
** Returned value: 非0,取到第n个逗号位置后的字符串
*********************************************************************************************************/
u16 getsubstr( u8 pos,char *str,char *substr )
{
char *p=str;
u16 len;
*substr = 0;
if( pos )
{
// while( isprint(*p) && pos )
while( *p != 0 && pos )
{
if( *p++ == ',' ) pos--;
}
if( pos ) return 0;
}
len=0;
// while( isprint(*p) && *p != ',' )
while( *p != 0 && *p != ',' )
{
*substr++ = *p++;
len++;
}
*substr = 0;
return len;
}
// ZX-D22
/*********************************************************************************************************
** Function name: hex_to_asciiH
** Descriptions: 把HEX的高4位转为字符串
** input parameters:
** Output parameters:
** Returned value: 转换后的字符串
*********************************************************************************************************/
char hex_to_asciiH(char data_hex)
{
char ASCII_Data;
ASCII_Data=data_hex & 0xF0;
ASCII_Data = ASCII_Data>>4;
if(ASCII_Data<10)
ASCII_Data=ASCII_Data+0x30; //?--9?
else
ASCII_Data=ASCII_Data+0x37; //慉--F?
return ASCII_Data;
}
/*********************************************************************************************************
** Function name: hex_to_asciiL
** Descriptions: 把HEX的低4位转为字符串
** input parameters:
** Output parameters:
** Returned value: 转换后的字符串
*********************************************************************************************************/
char hex_to_asciiL(char data_hex)
{
char ASCII_Data;
ASCII_Data=data_hex & 0x0F;
if(ASCII_Data<10)
ASCII_Data=ASCII_Data+0x30; //?--9?
else
ASCII_Data=ASCII_Data+0x37; //慉--F?
return ASCII_Data;
}
int my_atoi(const char *src)
{
int s = 0;
u8 isMinus = 0;
while(*src == ' ') //跳过空白符
{
src++;
}
if(*src == '+' || *src == '-')
{
if(*src == '-')
{
isMinus = 1;
}
src++;
}
else if(*src < '0' || *src > '9') //如果第一位既不是符号也不是数字,直接返回异常值
{
s = 2147483647;
return s;
}
while(*src != '\0' && *src >= '0' && *src <= '9')
{
s = s * 10 + *src - '0';
src++;
}
return s * (isMinus ? -1 : 1);
}
/*********************************************************************************************************
** Function name: getsubstr2
** Descriptions: 获取两个字符之间的子字符串(未对输出字符串做越界检验,输出字符串有可能会越界)
** input parameters:
** Output parameters: substr
** Returned value: 非0,取到子字符串的长度
*********************************************************************************************************/
u16 getsubstr2( char *str,char start,char over,char *substr )
{
char *p1,*p2;
u16 len = 0;
p1 = strchr( str,start );
p2 = strchr( p1+1,over );
*substr = 0;
if( p1 != NULL && p2 != NULL && p2 > p1+1 )
{
while( ++p1 < p2 )
{
*substr++ = *p1;
len++;
}
*substr = 0;
}
return len;
}
/*********************************************************************************************************
END
*********************************************************************************************************/
/**************************************************my_string.h*******************************************/
//防止多处调用产生重复定义错误
#ifndef __my_string_h_
#define __my_string_h_
/*********************************************************************************************************
外部变量声明
*********************************************************************************************************/
extern const char ascTable[17];
#include "my.h"
/*********************************************************************************************************
函数声明
*********************************************************************************************************/
void hex_2_ascii( u8 *buf, u16 len );
u16 ascii_2_hex( u8 *dest,char *src,u16 src_len );
u8 toint( char c );
u8 bit_pos_2_num( u32 bit_pos );
u32 num_2_bit_pos( u8 num );
u32 exchange32( u32 value );
void russian_2_matrix( unsigned char* src, unsigned char* dest);
void russian_2_unicode( unsigned char *dest,unsigned char *src );
void utf8_2_iso8859( unsigned char* src,unsigned char* dest,unsigned char mode );
u16 memory_append_byte( u8 *dest,u16 total,u8 byte );
u16 memory_append_buffer( u8 *dest,u16 total,u8 *src,u16 len );
u16 ascii_2_utf16( u8 *dest,char *src );
u16 getsubstr( u8 pos,char *str,char *substr );
char hex_to_asciiH(char data_hex);
char hex_to_asciiL(char data_hex);
int my_atoi(const char *src);
u16 getsubstr2( char *str,char start,char over,char *substr );
#endif