zoukankan      html  css  js  c++  java
  • 字节转换

    一、宽字节转窄字节

    char* wstr2asc(const short *pSrc, char *pDest)
    {
    char *pTmp=pDest;
    char ctmp=0;
    short stmp=0;

    if (NULL==pSrc)return NULL;

    while (*pSrc)
    {
    ctmp=*pSrc;
    stmp=*pSrc;

    if (ctmp<0)
    {
    *pTmp++=stmp;
    *pTmp|=(stmp>>=8);
    }
    else
    {
    *pTmp=*pSrc;
    }
    pSrc++;
    pTmp++;
    }
    *pTmp='\0';

    return pDest;
    }

    int wstrlen(const short *pSrc)
    {
    short *pTmp=(short*)pSrc;

    while (*pTmp&&pTmp++);

    return pTmp-pSrc;
    }

    char* wstr2asc(const short *pSrc, const int iSrcLen, char *pDest, const int iDestLen)
    {
    char *pTmp=pDest;
    char ctmp=0;
    short stmp=0;
    int i=0;
    int len=wstrlen(pSrc);
    if (NULL==pSrc)return NULL;

    len=len>iSrcLen?iSrcLen:len;
    len=(len*2)>iDestLen?iDestLen:(len*2);
    while (*pSrc && i<len)
    {
    ctmp=*pSrc;
    stmp=*pSrc;

    if (ctmp<0)
    {
    *pTmp++=stmp;
    *pTmp|=(stmp>>=8);
    i++;
    if (i>=len)
    {
    break;
    }
    }
    else
    {
    *pTmp=*pSrc;
    }
    pSrc++;
    pTmp++;
    i++;
    }
    *pTmp='\0';

    return pDest;
    }

    二、窄字节转宽字节

    short* asc2wstr(const char *pSrc,short *pDest)
    {
    short *pTmp = pDest;
    short tmp = 0;

    if (NULL==pSrc)return NULL;

    while(*pSrc)
    {
    if(*pSrc<0)
    {
    *pTmp=*(pSrc+1);
    *pTmp<<=8;
    tmp=*pSrc;
    *pTmp|=(tmp&=0x00ff);
    pSrc++;
    }
    else
    {
    *pTmp=*pSrc;
    }
    pSrc++;
    pTmp++;
    }
    *pTmp&=0;

    return pDest;
    }

    short* asc2wstr(const char *pSrc, const int iSrcLen, short* pDest, const int iDestLen)
    {
    short *pTmp = pDest;
    short tmp=0;
    int len=0;
    int i=0;

    if (NULL==pSrc)return NULL;

    len=strlen(pSrc);
    len=len>iSrcLen?iSrcLen:len;
    len=len>iDestLen?iDestLen:len;

    while(*pSrc && i<len)
    {
    if(*pSrc<0)
    {
    *pTmp=*(pSrc+1);
    *pTmp<<=8;
    tmp=*pSrc++;
    *pTmp|=(tmp&=0x00ff);
    i++;
    if(i>=len)
    {
    *pTmp&=0x00FF;
    pTmp++;
    break;
    }
    }
    else
    {
    *pTmp=*pSrc;
    }
    pSrc++;
    pTmp++;
    i++;
    }
    *pTmp&=0;

    return pDest;
    }

  • 相关阅读:
    XidianOJ 1096 数的拆分
    XidianOJ 1183 Water Problem: Items divided
    XidianOJ 1182 Chinese Paladin – Qi’s troubles
    XidianOJ 1112 Too stupid
    XidianOJ 1120 Gold of Orz Pandas
    XidianOJ 1177 Counting Stars
    XidianOJ 1076 小W喜欢的数字
    XidianOJ 1095 派对
    XidianOJ 1055 如此遍历
    XidianOJ 1145 数学题
  • 原文地址:https://www.cnblogs.com/ccmfc/p/2275634.html
Copyright © 2011-2022 走看看