高位与低位问题

tippchlj 2012-07-20 04:34:25
如何实现把整形236006660高位移到低位后变成70062350?
...全文
618 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tippchlj 2012-07-21
  • 打赏
  • 举报
回复
以后也经常来论坛,向牛人学习学习。
tippchlj 2012-07-21
  • 打赏
  • 举报
回复
各位都是牛人啊,膜拜之情.....
s11ss 2012-07-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
就是字节逆序:

function Bswap32(N: cardinal): cardinal;
asm
bswap eax
end;
[/Quote]
还可以这样(3楼的使用了内存单元寻址,慢):
function ChangeOrder(I: Integer): Integer;
asm
mov cl,ah
mov ch,al
shl ecx,16
shr eax,16
mov cl,ah
mov ch,al
mov eax,ecx
end;
s11ss 2012-07-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
Delphi(Pascal) code
我也来显丑了

uses WinSock;

procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
begin
i:=236006660;
i:=htonl(i);
ShowMessage(IntToStr(i));
end;
[/Quote]那么ntohl也可以,呵呵。
s11ss 2012-07-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
就是字节逆序:

function Bswap32(N: cardinal): cardinal;
asm
bswap eax
end;
[/Quote]果然还是有这个指令,真忘了。。。
frtrnr 2012-07-20
  • 打赏
  • 举报
回复
http://csdnimg.cn/bbs/m/i/red_2.gif
kaikai_kk 2012-07-20
  • 打赏
  • 举报
回复
我也来显丑了

uses WinSock;

procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
begin
i:=236006660;
i:=htonl(i);
ShowMessage(IntToStr(i));
end;
  • 打赏
  • 举报
回复
就是字节逆序:

function Bswap32(N: cardinal): cardinal;
asm
bswap eax
end;
广州佬 2012-07-20
  • 打赏
  • 举报
回复
楼上厉害。

还可以这样:

function ConvetLH(Num: Integer): Integer;
begin
Result := Num div (1 shl 24) + Num mod (1 shl 24) div (1 shl 16) shl 8
+ Num mod (1 shl 16) div (1 shl 8) shl 16 + Num mod 256 shl 24;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('236006660高位移到低位后变成:'+InttoStr(ConvetLH(236006660)));
end;
s11ss 2012-07-20
  • 打赏
  • 举报
回复
function ChangeOrder1(I: Integer): Integer;
begin
Result := (I shr 24) + (I shr 8) and $0000FF00 + (I shl 8) and $00FF0000 + (I shl 24) and $FF000000
end;

function ChangeOrder2(I: Integer): Integer;
var
P: PByte;
begin
P := PByte(@I);
Result := P^ shl 24;
Inc(P);
Inc(Result, P^ shl 16);
Inc(P);
Inc(Result, P^ shl 8);
Inc(P);
Inc(Result, P^)
end;

function ChangeOrder3(I: Integer): Integer;{速度最快}
asm
push eax
xchg [esp+3],al
mov [esp],al
mov cl,[esp+2]
mov [esp+2],ah
mov [esp+1],cl
pop eax
end;
Frank.WU 2012-07-20
  • 打赏
  • 举报
回复
未使用的变量自己去掉即可。
Frank.WU 2012-07-20
  • 打赏
  • 举报
回复

function ConvetLH(Num: Integer): Integer;
var
Arr, Brr: array of Byte;
s: string;
B: Byte;
I: Integer;
begin
s := InttoStr(Num);
SetLength(Arr, 4);
CopyMemory(Arr, @Num, 4);
SetLength(Brr, 4);
for I := High(Arr) downto Low(Arr) do
begin
Brr[3-I] := Arr[I];
end;
Result := PInteger(Brr)^;
end;

{To Do...}
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(InttoStr(ConvetLH(236006660)) + ' - 70062350');
end;

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧