Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert unicode characters to ascii codes in delphi 7?

Yes we're talking about ASCII codes. My appologies I'm not the Delphi dev here.

like image 264
UnkwnTech Avatar asked Dec 09 '22 22:12

UnkwnTech


2 Answers

For Delphi 7, I'd get the free Unicode Library by Mike Lischke who is the author of Virtual Treeview.

The libary includes a lot of conversion functions to go to and from Unicode, so you can use the ones that make most sense in your application.

Or you can upgrade to Delphi 2009 which has built-in encoding routines, and its own library of conversion functions.

like image 106
lkessler Avatar answered Dec 24 '22 00:12

lkessler


Let's get a few things straight. Character set (charset) and character encodings are two related but different concepts. A character set is an abstract list of characters with some sort of integer character code associated. Then there are character encodings, which is basically an algorithm that describes how the characters are represented in bytes.

ASCII acts as both the character set and encoding. It uses 7 bits to express 128 characters (94 printable). Unicode on the other hand is a character set, expressing 1,114,112 code points. There are several encodings to represent Unicode strings but most notable ones are UTF-8, UTF-16, UTF-16LE, and UTF-32. In other words, a single Unicode character can be represented in different ways depending on the encodings.

How can I convert unicode characters to ascii codes in delphi 7?

I think the question could be interpreted in two ways.

  1. I have a Unicode string in some encoding that only includes ASCII printable characters. How can I convert the string into a byte array of ASCII encoding?

  2. I have a Unicode string in some encoding that also includes non-ASCII printable characters such as Chinese characters. How can I encode the string into a ASCII encoding without losing information, and later decode it back to the original Unicode string?

If you mean the first, you can load the Unicode string into WideString like Osman is saying and do

var
  original: WideString;
  s: AnsiString;
begin
  s := AnsiString(original);

If you mean the second, you would need a generic encoding algorithm like Base64 encoding. You can use DCPBase64.pas included in David Barton's DCPcrypt v2 Beta 3.

like image 28
Eugene Yokota Avatar answered Dec 24 '22 02:12

Eugene Yokota