问题
I can convert string to crc16 but I need convert crc16 to string.Is it possible?
function TForm1.CRC_16(Icerik: string): word;
var
valuehex: word;
i: integer;
CRC: word;
Begin
CRC := 0;
for i := 1 to length(Icerik) do
begin
valuehex := ((ord(Icerik[i]) XOR CRC) AND $0F) * $1081;
CRC := CRC SHR 4;
CRC := CRC XOR valuehex;
valuehex := (((ord(Icerik[i]) SHR 4) XOR LO(CRC)) AND $0F);
CRC := CRC SHR 4;
CRC := CRC XOR (valuehex * $1081);
end;
CRC_16 := (LO(CRC) SHL 8) OR HI(CRC);
end;
This function convert string to CRC16 .
回答1:
No it's not possible, due to the Pigeonhole Principle. In order to be able to convert from CRC16 to a string you need to have a mapping function from the set of CRC16 values to the set of string values.
Since there are only 65536 (216) possible CRC16 values, and there are considerably more than 65536 possible values that a string can have, it's not possible to define a 1-to-1 correspondence, so converting from a CRC16 back to a string is impossible.
To put it another way, the string to CRC16 function is many-to-one: many different strings map to the same CRC16 value (i.e. they all get placed in the same "pigeonhole"). So if you start with just the CRC16 value, how will you know which of those many possible strings was the original string?
One special case exception to this is if the string is only allowed to have a certain set of possible values, and each value in the set maps to a unique CRC16 value. In that case the function is reversible, as long as you know the set of possible string values.
来源:https://stackoverflow.com/questions/29912220/crc16-to-string