Показаны сообщения с ярлыком Bytes. Показать все сообщения
Показаны сообщения с ярлыком Bytes. Показать все сообщения

воскресенье, 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

суббота, 2 января 2016 г.

ArrayOfString to Byte Array

http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte

http://stackoverflow.com/questions/14669820/how-to-convert-a-string-array-to-a-byte-array-java


Compress/Decompress

http://www.dotnetperls.com/compress

http://www.dotnetperls.com/decompress


 private byte[] Serialize(IEnumerable<Dto.Bar> bs)
        {
            BinaryFormatter bf = new BinaryFormatter();
            byte[] bytes;
            MemoryStream ms = new MemoryStream();

            var lst = bs.Select(b => b.ToStr()).ToList();

            bf.Serialize(ms, lst);
            ms.Seek(0, 0);
            
            var bts = ms.ToArray();

            return bts;
        }

 private IEnumerable<IBarSimple> DeSerialize(byte[] bytes)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            ms.Write(bytes, 0, bytes.Length);
            ms.Seek(0, 0);

            var ss = (List<string>)bf.Deserialize(ms);
            var bs = ss.Select(s => s.ToBarDto()).ToList();
            return bs;

        }

 private byte[] Compress(byte[] bytes)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    gzip.Write(bytes, 0, bytes.Length);
                }
                return ms.ToArray();
            }

        }

 private byte[] DeCompress(byte[] bytes)
        {
            using (GZipStream gzstream = new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress))
            {
                const int size = 4096;
                byte[] buffer = new byte[size];
                using (MemoryStream mstr = new MemoryStream())
                {
                    int count = 0;
                    do
                    {
                        count = gzstream.Read(buffer, 0, size);
                        if (count > 0)
                        {
                            mstr.Write(buffer, 0, count);
                        }
                    } while (count > 0);
                    return mstr.ToArray();
                }
            }

        }