@@ -25,7 +25,11 @@ public static string UnicodeToAnsi(string value, out int byteCount)
25
25
string result ;
26
26
int valueLength = value . Length ;
27
27
Encoding utf8Encoding = Encoding . UTF8 ;
28
+ #if NETFULL
29
+ Encoding ansiEncoding = Encoding . Default ;
30
+ #else
28
31
Encoding ansiEncoding = Encoding . GetEncoding ( 0 ) ;
32
+ #endif
29
33
30
34
var byteArrayPool = ArrayPool < byte > . Shared ;
31
35
int bufferLength = utf8Encoding . GetByteCount ( value ) ;
@@ -34,7 +38,7 @@ public static string UnicodeToAnsi(string value, out int byteCount)
34
38
35
39
try
36
40
{
37
- #if NET471 || NETSTANDARD || NETCOREAPP2_1
41
+ #if NET45 || NET471 || NETSTANDARD || NETCOREAPP2_1
38
42
result = ConvertStringInternal ( utf8Encoding , ansiEncoding , value , valueLength , buffer , bufferLength ) ;
39
43
#else
40
44
utf8Encoding . GetBytes ( value , 0 , valueLength , buffer , 0 ) ;
@@ -50,7 +54,7 @@ public static string UnicodeToAnsi(string value, out int byteCount)
50
54
51
55
return result ;
52
56
}
53
- #if NET471 || NETSTANDARD || NETCOREAPP2_1
57
+ #if NET45 || NET471 || NETSTANDARD || NETCOREAPP2_1
54
58
55
59
private static unsafe string ConvertStringInternal ( Encoding srcEncoding , Encoding dstEncoding , string s ,
56
60
int charCount , byte [ ] bytes , int byteCount )
@@ -59,10 +63,37 @@ private static unsafe string ConvertStringInternal(Encoding srcEncoding, Encodin
59
63
fixed ( byte * pBytes = bytes )
60
64
{
61
65
srcEncoding . GetBytes ( pString , charCount , pBytes , byteCount ) ;
66
+ #if NET471 || NETSTANDARD || NETCOREAPP2_1
62
67
string result = dstEncoding . GetString ( pBytes , byteCount ) ;
63
68
64
69
return result ;
65
70
}
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
66
97
}
67
98
#endif
68
99
}
0 commit comments