2

永続ストアのフィールドには 255 文字の制限があります。これらの 255 文字で、アプリケーションで正常に逆シリアル化できる最大数の日付と時刻の値を格納したいと考えています。ここintに記載されているようにそれらを変換することが私の唯一の選択肢でしょうか?

4

2 に答える 2

1

最初に、各 DateTime をできるだけ少ないバイト数で整数値に変換します (何を保持するかにもよりますが、おそらく 5 ~ 8 バイトです)。
分だけを保存する必要がある場合、3 バイトで 31 年、4 バイトで 8,166 年をカバーできます。

これらのバイトをバイト配列にパックし、そのバイト配列を Base64 に変換します。これにより、6 文字ごとに 1 つの日付が保存されます。

または、非 Unicode エンコーディングを選択し、バイト配列を直接文字列に変換します。これにより、印刷できない印刷可能な 4 文字ごとに 1 つの日付が格納されます。

于 2012-12-06T04:02:09.687 に答える
1

@SLaks に感謝します。

        [Test]
        public void DateTimeTest()
        {
            var dateTime = new DateTime(2012, 12, 12, 12, 12, 0);
            int intDateTime = Int32.Parse(dateTime.ToString("yymmddHHMM", CultureInfo.CurrentCulture));
            byte[] bytesDateTime = BitConverter.GetBytes(intDateTime);
            string base64 = Convert.ToBase64String(bytesDateTime);

            Debug.WriteLine(base64);    // Prints fIA/SA==

            byte[] newBytesDateTime = Convert.FromBase64String(base64);
            int newIntDateTime = BitConverter.ToInt32(newBytesDateTime, 0);
            var newDateTime = DateTime.ParseExact(newIntDateTime.ToString(), "yymmddHHMM", CultureInfo.CurrentCulture);

            Assert.AreEqual( dateTime, newDateTime );
        }
于 2012-12-06T06:35:43.673 に答える