![]() ![]() This method cannot be used to convert UTF-16 into UTF-8 or some other non-SQL Server-supported encoding.īelow is an Inline-TVF encapsulating the steps shown above: GOįROM dbo.ConvertBase64EncodedUTF8ToUTF16LE( Please note that this trick only works for converting from various source encodings into UTF-16 Little Endian (as that is how the XML datatype in SQL Server stores strings internally). In a DB with a Hebrew Collation, it gets the following error:Ĭonversion of one or more characters from XML to target collation impossible specified by the default Collation of the current Database:ĬONVERT(XML, '' + In a DB with a Latin1_General Collation it works: This is to VARCHAR, but "success" will depend on the Code Page This is to NVARCHAR, which will always work:ĬONVERT(XML, '' + Où est le café le plus proche? The trick to this trick is that you need to add the declaration (typically omitted) and specify the source encoding: DECLARE NVARCHAR(500) = N'T8O5IGVzdCBsZSBjYWbDqSBsZSBwbHVzIHByb2NoZT8=' ĬONVERT(XML, VARCHAR(500) = CONVERT(VARCHAR(500), 0x4FC3B920657374206C6520636166C3A9206C6520706C75732070726F6368653F The trick is to convert the Base64 decoded bytes, in their text representation (even with incorrectly converted characters) into XML. Hence, ù is the 8-bit version of the two-byte UTF-8 sequence for ù ( 0xC3 and 0xB9).įortunately, it is possible to convert a UTF-8 encoded string into UTF-16, or even into a non-Unicode Code Page (IF the Code Page supports all characters being converted). SQL Server uses UTF-16 Little Endian only for NVARCHAR data, and even for XML. So, decoding the Base64 gives you back the original UTF-8 sequence of bytes. I used these sites to understand how Base64 encoding really works and translated it to AL code.The problem is that you encoded a UTF-8 encoded string into Base64. A great resource can be found here and here. I’m not going into the details of Base64 encoding itself. In that way there is no dependency on any table. For binary data the code works with InStream and OutStream variables. The Codeunit is now supporting both text and binary data. That was another challenge that I just wanted to pick up. Because they were talking about a file I figured the code should not only support strings, but also binary data. And apparently, people were still interested in having the code. However, a new question came up about Base64. So I decided to not publish the code and wait for the next update. At that moment, I already read in a GitHub issue that Microsoft promised to make the Base64 functions on TempBLOB table available in the next update. ![]() Well, it turned out that it was possible and I ended up with a Codeunit that was able to convert a string to Base64 and also back into a string. Just as an excercise and at that moment I didn’t know if I would even succeed. So I decided to write Base64 encoding / decoding routines myself. Net, I figured it was going to take a while before we get Base64 support in AL code. And because those functions on the TempBLOB table are using. Unfortunately, at that time, it was not possible to call the Base64 methods on the TempBLOB table. He is building up a great library of very useful video’s, so make sure to check out his YouTube channel!Ī couple of weeks ago, I was trying to write web service examples in AL code for VS Code. Update June 13th, 2020: In this video, Erik Hougaard discusses this code and improves the performance dramatically by making use of the TextBuilder variable.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |