| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- /************************************************************************
- * AUTHOR: NiuJiuRu
- * FILENAME: swchar.c
- * DESCRIPTION: 字符操作扩展函数
- * NOTE:
- * HISTORY:
- * 1, [2010-08-17] created by NiuJiuRu
- ***********************************************************************/
- #include "swapi.h"
- #include "swchar.h"
- /* 在一个char内, 根据外部输入的位置信息(0 <= pos <= 7), 得到它所对应的二进制位的值 */
- char sw_char_getBit(char c, int pos)
- {
- if(pos < 0 || pos > 7) return -1;
- return (c >> pos) & 0x01;
- }
- /* 在一个char内, 根据外部输入的位置信息(0 <= pos <= 7), 设置它所对应的二进制位的值 */
- bool sw_char_setBit(char *c, int pos, char bit)
- {
- if(!c || (pos < 0 || pos > 7)) return false;
-
- if(bit == 0x00) *c &= ~(0x01 << pos);
- else if(bit == 0x01) *c |= (0x01 << pos);
- else return false;
- return true;
- }
- /* 在一个char数组内, 根据外部输入的位置信息(pos >= 0), 得到它所对应的二进制位的值 */
- char sw_chararry_getBit(const char *buf, int bufSize, int pos)
- {
- if(!buf || bufSize <= 0 || (pos < 0 || pos > bufSize*8-1)) return -1;
-
- return sw_char_getBit(buf[pos/8], pos%8);
- }
- /* 在一个char数组内, 根据外部输入的位置信息(pos >= 0), 设置它所对应的二进制位的值 */
- bool sw_chararry_setBit(char *buf, int bufSize, int pos, char bit)
- {
- if(!buf || bufSize <= 0 || (pos < 0 || pos > bufSize*8-1)) return false;
-
- return sw_char_setBit(&buf[pos/8], pos%8, bit);
- }
|