我要将一个数值转换为四个字节
例如:
var
sbuf:array[1..256] of byte;
将一个数值很大的数值10000000转换为四个字节
现在发送一串字符串:
sbuf[1]:=byte[x];
sbuf[2]:=byte[y];
sbuf[3]:=byte[z];
sbuf[4]:=byte[a];
怎么转换?

解决方案 »

  1.   

    我发送的是16进制的数啊
    例如:
    我将65535转换为四个字节表示(低字节在前,高字节在后)
    sbuf[1]:=byte($00);
    sbuf[2]:=byte($00);
    sbuf[3]:=byte($FF);
    sbuf[4]:=byte($FF);
    这个怎么转换?请赐教!在线等待!
      

  2.   

    X:=65535sbuf[1]:=byte((X and $FF000000) shr 24);
    sbuf[2]:=byte((X and $00FF0000) shr 16);
    sbuf[3]:=byte((X and $0000FF00) shr 8);
    sbuf[4]:=byte((X and $000000FF))
      

  3.   

    看看我的代码:
            sbuf[0]:=chr((x shr 24) and $ff);
            sbuf[1]:=chr((x shr 16) and $ff);
            sbuf[2]:=chr((x shr 8) and $ff);
            sbuf[3]:=chr(x and $ff);
    其实后面的 and $FF 不用也可以的。
    用这个方法速度有保证
      

  4.   

    对了我把sbuf看做为:PChar了,如果是Byte的话,直接用就可以了,无需转换!