Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17203
  • 博文数量: 37
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 354
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-15 22:04
文章分类

全部博文(37)

文章存档

2014年(37)

我的朋友

分类: C/C++

2014-07-15 22:17:59

#include "OJ.h"
#include
#include
#include


#define BITS_NUM_OF_BYTE    (8)
#define BITS_MALLOC_SIZE    (9)
#define ELEM_MAX_SIZE       (32)
#define ELEM_MALLOC_SIZE    (33)




/*******************************************************************************
Func Name       : ByteTransToBits
Date Created    : 2013-11-29
Author          : 
Description     : 
Input           : 
unsigned char   aInputByte[],   待转化的字节
unsigned int    uiIutputLen,    实际长度
Output          : 
char            *pcStrBuf,      转化后的二进制字符串
Return          : 
Caution         :
History         :
Date                Author                      Modification
*******************************************************************************/
void ByteTransToBits
(
 unsigned char   aInputByte[],
 unsigned int    uiIutputLen,
 char            *pcStrBuf
 )
{
unsigned int i      = 0;
unsigned int j      = 0;
unsigned int index  = 0;
char acTmp[BITS_MALLOC_SIZE] = {0};
for (i = 0; i < uiIutputLen; i++)
{
_itoa_s(aInputByte[i], acTmp, BITS_MALLOC_SIZE, 2);
if (strlen(acTmp) < BITS_NUM_OF_BYTE)   //如果itoa转化的二进制串不满8位,则在头部加'0'
{
j = BITS_NUM_OF_BYTE - (unsigned int)strlen(acTmp);
while (j-- > 0)
{
pcStrBuf[index] = '0';
index++;
}
memcpy(pcStrBuf + index, acTmp, (unsigned int)strlen(acTmp));
index += (unsigned int)strlen(acTmp);
memset(acTmp, 0, BITS_MALLOC_SIZE);
continue;
}
memcpy(pcStrBuf + index, acTmp, strlen(acTmp));
memset(acTmp, 0, BITS_MALLOC_SIZE);
printf("\n%s : %d", acTmp, strlen(acTmp));
}
}


/*******************************************************************************
Func Name       : BitsTransToNum
Date Created    : 2013-11-29
Author          : 
Description     : 根据数值所占位数,截取二进制字符串,获取真实数值
Input           : 
char            *pcStrBuf,      二进制字符串
unsigned int    uiElementNum,   有效二进制位数


Output          : 
ELEMENT_STRU    astElement[],   数值结构体
Return          : 
Caution         :
History         :
Date                Author                      Modification
*******************************************************************************/
void BitsTransToNum
(
 char            *pcStrBuf,
 unsigned int    uiElementNum,
 ELEMENT_STRU    astElement[]
)
{
int             iNum    = 0;
unsigned int    i       = 0;
unsigned int    j       = 0;
unsigned int    index   = 0;
char acNum[ELEM_MALLOC_SIZE] = {0};
index = 0;
for (i = 0; i < uiElementNum; i++)
{
iNum = 0;
memset(acNum, 0, ELEM_MALLOC_SIZE);
if (astElement[i].uiElementLength < 1 || astElement[i].uiElementLength > ELEM_MAX_SIZE)
{
return;
}
memcpy(acNum, pcStrBuf + index, astElement[i].uiElementLength);
index += astElement[i].uiElementLength;
for (j = 0; j < (unsigned int)strlen(acNum); j++)
{
iNum = iNum << 1;
iNum = iNum + acNum[j] - '0';
}
astElement[i].uiElementValue = iNum;
}
}


/*
功能: 根据数值占用BIT数,按顺序从输入字节流中解析出对应数值,解析顺序按输入数组astElement索引升序


输入:
unsigned int uiIutputLen:字节数组(流)长度
unsigned char aInputByte:字节数组(流)
unsigned int uiElementNum:解析数值个数
ELEMENT_STRU astElement[]:数值的结构数组指针,含义如下
Struct
{
unsigned int uiElementLength;   //表示uiElementValue占用BIT数,范围1~32
unsigned int uiElementValue;    //从字节流中按顺序解析的数值,用于输出
}ELEMENT_STRU;


输出:参见上述ELEMENT_STRU中uiElementValue描述


返回:void
*/
void Decode
(
 unsigned int uiIutputLen,
 unsigned char aInputByte[], 
 unsigned int uiElementNum,
 ELEMENT_STRU astElement[]
)
{
char *pcStrBuf = NULL;


if (NULL == aInputByte || NULL == astElement || 0 >= uiIutputLen || 0 >= uiElementNum)
{
return;
}


/* 步骤0. 初始化字符串指针 */
pcStrBuf = (char*) malloc (uiIutputLen * BITS_MALLOC_SIZE);
if (NULL == pcStrBuf)
{
return;
}
memset(pcStrBuf, 0, uiIutputLen * BITS_MALLOC_SIZE);


/* 步骤1. 把字节数组aInputByte转为二进制字符串,存入pcStrBuf */
ByteTransToBits(aInputByte, uiIutputLen, pcStrBuf);


/* 步骤2. 根据数值所占位数,截取二进制字符串,获取真实数值 */
BitsTransToNum(pcStrBuf, uiElementNum, astElement);


free(pcStrBuf);


return;
}
阅读(490) | 评论(0) | 转发(0) |
0

上一篇:开心的小明

下一篇:奖金提成

给主人留下些什么吧!~~