Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ TryGetBytes()

byte[] System.Numerics.BigInteger.TryGetBytes ( GetBytesMode mode,
Span< byte > destination,
bool isUnsigned,
bool isBigEndian,
ref int bytesWritten )
inlineprivate

Definition at line 1091 of file BigInteger.cs.

1092 {
1093 int sign = _sign;
1094 if (sign == 0)
1095 {
1096 switch (mode)
1097 {
1098 case GetBytesMode.AllocateArray:
1099 return new byte[1];
1100 case GetBytesMode.Count:
1101 bytesWritten = 1;
1102 return null;
1103 default:
1104 bytesWritten = 1;
1105 if (destination.Length != 0)
1106 {
1107 destination[0] = 0;
1108 return s_success;
1109 }
1110 return null;
1111 }
1112 }
1113 if (isUnsigned && sign < 0)
1114 {
1115 throw new OverflowException(System.SR.Overflow_Negative_Unsigned);
1116 }
1117 int i = 0;
1118 uint[] bits = _bits;
1119 byte b;
1120 uint num;
1121 if (bits == null)
1122 {
1123 b = (byte)((sign < 0) ? 255u : 0u);
1124 num = (uint)sign;
1125 }
1126 else if (sign == -1)
1127 {
1128 b = byte.MaxValue;
1129 for (; bits[i] == 0; i++)
1130 {
1131 }
1132 num = ~bits[^1];
1133 if (bits.Length - 1 == i)
1134 {
1135 num++;
1136 }
1137 }
1138 else
1139 {
1140 b = 0;
1141 num = bits[^1];
1142 }
1143 byte b2;
1144 int num2;
1145 if ((b2 = (byte)(num >> 24)) != b)
1146 {
1147 num2 = 3;
1148 }
1149 else if ((b2 = (byte)(num >> 16)) != b)
1150 {
1151 num2 = 2;
1152 }
1153 else if ((b2 = (byte)(num >> 8)) != b)
1154 {
1155 num2 = 1;
1156 }
1157 else
1158 {
1159 b2 = (byte)num;
1160 num2 = 0;
1161 }
1162 bool flag = (b2 & 0x80) != (b & 0x80) && !isUnsigned;
1163 int num3 = num2 + 1 + (flag ? 1 : 0);
1164 if (bits != null)
1165 {
1166 num3 = checked(4 * (bits.Length - 1) + num3);
1167 }
1168 byte[] result;
1169 switch (mode)
1170 {
1171 case GetBytesMode.AllocateArray:
1172 destination = (result = new byte[num3]);
1173 break;
1174 case GetBytesMode.Count:
1175 bytesWritten = num3;
1176 return null;
1177 default:
1178 bytesWritten = num3;
1179 if (destination.Length < num3)
1180 {
1181 return null;
1182 }
1183 result = s_success;
1184 break;
1185 }
1186 int num4 = (isBigEndian ? (num3 - 1) : 0);
1187 int num5 = ((!isBigEndian) ? 1 : (-1));
1188 if (bits != null)
1189 {
1190 for (int j = 0; j < bits.Length - 1; j++)
1191 {
1192 uint num6 = bits[j];
1193 if (sign == -1)
1194 {
1195 num6 = ~num6;
1196 if (j <= i)
1197 {
1198 num6++;
1199 }
1200 }
1201 destination[num4] = (byte)num6;
1202 num4 += num5;
1203 destination[num4] = (byte)(num6 >> 8);
1204 num4 += num5;
1205 destination[num4] = (byte)(num6 >> 16);
1206 num4 += num5;
1207 destination[num4] = (byte)(num6 >> 24);
1208 num4 += num5;
1209 }
1210 }
1211 destination[num4] = (byte)num;
1212 if (num2 != 0)
1213 {
1214 num4 += num5;
1215 destination[num4] = (byte)(num >> 8);
1216 if (num2 != 1)
1217 {
1218 num4 += num5;
1219 destination[num4] = (byte)(num >> 16);
1220 if (num2 != 2)
1221 {
1222 num4 += num5;
1223 destination[num4] = (byte)(num >> 24);
1224 }
1225 }
1226 }
1227 if (flag)
1228 {
1229 num4 += num5;
1230 destination[num4] = b;
1231 }
1232 return result;
1233 }
static string Overflow_Negative_Unsigned
Definition SR.cs:42
Definition SR.cs:7
readonly uint[] _bits
Definition BigInteger.cs:20
static readonly byte[] s_success
Definition BigInteger.cs:30

References System.Numerics.BigInteger._bits, System.Numerics.BigInteger._sign, System.destination, System.SR.Overflow_Negative_Unsigned, and System.Numerics.BigInteger.s_success.

Referenced by System.Numerics.BigInteger.GetByteCount(), System.Numerics.BigInteger.ToByteArray(), System.Numerics.BigInteger.TryWriteBytes(), and System.Numerics.BigInteger.TryWriteOrCountBytes().