Coding in TCL

Source: Internet
Author: User

In TCL, UTF-8 encoding is used by default, and all input strings are eventually converted to this encoding. This causes a problem, usually in different systems using the editor to save the default encoding is different, such as ns in the Tcl files are provided by UTF-8 encoding, in simplified Chinese Windows, the default saving encoding is gb2312 (cp936 ).

When we open the Tcl document in NS on Windows, first convert it to gb2312 and then modify it. After modification, if there are no special settings, save it as a gb2312 file. If you only use English in this file, it will not be difficult because it remains unchanged in different encodings. (!!! Defeat the English privilege !!!) If you use Chinese or other languages directly in these files, it may cause garbled characters or even program execution failures. 1. There are several solutions for encoding in files: one is to save the Tcl file as utf8 encoding after modification. However, some Chinese characters converted in this way cannot be displayed. The other is to use the encoding command in TCl. When garbled during use, you can do this: Set string [encoding convertfrom cp936 "test string"] Where cp936 must be consistent with the encoding saved in the file, if you use UTF-8 encoding, then it is changed to UTF-8. Replace the string with $ string. However, this method does not seem to pass when processing UTF-8 files in vs2005, the reason is unknown. If it is saved as cp936, Chinese characters can be displayed. In actual use, some Chinese characters cannot be displayed. It seems that the gets command was called while reading the file, and the command was incorrectly encoded. Of course, these two methods are the simplest. If your Chinese characters can be displayed, congratulations! Everything is done. If neither method works, we use the last one: set title [encoding convertfrom cp936 "/UCE/uc4/UBC/UFE"] $ M add command-label $ title-command "puts {file}" set title [encoding convertfrom cp936" /ub0/Uef/ud6/UFA "] $ M add command-label $ title-command" puts {help} "in which CE C4 BC Fe is the cp936 encoding of the word" file "b0 EF D6 fa Is the cp936 encoding of the word "help". This encoding can be seen in a hexadecimal editor such as uedit (enter Chinese Characters in text mode, in hexadecimal format ). Or you can write a short C code to convert it. 2. For Encoding Problems in mixed tcl and C Programming, see the following code to convert int _ tmain (INT argc, _ tchar * argv []). {// use this statement to use all encoding tcl_findexecutable (argv [0]); tcl_interp * interp = tcl_createinterp (); // set the Tcl variable value in C string text = "today's weather legends are good like"; tcl_setvar (interp, "text", text. c_str (), tcl_global_only); tcl_setvar (interp, "RET", "", tcl_global_only ); // encode and convert the string for other processing // cp936 indicates the encoding of the C file // the result of the computation is stored in the $ RET variable string code = "set text [Encoding Convertfrom cp936 $ text]/n set RET [String range $ text 1 5] "; tcl_eval (interp, code. c_str (); // in C, get the $ RET variable value char * ret = 0; ret = (char *) tcl_getvar (interp, "RET", tcl_global_only ); // utf8 encoding is used in the middle of the Tcl operation, and the final result is stored with encoding. // If you want to operate in C code, you must convert it to Unicode or ASCII. // The order of the two statements cannot be reversed, that is, the statements must be converted to Unicode and then to ASCII (as needed ). // The encoding cp_utf8 and cp_acp used in the conversion cannot be changed wchar_t wchar [1024]; char ASCII [1024]; int n =: multibytetowidechar (cp_utf8, mb_err_invalid_chars, RET, strlen (RET), wchar, 1024); n =: widechartomultibyte (cp_acp, wc_compositecheck, wchar, N, ASCII, 1024, null, null); ASCII [N] = 0; cout <"eval Result:" <ASCII <"<n <" byets) "<Endl; System (" pause "); tcl_deleteinterp (interp );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.