воскресенье, 24 января 2021 г.

Bytes To String

https://stackoverflow.com/questions/1003275/how-to-convert-utf-8-byte-to-string

  1. Encoding's GetString
    , but you won't be able to get the original bytes back if those bytes have non-ASCII characters.

  2. BitConverter.ToString
    The output is a "-" delimited string, but there's no .NET built-in method to convert the string back to byte array.

  3. Convert.ToBase64String
    You can easily convert the output string back to byte array by using Convert.FromBase64String.
    Note: The output string could contain '+', '/' and '='. If you want to use the string in a URL, you need to explicitly encode it.

  4. HttpServerUtility.UrlTokenEncode
    You can easily convert the output string back to byte array by using HttpServerUtility.UrlTokenDecode. The output string is already URL friendly! The downside is it needs System.Web assembly if your project is not a web project.



byte[] bytes = { 130, 200, 234, 23 }; // A byte array contains non-ASCII (or non-readable) characters

string s1 = Encoding.UTF8.GetString(bytes); // ���
byte[] decBytes1 = Encoding.UTF8.GetBytes(s1);  // decBytes1.Length == 10 !!
// decBytes1 not same as bytes
// Using UTF-8 or other Encoding object will get similar results

string s2 = BitConverter.ToString(bytes);   // 82-C8-EA-17
String[] tempAry = s2.Split('-');
byte[] decBytes2 = new byte[tempAry.Length];
for (int i = 0; i < tempAry.Length; i++)
    decBytes2[i] = Convert.ToByte(tempAry[i], 16);
// decBytes2 same as bytes

string s3 = Convert.ToBase64String(bytes);  // gsjqFw==
byte[] decByte3 = Convert.FromBase64String(s3);
// decByte3 same as bytes

string s4 = HttpServerUtility.UrlTokenEncode(bytes);    // gsjqFw2
byte[] decBytes4 = HttpServerUtility.UrlTokenDecode(s4);
// decBytes4 same as bytes

четверг, 7 января 2021 г.

SQL, ulong, uint

 @markolbert Unsigned longs, ints, and shorts are not yet supported for SQL Server because the underlying ADO.NET provider for SQL Server ("a.k.a. SQL Client") does not support them. They can be used with other providers, such as SQLite. We plan to support them for SQL Server when type conversions are implemented. This is being tracked by issue #242. Closing this as a duplicate of that issue.

https://github.com/dotnet/efcore/issues/6480

These are the default conversions:

  • sbyte -> smallint
  • ushort -> int
  • uint -> bigint
  • ulong -> decimal(20, 0)
  • char -> nchar(1)

These can be used by changing the store type:

  • sbyte -> byte (not order preserving)
  • ushort -> short (not order preserving)
  • uint -> int (not order preserving)
  • ulong -> long (not order preserving)
  • char -> other string types of any size: char, nchar, varchar, nvarchar, text
  • char -> int