BinaryReaderExtensions.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // RichTextKit
  2. // Copyright © 2019-2020 Topten Software. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. // not use this product except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Text;
  19. namespace Topten.RichTextKit
  20. {
  21. static class BinaryReaderExtensions
  22. {
  23. public static int ReadInt32BE(this BinaryReader reader)
  24. {
  25. var bytes = reader.ReadBytes(4);
  26. Array.Reverse(bytes);
  27. return BitConverter.ToInt32(bytes, 0);
  28. }
  29. public static uint ReadUInt32BE(this BinaryReader reader)
  30. {
  31. var bytes = reader.ReadBytes(4);
  32. Array.Reverse(bytes);
  33. return BitConverter.ToUInt32(bytes, 0);
  34. }
  35. public static void WriteBE(this BinaryWriter writer, int value)
  36. {
  37. var bytes = BitConverter.GetBytes(value);
  38. Array.Reverse(bytes);
  39. writer.Write(bytes);
  40. }
  41. public static void WriteBE(this BinaryWriter writer, uint value)
  42. {
  43. var bytes = BitConverter.GetBytes(value);
  44. Array.Reverse(bytes);
  45. writer.Write(bytes);
  46. }
  47. }
  48. }