@@ -25,7 +25,11 @@ public static string UnicodeToAnsi(string value, out int byteCount)
2525 string result ;
2626 int valueLength = value . Length ;
2727 Encoding utf8Encoding = Encoding . UTF8 ;
28+ #if NETFULL
29+ Encoding ansiEncoding = Encoding . Default ;
30+ #else
2831 Encoding ansiEncoding = Encoding . GetEncoding ( 0 ) ;
32+ #endif
2933
3034 var byteArrayPool = ArrayPool < byte > . Shared ;
3135 int bufferLength = utf8Encoding . GetByteCount ( value ) ;
@@ -34,7 +38,7 @@ public static string UnicodeToAnsi(string value, out int byteCount)
3438
3539 try
3640 {
37- #if NET471 || NETSTANDARD || NETCOREAPP2_1
41+ #if NET45 || NET471 || NETSTANDARD || NETCOREAPP2_1
3842 result = ConvertStringInternal ( utf8Encoding , ansiEncoding , value , valueLength , buffer , bufferLength ) ;
3943#else
4044 utf8Encoding . GetBytes ( value , 0 , valueLength , buffer , 0 ) ;
@@ -50,7 +54,7 @@ public static string UnicodeToAnsi(string value, out int byteCount)
5054
5155 return result ;
5256 }
53- #if NET471 || NETSTANDARD || NETCOREAPP2_1
57+ #if NET45 || NET471 || NETSTANDARD || NETCOREAPP2_1
5458
5559 private static unsafe string ConvertStringInternal ( Encoding srcEncoding , Encoding dstEncoding , string s ,
5660 int charCount , byte [ ] bytes , int byteCount )
@@ -59,10 +63,37 @@ private static unsafe string ConvertStringInternal(Encoding srcEncoding, Encodin
5963 fixed ( byte * pBytes = bytes )
6064 {
6165 srcEncoding . GetBytes ( pString , charCount , pBytes , byteCount ) ;
66+ #if NET471 || NETSTANDARD || NETCOREAPP2_1
6267 string result = dstEncoding . GetString ( pBytes , byteCount ) ;
6368
6469 return result ;
6570 }
71+ #else
72+ }
73+
74+ int resultLength = dstEncoding . GetCharCount ( bytes , 0 , byteCount ) ;
75+ var charArrayPool = ArrayPool < char > . Shared ;
76+ char [ ] resultChars = charArrayPool . Rent ( resultLength + 1 ) ;
77+ resultChars [ resultLength ] = '\0 ' ;
78+
79+ string result ;
80+
81+ try
82+ {
83+ fixed ( byte * pBytes = bytes )
84+ fixed ( char * pResultChars = resultChars )
85+ {
86+ dstEncoding . GetChars ( pBytes , byteCount , pResultChars , resultLength ) ;
87+ result = new string ( pResultChars , 0 , resultLength ) ;
88+ }
89+ }
90+ finally
91+ {
92+ charArrayPool . Return ( resultChars ) ;
93+ }
94+
95+ return result ;
96+ #endif
6697 }
6798#endif
6899 }
0 commit comments