例如:定义一个CString a
     若a="12345678"
     如何把a变成0x78,0x56,0x34,0x12之后用于通信

解决方案 »

  1.   

    cahr pa[100];strcpy(pa,(char *)a);for(i=0;i<strlen(pa);++i)
    {
      //  做你该做的事;}
      

  2.   

    int n=atoi(a);
    char* buf = &n;
    buf的字节顺序就是你要求的顺序如果a中包含16进制数据,则用strtol
      

  3.   

    请问buf的字节顺序,是怎么决定的呢?
      

  4.   

    因为buf指向int类型,因此buf的字节顺序和int类型的字节顺序相同,int的字节顺序就是低字节在低地址
      

  5.   

    如果CString a="abcdefgh" 还能用atoi吗?
      

  6.   

    //把输入缓冲数据转换成可显示的字符串
    //注意输出缓冲的大小需要足够大
    void initstr(BYTE* inbuf,BYTE* outbuf, int number)
    {
    BYTE *p;
    char c[10];
    p=outbuf;
    for(int i=0;i<number;)
    {
    _itoa((unsigned int)(*inbuf++), c, 16);
    if(c[1] == 0)
    {
    *p++='0';
    *p++=c[0];
    }
    else
    {
    *p++=c[0];
    *p++=c[1];
    }
    *p++=0x20;  //空格
    i++;
    if(i%20==0)
    {
    *p++=0x0d;
    *p++=0x0a;
    }
    }
    }参考一下上面的程序,自己写一个吧
      

  7.   

    谢谢各位:我现在解决了换位即CString a="12345678"
    可以转化为 CSting 型的“78563412”但不知道怎样再把它围化为:0x78,0x56,0x34,0x12
    不好意思,还望不嫌烦
      

  8.   


    字节序转换有专门的函数:
    htons  host data type to network short
    htonl  host   .        to network long
    ntohl  network .       to host long
    ntohs  network  .      to host short
      

  9.   

    CString a="12345678";
    CString szMid;
    int buf[4];
    int j=0;
    for(int i = a.GetLength();i>0;i-=2){

    szMid=a.Mid(i-2,2);
    sscanf(szMid.GetBuffer(0),"%x",&buf[j]);
    j++;
    }
    szMid.Format("0x%x,0x%x,0x%x,0x%x\n",buf[0],buf[1],buf[2],buf[3]);
    MessageBox(szMid);
      

  10.   

    以下代码可以实现,被字符串设为16进制字符形式。
      
    CString str = _T("a12345678");
    BYTE buf[256] = "\0";int lenght = str.GetLength ();
    str.MakeLower();if(lenght%2)//双字节对齐
    {
       str.Insert(0,"0");
       lenght++;
    }int index = 0;while (index <= lenght/2)
    {
       if (str[index] >= '0' && str[index] <= '9' 
    || str[index] >= 'A' && str[index] <= 'F')
       {
    int iVal = 0;
    sscanf((LPCSTR)(str.Mid(lenght-(index+1)*2,2)),"%x",&iVal);
    buf[index] = (BYTE)iVal;
       }
       index++;
    }
      

  11.   

    谢谢各位
    但我的问题是:如何把CString a="12345678"转化为b[]={0x12,0x34,0x56,0x78}
    完了,这下肯定没人理我了,但我真的被自己搞晕头了,所以转来转去就转不出来还望各位多多原谅
      

  12.   

    首先声明,我不要你的分。
    第二点,麻烦你说明清楚一点:
        是要把字符串CString a = "12345678" =>CString b = "0x12,0x34,0x56,0x78";
        或是要把字符串CString a = "12345678" =>char b[] = {0x12,0x34,0x56,0x78};
      

  13.   

    你的不是数据格式转化,只是截取字段吧,用一般的字符串处理函数就可以了
    msdn里面有截取字符串的一部分的函数的用法
      

  14.   

    我不理解CString a="12345678"怎么可能转化为b[]={0x12,0x34,0x56,0x78}说真的,我只能猜你的意思
    而且也没有猜明白
      

  15.   

    to 大家:不好意思,是我没讲清楚,对不起了
    我要的是:把字符串CString a = "12345678" =>char b[] = {0x12,0x34,0x56,0x78}
      

  16.   

    char b[] = {0x12,0x34,0x56,0x78}???
    更不明白了,有这样的数组表示吗?
    继续关注!
      

  17.   

    肯定不是直接转换就出得来的,要写函数,可我总写得不对,没办法,比较菜
    哦,是把字符串CString a = "12345678" =>int b[] = {0x12,0x34,0x56,0x78}
      

  18.   

    是想int  b[] = {0x12,0x34,0x56,0x78}?
    还是char *b[] = {0x12,0x34,0x56,0x78}?
    楼主,我被你彻底搞崩溃了,不再关注了,闪人
      

  19.   

    每两个字符截取,转化为10进制的int(这个以上网友都说的很详细了,不再说了,再说就烦死了)
    int nX;//得到的10进制的int
    nX = nX/10*16 + nX%10;
    nX即是
      

  20.   

    CString str = "12345678";
    unsign char buf[256] = "\0";
    int index = 0;while (index <= lenght/2)
    {
       int iVal = 0;
       sscanf((LPCSTR)(str.Mid(index*2,2)),"%x",&iVal);
       buf[index++] = (BYTE)iVal;
    }现在buf的内容为b = {0x12,0x34,0x56,0x78}
      

  21.   

    你的意思是把字符串转化为整型数组,还是数值?
    如果是数组:
    CString a = "12345678";
    int n=atoi(a);
    char* buf = &n;
    int b[4]
    char c[4];
    for(i=0;i<4;i++){
      b[i] = *buf;
      c[i] = *buf;
      ++buf;
    }
      

  22.   

    CString a = "12345678";
    int b[10];
    b[0] = atoi(a.left(2));// b[0] = 12 ,but not 0x12
    //then ,how to convert 12 to 0x12?int c[10];
    c[0] = b[0]/100 * 256 + b[0]/10 * 16 + b[0]%10;  //c[0]的值应该是0X12了,是十进制表示的,应该是你要的了吧
    (可是个人感觉这样做好变态)
      

  23.   

    16进制可以把wsz1995(你好)的方法简单修改一下CString a = "12345678";
    char* p;
    int n=strtol(a, &p, 16);
    char* buf = &n;
    int b[4]
    char c[4];
    for(i=0;i<4;i++){
      b[i] = *buf;
      c[i] = *buf;
      ++buf;
    }数组b即为所求
      

  24.   

    问题解决谢谢各位,特别谢谢Powerdix(大山) .blackblue(跌跌撞撞) 还有sjzxyg(小小鸟) 和aben456(风轻扬,吹皱一池春水) .wsz1995(你好) ,真的很谢谢你们