联合成char [英] Union to char

查看:80
本文介绍了联合成char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从客户端向服务器发送数据包.

包装:

  • 数据包的结构具有两个联合
  • 联合头和联合数据
  • 联合数据内部具有很多结构
  • I want to send a packet from client to server.

    The packet:

    • the struct of the packet has two union
    • union header and union data
    • union data has a lot of struct inside
    • struct test1
      struct test2


      那么test1有很多数据

      例子


      Then test1 has a lot of data

      Example

      int num1;
      int num2;


      现在我设置


      Now I set

      packet.data.test1.num1 = 1
      packet.data.test1.num2 = 2


      我的sha1check函数:


      My sha1check function:

      char* sha1check(const char* source){
      TCHAR	stemp[1024];
      _tcscpy(stemp,_TEXT(source));
      CSHA1 sha1;
      sha1.Update((UINT_8*)stemp, _tcslen(stemp)*sizeof(TCHAR));
      sha1.Final();
      sha1.ReportHashStl(srtemp, CSHA1::REPORT_HEX_SHORT);
      return (char*)srtemp.c_str();
      }


      使用此函数获取sha1值,包括packet.data.test1.num1packet.data.test1.num2


      Using this function to get the sha1 value including the packet.data.test1.num1 and the packet.data.test1.num2 value

      推荐答案

      在您的代码中使用的是_tcscpy() and _tcslen(),但是如果您在结构中具有非ascii值(例如,使用int num1),则这些功能将不起作用.
      为了使它起作用,您将需要通过其他方式(可能由sizeof()运算符)来计算数据的长度,或者仅计算基本项目的类型和数量.然后,您可以使用指向结构/联合的开始的指针,并将其强制转换为BYTE类型,然后将其发送到SHA例程.将数据从一个位置复制到另一个位置以更改转换没有任何意义,因为这只会给程序增加不必要的开销.

      In your code you are using _tcscpy() and _tcslen(), but these functions will not work if you have non-ascii values within your structures, which you do (int num1 for example).

      In order for this to work you will need to calculate the length of your data in some other way, possibly by the sizeof() operator, or just counting the type and number of elementary items. You can then use a pointer to the start of your struct/union and cast it to a BYTE type, and send that to the SHA routine. There is little point in copying the data from one place to another just to change the casting, as this merely adds unnecessary overhead to your program.

      写道:​​

      ps:我的英语不好...对不起:(

      ps: my english is poor...sorry :(



      但是您的解释仍然很好.而且您不需要为您的英语道歉;我不希望我能说任何您的母语.



      But your explanation is still good. And you do not need to apologise for your English; I don''t expect I can speak any of your native language.


      我不确定您在这里的意思.结构只是对编译器的描述,允许您从复合字段插入或提取元素.联合是结构或其他元素的组合.单个字符不能被视为更多.

      也许您可以澄清您要实现的目标.
      I am not sure what you mean here. A struct is merely a description for the compiler to allow you to insert or extract elements from a composite field. A union is a composite of structs or other elements. A single char cannot be viewed as anything more than that.

      Perhaps you could clarify what you are trying to achieve.


      可能您需要对char *进行简单的强制转换,无论如何您都没有提供足够的细节来获取有意义的答案.
      :)
      Probably you need a simple cast to char *, anyway you didn''t provide enough details to get a meaningful answer.
      :)


      这篇关于联合成char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆