delphi - How can I reverse string to bin? -


how can convert bin string? example:

 string:='s';----------->bin:='0011'; 

how convert reverse?

my stringtobin code is:

function strtobinstr( astring: string ): string; var : integer; begin := 1 length( astring ) result := inttobin( byte(astring[i]), 4 ); end;  function inttobin(avalue, bits: integer): string; var : integer; begin := bits-1 downto 0 result := result + copy( '10', word(((1 shl i) , avalue) = 0)+1, 1 ); end; 

this may help:

function  inttobin( const value: longint; digits: byte;   const spaces: boolean ): ansistring; begin   if digits > 32     digits := 32;   setlength( result, digits );   result := '';   while digits > 0   begin     if (spaces) , ((digits mod 8) = 0)       result := result + #32;     dec(digits, 1);     result := result + inttostr((value shr digits) , 1);   end; end;  function bintoint( value: ansistring ): longint; var   ctmp: ansichar;   lictr, lilen: longint; begin   value := ansistring(stringreplace(value, #32, '', [rfreplaceall]));   lilen := length(value);   ctmp := value[lilen];   dec(lilen);   result := strtoint(ctmp);   lictr := 1;   while lilen > 0   begin     ctmp := value[lilen];     dec( lilen );     result := result + (strtoint(ctmp) shl lictr );     inc(lictr);   end; end; 

sample use:

procedure tform1.formshow(sender: tobject); var   teststr: ansistring;   i: integer;   temp: ansistring; begin   teststr := 'abc';   temp := '';   := 1 length(teststr)     temp := temp + inttobin(ord(ansichar(teststr[i])), 8, false);   showmessage('temp = ' + temp);    teststr := '';   := 1;   while < length(temp)   begin     teststr := teststr + ansichar(bintoint(copy(temp, i, 8)));     inc(i, 8);   end;   showmessage('teststr = ' + teststr); end; 

as said in comment original question, think terrible idea, these work.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -