[Solved] Convert UTF8String to/from multi-bite codepage like Shift-JIS lazarus / 61-Beginners ---------------------------------------------------------------------- AlexTP · 2022-12-24 19:20 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SetCodePage(var s : RawByteString; CodePage : TSystemCodePage; Convert : Boolean = True);It allows to convert UTF8String var to some codepage, e.g. Shift-JIS, if I use correct codepage number. Is it correct? If so, how can I convert UTF8String containing Shift-JIS text, BACK to UTF8? How to do backward convertion? ---------------------------------------------------------------------- PascalDragon · 2022-12-24 20:38 Pass CP_UTF8 as code page for SetCodePage to convert to UTF-8. ---------------------------------------------------------------------- Jorg3000 · 2022-12-24 21:46 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var s: RawByteString;SetCodePage(s,CP_UTF8);  // from any codepage to UTF-8SetCodePage(s,932);  //  from any codepage to 932 = Shift-JIS ? ---------------------------------------------------------------------- Zoran · 2022-12-24 22:02 Quote from: Jorg3000 on December 24, 2022, 10:46:44 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var s: RawByteString;SetCodePage(s,CP_UTF8);  // from any codepage to UTF-8SetCodePage(s,932);  //  from any codepage to 932 = Shift-JIS ?  I would first set code page to source code page without converting (third parameter false), and then set code page to destination code page with third parameter true. See here: https://forum.lazarus.freepascal.org/index.php/topic,45589.msg322552.h tml#msg322552 ---------------------------------------------------------------------- PascalDragon · 2022-12-25 15:06 Quote from: Zoran on December 24, 2022, 11:02:47 pmI would first set code page to source code page without converting (third parameter false), and then set code page to destination code page with third parameter true. See here: https://forum.lazarus.freepascal.org/index.php/topic,45589.msg322552.h tml#msg322552 You should only do this if you really know that the string data contained in the string is encoded as the source code page. If it's encoded as something else and then you convert to another code page you'd get an incorrect conversion. ---------------------------------------------------------------------- Zoran · 2022-12-25 18:54 Quote from: PascalDragon on December 25, 2022, 04:06:08 pmYou should only do this if you really know that the string data contained in the string is encoded as the source code page. If it's encoded as something else and then you convert to another code page you'd get an incorrect conversion. I assumed this was the use case we are talking about. Anyway, for completeness: 1. If you don't know what the actual encoding of the data in source string is, but you can assume that the source string has its code page assigned according to data it contains, then you should skip the first line (with third parameter false), and just do "one line conversion". 2. If you can assume the real encoding of the data the string is filled with (*), and you are not sure about the codepage of the string (how fpc assumes that the string is encoded), then you should do the "two lines" conversion I proposed. (*) for example, when you want to let the user of the application choose the encoding of the text file to load, and then show that text in a LCL control (as in the forum topic I linked to).