Why am I getting this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott M.

    Why am I getting this?

    Why does the following code produce a string ("Byte me!") in the output
    file, rather than byte data as integers (66 121 116 101 32 109 101 33)?

    Dim fs As System.IO.FileS tream = System.IO.File. Open("C:\myMess age.dat",
    IO.FileMode.Cre ate)
    Dim msg As String = "Byte me!"
    Dim msgByteArray() As Byte = System.Text.Enc oding.Default.G etBytes(msg)
    fs.Write(msgByt eArray, 0, msgByteArray.Le ngth)

    ....And, how do I get the output file to have the byte array data?

    PS - I know I could use a different Stream type, but I want to do this using
    a FileStream.

  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

    #2
    RE: Why am I getting this?



    "Scott M." wrote:
    Why does the following code produce a string ("Byte me!") in the output
    file, rather than byte data as integers (66 121 116 101 32 109 101 33)?
    >
    Dim fs As System.IO.FileS tream = System.IO.File. Open("C:\myMess age.dat",
    IO.FileMode.Cre ate)
    Dim msg As String = "Byte me!"
    Dim msgByteArray() As Byte = System.Text.Enc oding.Default.G etBytes(msg)
    fs.Write(msgByt eArray, 0, msgByteArray.Le ngth)
    >
    ...And, how do I get the output file to have the byte array data?
    >
    PS - I know I could use a different Stream type, but I want to do this using
    a FileStream.
    >
    It does show those values. I'm guessing you are using a text editor rather
    than something like Notepad++ that can show the values as bytes. The bytes
    you want and the string you got are the same.

    Comment

    • =?Utf-8?B?U3VydHVyWg==?=

      #3
      RE: Why am I getting this?

      As Mike says, you are actually getting the result you want.

      However, you could try a BinaryWriter if you want to write other data types
      (e.g. Integer) in binary format.






      --
      David Streeter
      Synchrotech Software
      Sydney Australia


      "Scott M." wrote:
      Why does the following code produce a string ("Byte me!") in the output
      file, rather than byte data as integers (66 121 116 101 32 109 101 33)?
      >
      Dim fs As System.IO.FileS tream = System.IO.File. Open("C:\myMess age.dat",
      IO.FileMode.Cre ate)
      Dim msg As String = "Byte me!"
      Dim msgByteArray() As Byte = System.Text.Enc oding.Default.G etBytes(msg)
      fs.Write(msgByt eArray, 0, msgByteArray.Le ngth)
      >
      ...And, how do I get the output file to have the byte array data?
      >
      PS - I know I could use a different Stream type, but I want to do this using
      a FileStream.
      >

      Comment

      • Cor Ligthert[MVP]

        #4
        Re: Why am I getting this?

        Scott,

        I am not sure, however for one thing I am sure, your default text encoding
        is not ASCII while you are showing us here a decimal representation of bit
        values in 7 bit (bit)words confirm ASCII.

        Cor

        "Scott M." <smar@nospam.no spamschreef in bericht
        news:094D81C4-0DFC-406E-ACB2-5AEEB218DAA7@mi crosoft.com...
        Why does the following code produce a string ("Byte me!") in the output
        file, rather than byte data as integers (66 121 116 101 32 109 101 33)?
        >
        Dim fs As System.IO.FileS tream =
        System.IO.File. Open("C:\myMess age.dat", IO.FileMode.Cre ate)
        Dim msg As String = "Byte me!"
        Dim msgByteArray() As Byte = System.Text.Enc oding.Default.G etBytes(msg)
        fs.Write(msgByt eArray, 0, msgByteArray.Le ngth)
        >
        ...And, how do I get the output file to have the byte array data?
        >
        PS - I know I could use a different Stream type, but I want to do this
        using a FileStream.

        Comment

        • Jialiang Ge [MSFT]

          #5
          RE: Why am I getting this?

          Hello Scott,

          Apart from Mike, Davud and Cor's suggestions, if you expect to see the
          integers (66 121 ...) in the output file, we can do it in this way:

          Dim fs As System.IO.FileS tream =
          System.IO.File. Open("C:\myMess age.dat", IO.FileMode.Cre ate)
          Dim msg As String = "Byte me!"
          Dim msgByteArray() As Byte =
          System.Text.Enc oding.Default.G etBytes(msg)
          For Each bt As Byte In msgByteArray
          Dim tmpByteArray() As Byte =
          System.Text.Enc oding.Default.G etBytes(bt.ToSt ring())
          fs.Write(tmpByt eArray, 0, tmpByteArray.Le ngth)
          Next
          fs.Close()

          Hope it helps,

          If you have any other concerns or questions, please feel free to let me
          know.

          Regards,
          Jialiang Ge (jialge@online. microsoft.com, remove 'online.')
          Microsoft Online Community Support

          =============== =============== =============== ====
          When responding to posts, please "Reply to Group" via your newsreader
          so that others may learn and benefit from your issue.
          =============== =============== =============== ====
          This posting is provided "AS IS" with no warranties, and confers no rights.

          Comment

          Working...