C# big-endian UCS-2

前端 未结 3 2088
Happy的楠姐
Happy的楠姐 2020-12-06 08:58

The project I\'m currently working on needs to interface with a client system that we don\'t make, so we have no control over how data is sent either way. The problem is tha

3条回答
  •  心在旅途
    2020-12-06 09:24

    string x = "abc";
    byte[] data = Encoding.BigEndianUnicode.GetBytes(x);
    

    In other direction:

    string decodedX = Encoding.BigEndianUnicode.GetString(data);
    

    It is not exactly UCS-2 but it is enough for most cases.

    UPD: Unicode FAQ

    Q: What is the difference between UCS-2 and UTF-16?

    A: UCS-2 is obsolete terminology which refers to a Unicode implementation up to Unicode 1.1, before surrogate code points and UTF-16 were added to Version 2.0 of the standard. This term should now be avoided.

    UCS-2 does not define a distinct data format, because UTF-16 and UCS-2 are identical for purposes of data exchange. Both are 16-bit, and have exactly the same code unit representation.

    Sometimes in the past an implementation has been labeled "UCS-2" to indicate that it does not support supplementary characters and doesn't interpret pairs of surrogate code points as characters. Such an implementation would not handle processing of character properties, code point boundaries, collation, etc. for supplementary characters.

提交回复
热议问题