如何将ASCII转换成二进制(100分)

  • 主题发起人 黄海庆
  • 开始时间

黄海庆

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将ASCII转换成二进制,请提供函数
 
数字本来在计算机就是以二进制保存。
应该没有转成二进制的。
通过位移可以得到。
 
var
test: int;
test=ORD('A');//则test=65.
 
65同样不是二进制数。
不过,如果要显示二进制数。
可以用字符串方式显示。
 

ORD('A');

binto..
 
function DecToBinStr(n: integer): string;
var
S: string;
i: integer;
Negative: boolean;
begin
if n < 0 then
Negative := true
else
Negative := False;
n := Abs(n);
for i := 1 to SizeOf(n) * 8 do
begin
if n < 0 then
S := S + '1'
else
S := S + '0';
n := n shl 1;
end;
// Delete(S,1,Pos('1',S) - 1);//remove leading zeros
Delete(S,1,length(s)-8);//变成8位
if Negative then
S := '-' + S;
Result := S;
end;
 
var
test: int;
test=ORD('A');//则test=65.
showmessage(IntToBin(test));
{
Returns a binary string representation for an Integer value.
function IntToBin(Value: cardinal): string;
Parameters
Value: cardinal
Cardinal value to be converted.
Returns
String - Binary representation of the Integer value.
Description
IntToBin is a String function used to construct a binary representation
of a 32-bit Integer value. The return value for IntToBin will contain a
string of "0" or "1" characters for each of the bits in the integer Value.
}
 
想不到有这个函数。
真是不好意思。
要uses IdGlobal;
 
完善一下,去掉前面的0:
var
test: integer;
strTemp : string;
i : integer;
begin
test:=ORD('A');//则test=65.//ORD是PASCAL的函数,一般用BYTE
strTemp := IntToBin(test);
for i := 1 to length(strTemp) do
begin
if strTemp = '1' then
break;
end;
delete(strTemp,1,i-1);
showmessage(strTemp);
end;
 
IntToBin要从哪里引用?
 
我前面不是说了吗?
uses IdGlobal;
 
影 子:
好像只有Delphi6 才支持IdGlobal。
我前面用Delphi5没通过。
代码:
[:)][:D]
 
只有DELPHI才有,
如果用DELPHI5,可以自己写一个函数。
只是一般的算法,没什么困难的。
 
接受答案了.
 
顶部