Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

MD5 How-to: How To Get the MD5 Hash of Floating Point Types

Applies to: ~>1.0

TPJMD5 provides no direct means of getting the MD5 hash of floating point types. Instead we need to use the untyped overloads of the TPJMD5.Calculate and TPJMD5.Process methods.

Working with untyped data is covered in How To Get the MD5 Hash of Untyped Data

The general principle is the same as for ordinal types - you pass the floating point variable as the first parameter to TPJMD5.Calculate or TPJMD5.Process and the size of the variable as the second parameter.

A single example will suffice. Suppose you have a text file of the text representations of floating point numbers, with just one number per line. The following function gets the MD5 hash of all the (Double) numbers in the file. This hash will be different to taking the hash of the text file. It allows files with different representations of the same number (like 0.12 and .12) to have the same hash.

The example in this how-to only uses TPJMD5.Process because it allows more than one floating point to be added to the hash, but TPJMD5.Calculate can be used in the unlikely event you want to take the hash of just one variable. For details of the differences see Understand the Calculate and Process methods.

function MD5OfFileOfFloats(const FileName: string): TPJMD5Digest;
  F: TextFile;
  Num: Double;
  MD5: TPJMD5;
  MD5 := TPJMD5.Create;
    AssignFile(F, FileName);
    while not EOF(F) do
      ReadLn(F, Num);
      MD5.Process(Num, SizeOf(Num));
    Result := MD5.Digest;

Although this example uses the Double type, it applies equally to any other floating point type, such as Extended.

See Also