你可以把strType先按照每两个字节拆成数组,然后再把每一项前面补上0加起来成为新的字符串

解决方案 »

  1.   

    timmy3310(Tim):你能否具体一点,或者有更好的办法?
      

  2.   

    下面的代码是我同事的需求,是用C写的,我的任务是按照他的需求,在.net中写成二进制文件
    typedef struct _timetable {
        long type;    
        long time;              // When type==IMMEDIATE, time is ignored;
                                // When type==PERIODIC, time is calculate on-time
        long kind;
        union {
            struct {
                long basetime;
                long period;
                long lasttime;//UI need not consider this
                char cmdline[84];
            } periodic;
            char cmdline[96];
        } u;
        long nextstep;//Following
        char md5sum[16];
    } TIMETABLE;
    .....
    我的代码是这样的:
    objFileStream = New FileStream( MapPath( "TB.dat" ), FileMode.Create )
    objBinaryWriter = New BinaryWriter( objFileStream )

    objBinaryWriter.Write( strType)
    -objBinaryWriter.Write( strPeriod)
    objBinaryWriter.Write( strClass)
    objBinaryWriter.Write( strTime)
    objBinaryWriter.Write( strBaseTime)
    objBinaryWriter.Write( strCommand)
    objBinaryWriter.Write( strNext)objBinaryWriter.Close但我没有考虑字节数问题,比如他要command要96个字节等。
      

  3.   

    you should use specific types, not strings, seehttp://msdn.microsoft.com/library/en-us/cpguide/html/cpconstructssample.asp?frame=truehttp://msdn.microsoft.com/library/en-us/cpguide/html/cpconunionssample.asp?frame=true