Je v Delphi 7 nějaká funkce, která by převedla string jako např. '11010100' na hexidecimální hodnotu 'A80034' a zpět? Případně jak tento postup provést s vícero funkcemi?
Fórum › Delphi
Převod BIN stringu 00101110 na string v HEX formátu

tyhle věci se normálně hledaj na internetu
https://stackoverflow.com/questions/9854336/hex-to-binary-convert
stačí do googlu zadat Hex to BIN Delphi 7
a proč nepoužíváš nejnovější CodeGearRAD Studio ???
nebo taky tohle
https://uloz.to/file/7UGWGsxk/scantybasic-zip
Na internetu jsem hledal a nic jsem nenašel. To co si poslal není co potřebuju. Ta funkce vrací integer ale já mám na vstupu i výstupu string.
Proč tam používají TMemoryStream? Není to zbytečné? Nešlo by to vypustit? Mohl bys mi ukázat implementaci?
Při nejmenším by v Delphi mělo umět StrToInt a IntToHex. Dost možná i ostatní, ale není problém si to dopsat. Takové věci se řeší v prvních týdnech výuky programování.
////////////////////////////////////////
// HEX
// sysutils:
// function IntToHex ( DecimalValue : Integer; MinimumWidth : Integer ) : string;
// function StrToInt ( IntegerString : string ) : Integer;
function HexToInt(hex: string): integer;
begin
result := StrToInt('$' + hex);
end;
////////////////////////////////////////
// BIN
function IntToBin(val: integer; width: integer): string;
begin
result := '';
while val > 0 do
begin
result := IntToStr(val and 1) + result;
val := val shr 1;
end;
while Length(result) < width do
result := '0' + result;
end;
function BinToInt(bin: string): integer;
var
i: integer;
begin
result := 0;
for i := 1 to Length(bin) do
begin
result := result shl 1;
result := result or StrToInt(bin[i]);
end;
end;
////////////////////////////////////////
// BIN HEX
function BinToHex(bin: string): string;
var
one: string;
begin
result := '';
while Length(bin) > 0 do
begin
one := Copy(bin, 1, 8);
bin := Copy(bin, 9); // od 9 do konce, nejsem si jisty jestli to jde napsat neuvedenim delky
result := result + IntToHex(BinToInt(one), 2);
end;
end;
function HexToBin(hex: string): string;
var
one: string;
begin
result := '';
while Length(hex) > 0 do
begin
one := Copy(hex, 1, 2);
hex := Copy(hex, 3);
result := result + IntToBin(HexToInt(one), 8);
end;
end;
Pokud je to zarovnane po 8/16/32 bitech tak proste vemes 4 bity a bud prepocitas, nebo si udelas vyhledavaci tabulku a vyflusnes jeden znak 0..9A..F (technicky vzato 4 znaky jsou jedno 32b cislo, ale ruzny pretypovani se tusim v pascalu delaji slozite)
@gna: díky. Poznámka k str_HexToBin ... neměla by ta smyčka jet po dvou místo po jednom bitu? Mě to tak jak to je stačí, protože budu pracovat s hodnotami od 00 do FF ale jen mě to tak napadlo. J bude tam chyba protože 00FF dá jen nuly, ale to nevadí, jsem rád za ukázku, později když tak opravím.
Žadné navyšování tam není.
while Length(hex) > 0 do // dokud neni `hex` prázdný
begin
one := Copy(hex, 1, 2); // vezmu první 2 znaky
hex := Copy(hex, 3); // a odmažu je
Napsal jsem to demetně, aby tam nebylo nic moc k přemýšlení... pro někoho.
#9 gna
nemá tam místo
hex := Copy(hex, 3); // a odmažu je
být
hex := Copy(hex, length(hex)-3); // a odmažu je
?
EDIT: asi nemá já blbě pochopil Copy(hex,3);
já Copy nepoužívám, tedy jestli to je ve významu kopíruj z od pozice počet, pokud není počet tak do konce?

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
String to hex ebo Byte()"string" — založil Roman Uherek
Prevod Dec -> Bin — založil Mutagen
Převod do pdf formátu — založil Pebble
Convert hex '0x00' to string - nefunkčná :( — založil David
Převod hex do dec — založil cdx