This is a Jerry 2022 The second original article in , This is also the first official account. 370 Original articles .
A friend asked me this question on Zhihu before , I think it's very representative , Therefore, a special article is used to describe some relevant knowledge points .
Let's first look at the specific problems encountered by this friend .
use Postman Call the third-party interface , The Chinese characters inside can be displayed normally .
However, when used ABAP Of HTTP Tool class CL_HTTP_CLIENT Of response->get_data( ) After reading the response , Find the Chinese characters inside , for example " Successful visit " It's garbled :
First of all, make it clear , since Postman It can correctly display the Chinese content in the response data , explain API provider There is no problem , This garbled code problem occurs in the receiver , namely ABAP The programming implementation of the code needs to be adjusted .
We just need to find out the reason for the garbled code , Can be targeted for repair .
Last century 60 years , The United States has developed a set of character codes , The one-to-one mapping relationship between English characters and binary bits is defined , be called ASCII code . Graphic display of a symbol , This behavior associated with its binary storage bits , It is called character coding .ASCII It is the simplest character set and character encoding method .
One byte has 8 position ,2 Of 8 The second party is 256, therefore 1 Bytes can only represent 256 Symbols , The total number of Chinese characters exceeds 10 m , Obviously it doesn't work 1 Bytes to store .
In addition to the familiar English characters and Chinese characters , There are many words with a longer history , Like Egyptian hieroglyphs :
And Jay Chou 《 Love is before the West dollar 》 The cuneiform mentioned in :
Is there such a way of computer coding , Can you include these strange symbols ? Yes , This is it. Unicode, As its name implies ,Unicode Each character of the world's languages is assigned a unique code , To meet cross language needs 、 Cross platform text information conversion .
We according to the Unicode Encoding table , You can find a character corresponding to Unicode code , Like Chinese characters " Wang " Corresponding Unicode Encoded as 00006C6A.
6C6A The binary representation of 0110 1100 0110 1010, Need two bytes to store . Other symbols , It may take three or even four bytes to store .
On the other hand , For what already exists in ASCII English characters in the coding table , Only 1 One byte can store . If Unicode It is mandatory that each character be stored according to the maximum required storage space , namely 4 Bytes to store , Obviously, for English characters , It means a great waste of space .
therefore ,Unicode Only define the mapping relationship between characters and their encoding . How many bytes do these codes take to store , from Unicode Specific implementation mode , such as UTF-8,UTF16 Wait to decide .
UTF-8 It's a variable length encoding , Use 1 To 4 A byte represents a character , The symbols are different , The length of bytes used for storage is also different . such as " Wang " Of UTF-8 The code value is E6B1AA, Three bytes are required to store .
according to SAP Help document ,ABAP use UCS-2 Encoding mode , Can be seen as UTF-16 Subset , because UCS-2 I won't support it UTF-16 Of surrogates Some special symbols defined in the interval .
So-called UTF-16, That is, all characters are fixed in two bytes .
As can be seen from the table below ,UTF-16 Divide again UTF-16BE and UTF-16LE Two ways of implementation . In Chinese " Wang " Of Unicode Encoding value 6C6A For example , If 6C Low address stored in memory ,6A High address stored in memory , This is it. Big Endian That is, the big tail sequence ( Sometimes translated as big head , Big end ) storage , And vice versa Little Endian That is, the storage mode of small tail order .
These two names come from the English satirical fable writer Swift's 《 Gulliver's Travels 》. The Lilliput in the book broke out in civil war , The cause of the war is that people argue that when eating eggs, they should start from the big end (Big Endian) Knock one end open , Or from the small head (Little Endian) knock open .
that ABAP Of UCS-2(UTF-16 Subset ), What is the BE Storage or LE Storage ? Try and you will know .
In my system , The answer is UTF-16LE.
Another way , Check the system class directly CL_ABAP_CHAR_UTILITIES Properties of ENDIAN. stay Jerry In the system , The value of this property is L, representative Little Endian:
We learned this knowledge , Then fix the garbled code problem described at the beginning of the article .
Observe carefully Postman call API Return result of , I found another important message :charset=GB18030, intend API Response data take GB18030 Character set encoding .
Chinese characters " interview " Of GB18030 The code value is B7C3, Completely different from UTF-16LE Coded value in BF8B.
If we were ABAP In the code , By default UTF-16LE The way to read a according to GB18030 Coded symbols , Of course, you won't get the desired results . This decoding method is shown in the figure below 55 Yes get_cdata Method , Finally, there will be garbled code .
The right way , Take No 57 That's ok get_data, Return to one 16 Binary data stream , The type is xstring:
In this 16 In the binary data stream , We have seen Chinese characters " interview " and " ask " Corresponding GB18030 Encoding value .
The rest is easy , Using character sets GB18030 Decode this data stream .
Let's first open the database table TCP00, According to the key words 18030 Query table fields CPCOMMENT:
obtain GB18030 Corresponding SAP Code Page by 8401:
In the following code , Pass in 8401, Variable lv_binary What's stored is 16 Binary data stream , Variable lv_text The storage is based on GB18030 Of API Response content :
You can see that the garbled code has disappeared , stay ABAP The content displayed in the program has been compared with Postman Exactly the same as observed in .
I hope the example introduced in this article , To everyone in ABAP It is enlightening to deal with the problem of Chinese garbled code in , Thank you for reading .
Jerry Of ABAP special collection of works
- Jerry Of ABAP, Java and JavaScript Stew
- ABAP What should developers learn in the future
- Jerry 2017 May Day holiday in :8 A classic sort algorithm ABAP Realization
- Jerry Of ABAP A collection of original technical articles
- 300 That's ok ABAP The code implements the simplest blockchain prototype
- Use Java+SAP Cloud platform +SAP Cloud Connector call ABAP On-Premise Functions in the system
- stay SAP Cloud platform CloudFoundry Consumption in the environment ABAP On-Premise OData service
- ABAP vs Java, Breaststroke vs free style
- Chat C Language and ABAP
- Use it by hand ABAP Channel Develop some gadgets , Improve the efficiency of daily work
- I use ABAP The boring things I've done
- Don't like SAP GUI? Try it on Eclipse Conduct ABAP Develop
- Use Visual Studio Code Write and activate ABAP Code
- Yours ABAP Has the program given light to Buddha ? To have a try Jerry This little trick
- stay SAP Cloud platform ABAP Write the first paragraph in the programming environment ABAP Program
- SAP Officially released ABAP Programming specification
- ABAP Code Inspector Those hidden features , Do you know ?
- It's still in use ABAP Conduct SAP Second development of products ? Let's understand this new concept of secondary development
- ABAP Netweaver The parasitic programming languages in the body
- from SAP A blog in the community started , Chat SAP The feelings behind the product naming
- In the clouds ABAP Restful Service development
- How to be in SAP Cloud platform ABAP In the programming environment CDS view Exposed as OData service
- Use abapGit stay ABAP On-Premises Systems and SAP Cloud platform ABAP Code transfer between environments
- 30 Minutes Restful ABAP Programming The model development supports the addition, deletion, modification and query Fiori application
- Jerry Let's show you Restful ABAP Programming Model series two :Action and Validation The implementation of the
- Jerry Let's show you Restful ABAP Programming Model series three : Cloud ABAP Application debugging
- SAP On the cloud platform ABAP How to consume third-party services in programming environment
- ABAP It's time for developers to go to the cloud - Now you can use it for free SAP Cloud platform ABAP A trial version of the environment
- Learning without thinking is useless - SAP Cloud platform ABAP The origin and applicable scenarios of programming environment
- SAP Trident application in cloud platform
- How to base on Restful ABAP Programming Model development and deployment of a support to add, delete, modify and query Fiori application
- SAP 2019 TechEd Key Note Reading : Under the cloud age SAP How to do secondary development for employees ?
- What are they? ABAP Keywords and Syntax , here we are ABAP You can't use it in the cloud environment ?
- ABAP Finally, the development environment supports automatic formatting with hump naming ABAP Variable name
- utilize ABAP 740 New keywords for REDUCE Finish a practical task
- A period of shivering ABAP Code
- Yesterday's Halloween ABAP Monster code puzzle , The answer is published
- To introduce a new method in the field of ABAP The method of efficient copy of inner table in kernel mode
- Use SAP Cloud Application Programming Model development OData A practical example of
- When ABAP Meet Prometheus
- Use ABAP Draw scalable vector
- ABAP Development environment syntax highlights things
- SAP Seven weapons of error message debugging : So that all error messages can be located
- Use ABAP operation Excel Several ways to
- SAP GUI Favorite transaction code management tool in
- SAP GUI and Windows The registry
- With Debug Authority can do bad things ? Be careful , Your every move is monitored by the system
- ABAP CCDEF, CCIMP, CCMAC, CCAU, CMXXX What the hell are these things
- Realization ABAP Three ways of conditional breakpoints
- Use SAT Tracking monitoring opens from the browser SAP Application performance and call stack
- One 13 year ABAP Veteran's advice : Know the basics , Yes ABAP Development is profitable without harm
- SAP ABAP Netweaver Containerization , Impossible tasks ?
- SAP Product enhancement technology review
- SAP API A complete set of development methods
- Talking about Java and SAP ABAP Static agent and dynamic agent , as well as ABAP An attempt at aspect oriented programming
- SAP ABAP Application server HTTP Response status code (Status Code)
- SAP ABAP In existence Java List This kind of tool class ?CL_OBJECT_COLLECTION Get to know
- ABAP Interview questions series : Writing a group will result in a deadlock (Deadlock) Of ABAP Program
- SAP ABAP Netweaver The standard login mode of the server is explained
- SAP ABAP Keyword syntax and ABAP Code generation tool Code Composer
- SAP ABAP SM50 Alternative use of - ABAP Detection of database table read operations by the working process
- About SAP ABAP Character variable and string variable character number of a knowledge point , And a bloody case
- SAP ABAP A set of keywords IS BOUND, IS NOT INITIAL and IS ASSIGNED An analysis of the usage of
- SAP ABAP and Java The weak reference in (WeakReference) And soft quotes (SoftReference)
- SAP AMDP Introduce - ABAP managed HANA Database process
- Give you the ABAP Objects are labeled (Tag)
- Today in history : In programming language null The billion dollar quote is wrong
- ABAP Development Tool Code templates and other practical tips
- SAP ABAP Development Tool Ten tips to improve development efficiency
- How to be in SAP BTP platform ABAP Consumption in programming environment is based on SOAP Of Web Service
- ABAP Will it really be out of date ? Chat ABAP Past , Present and future
- be based on abapGit and abaplint Of ABAP An example of continuous integration
- Don't use any framework , Handwritten pure JavaScript Upload local files to ABAP The server
- Use JavaScript Upload PDF and Excel Wait for binary files to ABAP Server and parse
- from ABAP Netweaver To ABAP Platform, We have been trying to
more Jerry The original article of , All in :" Wang Zixi ":