Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determinate if character is printable

I want to develop a hex-dump-view and have problems with characters which are not printable in the current active ANSI codepage (CP_ACP). How do I detect them and print a dot instead?

My function currently looks like this:

function HexChar(j: byte): AnsiChar;
begin
  if j < $20 then result := '.'

  // Dirty workaround which only supports the undefined characters of Windows-1252
  else if (GetACP=1252) and ((j=$81) or (j=$8D) or (j=$8F) or (j=$90) or (j=$9D)) then result := '.'

  else result := AnsiChar(j);
end;

Using Delphi XE4 and the font Courier New, the characters $81, $8D, $8F, $90, $9D are invisible. GetACP returns 1252, so I am using Windows-1252 . According to Wikipedia, the range I discovered is not defined in Windows-1252. How can I check if the character with ordinal value j is defined in the current active codepage or not?

like image 842
Daniel Marschall Avatar asked Dec 01 '25 02:12

Daniel Marschall


1 Answers

Call GetStringTypeW function which supports detailed character classification.

It's also possible to use GetStringTypeEx or deprecated GetStringTypeA functions, but both just calls GetStringTypeW according to MSDN. Also, GetStringTypeEx hides difference between ANSI and Unicode versions and recommended by MSDN for character type retrieval.

Another possibility is to use TCharacter.GetUnicodeCategory() method from character.pas.

like image 76
ThinkJet Avatar answered Dec 04 '25 00:12

ThinkJet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!