binary_values.c

来自「spiht的压缩解压缩c编写的」· C语言 代码 · 共 73 行

C
73
字号
#include "spiht.h"
#include "spihtdecode.h"

int QccBinaryCharToInt(const unsigned char *ch, unsigned int *val)
{
  int byte_cnt;

  if ((val == NULL) || (ch == NULL))
    return(0);

  /*  MSB first  */
  *val = 0;
  for (byte_cnt = 0; byte_cnt < QCC_INT_SIZE; byte_cnt++)
    {
      *val <<= 8;
      *val |= ch[byte_cnt];
    }

  return(0);
}

int QccBinaryIntToChar(unsigned int val, unsigned char *ch)
{
  int byte_cnt;

  if (ch == NULL)
    return(0);

  /*  MSB first  */
  for (byte_cnt = QCC_INT_SIZE - 1; byte_cnt >= 0; byte_cnt--)
    {
      ch[byte_cnt] = (val & 0xff);
      val >>= 8;
    }

  return(0);
}

int QccBinaryCharToFloat(const unsigned char *ch, float *val)
{
  /*  Relies on floats being QCC_INT_SIZE bytes long  */
  union
  {
    float d;
    unsigned int i;
  } tmp;

  if ((val == NULL) || (ch == NULL))
    return(0);

  QccBinaryCharToInt(ch, &(tmp.i));
  *val = tmp.d;

  return(0);
}

int QccBinaryFloatToChar(float val, unsigned char *ch)
{
  /*  Relies on floats being QCC_INT_SIZE bytes long  */
  union
  {
    float d;
    unsigned int i;
  } tmp;

  if (ch == NULL)
    return(0);

  tmp.d = val;
  QccBinaryIntToChar(tmp.i, ch);

  return(0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?