Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Writing hexadecimal to a file

Writing hexadecimal to a file

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samodrak
    wrote on last edited by
    #1

    I am trying to write a char in hexadecimal format to a binary file. I'm using the following: FileStream fs = new FileStream("test.bin"); BinaryWriter bw = new BinaryWriter(fs); char c = '\xABCD'; bw.Write(c); What I get in the file is EA AF ..... instead of expected AB CD ...... Any ideas? Thx. Samo.

    J 1 Reply Last reply
    0
    • S samodrak

      I am trying to write a char in hexadecimal format to a binary file. I'm using the following: FileStream fs = new FileStream("test.bin"); BinaryWriter bw = new BinaryWriter(fs); char c = '\xABCD'; bw.Write(c); What I get in the file is EA AF ..... instead of expected AB CD ...... Any ideas? Thx. Samo.

      J Offline
      J Offline
      Julian Bucknall MSFT
      wrote on last edited by
      #2

      What's happening is that, by default, the character is being written out in UTF-8. If you want to just write out the character as if it were a ushort, either cast it to a ushort, or change the encoding for the binary writer. By the way, writing out the ushort 0xABCD to a file will produce 0xCD 0xAB in that order. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups