功勋会员
WJ
- 积分
- 6304
- 威望
- 1456
- 贡献
- 4674
- 兑换币
- 17
- 注册时间
- 2008-4-6
- 在线时间
- 87 小时
|
<><FONT size=4>这个是从BEEKIT上发现的,非常不错的,介绍给大家</FONT></P><P><FONT size=4>#include "string.h"</FONT></P><P><FONT color=#f73809 size=4>void int2string (UINT32 value, char *string) {</FONT></P><P><FONT size=4> UINT8 remainder;<BR> UINT8 offset = 9;<BR> <BR> (void)strcpy(string, " ");<BR> <BR> do {<BR> remainder = value % 10;<BR> string[offset] = UTILDec2Char(remainder);<BR> value = value / 10;<BR> offset--;<BR> } <BR> while (value != 0);</FONT></P><P><FONT size=4> string[10] = 0; //Set to null.</FONT></P><P><FONT size=4>}</FONT></P><P><FONT color=#ee3d11 size=4>char UTILDec2Char(UINT8 value) {</FONT></P><P><FONT size=4> if (value <=9) // ((value >= 0) && (value <=9))<BR> return (char) (value + 0x30);<BR> else<BR> return (char) (value + 0x37);<BR>}</FONT></P><P><FONT color=#f73809 size=4>UINT8 char2num(char ch) {</FONT></P><P><FONT size=4> UINT8 value=0;</FONT></P><P><FONT size=4> if ((ch >= '0') && (ch <='9')) {<BR> value = ch - 0x30;<BR> } else {<BR> value = 0;<BR> }<BR> <BR> return value;<BR>}</FONT></P><P><FONT size=4><FONT color=#ff3300>UINT32 String2Uint32(char *str) {</FONT><BR> <BR> UINT8 u8i, u8length;<BR> UINT32 u32value=0;<BR> UINT32 u32total=0;<BR> UINT16 u16base=1;<BR> <BR> //Check for Null String<BR> u8length = (UINT8) strlen(str);<BR> if (u8length == 0) {<BR> return 0;<BR> }<BR> <BR> //Check for Overflow of the UINT32 variable.<BR> if (u8length > 8) {<BR> return 0xFFFFFFFF;<BR> }<BR> <BR> for (u8i=0; u8i<u8length; u8i++) {<BR> u32value = char2num((char) str[u8length-u8i-1]);<BR> u32value *= u16base;<BR> u16base *= 10;<BR> u32total += u32value;<BR> }<BR> <BR> return u32total;<BR>}</FONT></P><P><FONT size=4><FONT color=#f73809>UINT16 String2Uint16(char *str) {</FONT><BR> <BR> UINT8 u8i, u8length;<BR> UINT32 u32value=0;<BR> UINT16 u16total=0;<BR> UINT16 u16base=1;<BR> <BR> //Check for Null String<BR> u8length = (UINT8)strlen(str);<BR> if (u8length == 0) {<BR> return 0;<BR> }<BR> <BR> //Check for Overflow of the UINT32 variable.<BR> if (u8length > 8) {<BR> return 0xFFFFFFFF;<BR> }<BR> <BR> for (u8i=0; u8i<u8length; u8i++) {<BR> u32value = char2num((char) str[u8length-u8i-1]);<BR> u32value *= u16base;<BR> u16base *= 10;<BR> u16total += (UINT16)u32value;<BR> }<BR> <BR> return u16total;<BR>}</FONT></P><P><FONT color=#f73809 size=4>void UTILInt2String (UINT32 value, UINT8 *string) {</FONT></P><P><FONT size=4> UINT8 remainder;<BR> UINT8 offset = 9;<BR> <BR> (void)strcpy(string, " ");<BR> <BR> do {<BR> remainder = value % 10;<BR> string[offset] = UTILDec2Char(remainder);<BR> value = value / 10;<BR> offset--;<BR> } <BR> while (value != 0);</FONT></P><P><FONT size=4> string[10] = 0; //Set to null.</FONT></P><P><FONT size=4>}</FONT></P><P><FONT color=#ff0000 size=4>void Uint32ToHexString(UINT32 u32Num, UINT8 *pu8Str) {</FONT></P><P><FONT size=4> UINT8 u8Mod = 0;<BR> UINT8 u8Offset = 9;<BR> <BR> (void)strcpy(pu8Str, "00000000000");<BR> <BR> while(u32Num != 0){<BR> u8Mod = u32Num % 16;<BR> pu8Str[u8Offset] = UTILDec2Char(u8Mod);<BR> u32Num/=16;<BR> u8Offset--;<BR> }<BR> pu8Str[0] = '0';<BR> pu8Str[1] = 'x';<BR> pu8Str[10] = '\0';<BR> <BR>}</FONT></P><P><FONT size=4><FONT color=#f73809>void trimWhiteSpace(char *str) {</FONT><BR> <BR> UINT8 length, i=0;<BR> UINT8 leading_ws=0;<BR> UINT8 ws_flag = TRUE;<BR> <BR> length = (UINT8) strlen(str);<BR> </FONT></P><P><FONT size=4> while ((i < length) && (ws_flag == TRUE)) {<BR> if (str<i> >= 33 && str<i> <=126) {<BR> ws_flag = FALSE;<BR> } else {<BR> i++;<BR> }<BR> }<BR> <BR> leading_ws = i;<BR> (void)strcpy(str, str + leading_ws); <BR>}<BR></FONT></P> |
|