HexToBin

4 отговора 264 прегледа
#1

HexToBin

Zdravejte, imam edno pitane: znachi kak moga ot shesnaisetichno chislo da napraviq dvoichno i da rabotia s nego bit po bit. Demek, ako imam v edin string D7 v shesnaisetichno, kak moga da go napraviq pak v string obache ne D7 ami tam 10010101, v dvoichen vid?

Отговор
в отговор на wizard_1 #2

Re: HexToBin

[code]
function IntToBin(const X:Integer):AnsiString;

{Convert any integer into a 32 byte right justified 1/0 string. Use
LTrim to remove leading zeros if desired.}

begin
SetLength(Result,32);
asm
Push EDI
Push EAX

Mov EAX,@Result
Mov EDI,[EAX] //de-reference Result pointer
Mov EDX,X //restore X
Mov ECX,32
Cld
Mov AL,48
@L1:
And AL,48
Shl EDX,1
Jnc @L2
Or AL,1
@L2:
Stosb
Dec ECX
Jnz @L1
// Loop @L1
Cld
Pop EDI
Pop EAX
end;
end;


function BinToInt(const Source:AnsiString):Integer;

{Converts a right justified 1/0 binary string into an integer.}

asm
Push ESI
Xor EDX,EDX //Default return
Or EAX,EAX //null pointer
Jz @Done
Mov ESI,EAX //Move address to ESI
Mov ECX,[ESI-4] //String Length into ECX
Jecxz @Done //Abort on null
Cld //make sure we go forward
@L1:
Lodsb //load a byte from string
Sub AL,48 //adjust for zero
Jz @L2
Shr AL,1
@L2:
Rcl EDX,1
@L3:
Dec ECX
Jnz @L1
@Done:
Mov EAX,EDX

Pop ESI

end; //and we're outta here


function HexToInt(const Source:AnsiString):Integer;

{Convert a hexidecimal string into an integer. Prefixes and otherwise
invalid characters (any other than 0-9,A-F) are ignored.

The standard Pascal way to achieve this is to prepend '$' to the string
and use StrToInt while checking for exceptions. This function eliminates
the need to prepend and returns zero if string is null.}

asm
Push ESI //Save the good stuff
Or EAX,EAX
Jz @Done
Mov ECX,[EAX-4] //String Length into ECX
Xor EDX,EDX //Default return
Jecxz @Done //Abort on null string
Mov ESI,EAX //Address into ESI
Xor AL,AL //clear AL
Cld //make sure we go forward
@L1:
Lodsb //get the first byte into AL
Cmp AL,48 //less than 0 ?
Jb @L3 //yes, then ignore
Sub AL,48 //adjust for 0-9
Cmp AL,10 //OK ?
Jb @L2 //yes, then continue
Sub AL,7 //no, then adjust for A-F
Cmp AL,15 //OK ?
Ja @Done //no, then abort
@L2:
Shl EDX,4 //make room for 4 bits
Or DL,AL //add AL to EDX
@L3:
Dec ECX
Jnz @L1

@Done:
Mov EAX,EDX //return 4 bytes into EAX
Pop ESI

end; //and we're outta here

[/code]

Отговор
в отговор на wizard_1 #3

Не знам как от D7(215) да направиш 95(149)

Не знам как от D7(215) да направиш 95(149)
А ако приблемът ти е грешния пример:
Направи си таблица:
'0': '0000'
'1': '0001'
...
'F': '1111'
и сетне си замени символите.

А как да извършваш действията със стрингове - ами символ по симвл. Но според мен е по-добре да ги превръщаш в числа, така да извършиш действията и да ги правиш пак на стрингове.

Отговор
в отговор на wizard_1 #4

Re: HexToBin

Например така :

function HexToBinStr (s:string):string;
var
i : byte;
st : string;
begin
Result := '';
st := '';
for i:= 1 to 2 do
begin
case s of
'0' : st := '0000';
'1' : st := '0001';
'2' : st := '0010';
'3' : st := '0011';
'4' : st := '0100';
'5' : st := '0101';
'6' : st := '0110';
'7' : st := '0111';
'8' : st := '1000';
'9' : st := '1001';
'A' : st := '1010';
'B' : st := '1011';
'C' : st := '1100';
'D' : st := '1101';
'E' : st := '1110';
'F' : st := '1111';
end;
Result := Result + st;
end;
end;

Ама както казва NikB - няма да получиш 10010101 като резултат от Д7

----------
По склона към връх Фудзи плахо пълзи охлюв към самото небе.
Отговор
в отговор на wizard_1 #5

Re: HexToBin

Ако стринга ти е винаги с 2 шестнадесетични цифри:


function TwoHexToEightBits(s:string):string;
var n,k:integer;
begin
n:=StrToInt('$'+s);
for k:=7 downto 0 do Result:=Result+IntToStr((n and (1 shl k)) shr k);
end;


Отговор