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. Byte Conversion

Byte Conversion

Scheduled Pinned Locked Moved C#
helpdatabase
10 Posts 3 Posters 0 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.
  • A Offline
    A Offline
    Agweet
    wrote on last edited by
    #1

    Hi all, im having a problem with retrieving a binary file from the database and reading it in my code, so far i have this: byte AttachmentByte = new byte(); AttachmentByte = StringToByteArray(inputString); and a function to convert it: public static byte[] StringToByteArray(string inputString) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); return encoding.GetBytes(inputString); } but when i run it i get the error: Cannot implicitly convert type 'byte[]' to 'byte' can someone please tell me what im doing wrong. thanks in advance!

    living life on the flip side

    G N 2 Replies Last reply
    0
    • A Agweet

      Hi all, im having a problem with retrieving a binary file from the database and reading it in my code, so far i have this: byte AttachmentByte = new byte(); AttachmentByte = StringToByteArray(inputString); and a function to convert it: public static byte[] StringToByteArray(string inputString) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); return encoding.GetBytes(inputString); } but when i run it i get the error: Cannot implicitly convert type 'byte[]' to 'byte' can someone please tell me what im doing wrong. thanks in advance!

      living life on the flip side

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      StringToByteArray returns byte[] while AttachmentByte is defined as byte.

      Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

      1 Reply Last reply
      0
      • A Agweet

        Hi all, im having a problem with retrieving a binary file from the database and reading it in my code, so far i have this: byte AttachmentByte = new byte(); AttachmentByte = StringToByteArray(inputString); and a function to convert it: public static byte[] StringToByteArray(string inputString) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); return encoding.GetBytes(inputString); } but when i run it i get the error: Cannot implicitly convert type 'byte[]' to 'byte' can someone please tell me what im doing wrong. thanks in advance!

        living life on the flip side

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Agweet wrote:

        byte AttachmentByte = new byte(); AttachmentByte = StringToByteArray(inputString);

        byte[] AttachmentByte;
        AttachmentByte = StringToByteArray(inputString);

        Navaneeth How to use google | Ask smart questions

        A 1 Reply Last reply
        0
        • N N a v a n e e t h

          Agweet wrote:

          byte AttachmentByte = new byte(); AttachmentByte = StringToByteArray(inputString);

          byte[] AttachmentByte;
          AttachmentByte = StringToByteArray(inputString);

          Navaneeth How to use google | Ask smart questions

          A Offline
          A Offline
          Agweet
          wrote on last edited by
          #4

          Hi guys, thanks for the quick replies, the problem is that i tried using:

          byte[] AttachmentByte;

          then it gives the error on the filestream:

          using (System.IO.FileStream fs = System.IO.File.Create(newPath))
          {
          fs.WriteByte(AttachmentByte);
          }

          i get the errors: The best overloaded method match for 'System.IO.Stream.WriteByte(byte)' has some invalid arguments Argument '1': cannot convert from 'byte[]' to 'byte' is there something else im missing?

          living life on the flip side

          N G 2 Replies Last reply
          0
          • A Agweet

            Hi guys, thanks for the quick replies, the problem is that i tried using:

            byte[] AttachmentByte;

            then it gives the error on the filestream:

            using (System.IO.FileStream fs = System.IO.File.Create(newPath))
            {
            fs.WriteByte(AttachmentByte);
            }

            i get the errors: The best overloaded method match for 'System.IO.Stream.WriteByte(byte)' has some invalid arguments Argument '1': cannot convert from 'byte[]' to 'byte' is there something else im missing?

            living life on the flip side

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Agweet wrote:

            is there something else im missing?

            Yes. A good C# book. Seriously, the error message you got is self explanatory. It has some invalid arguments. Look at the documentation for that method and see what arguments it expects. BTW, this some different code you are showing than the one given in the original post.

            Navaneeth How to use google | Ask smart questions

            A 1 Reply Last reply
            0
            • A Agweet

              Hi guys, thanks for the quick replies, the problem is that i tried using:

              byte[] AttachmentByte;

              then it gives the error on the filestream:

              using (System.IO.FileStream fs = System.IO.File.Create(newPath))
              {
              fs.WriteByte(AttachmentByte);
              }

              i get the errors: The best overloaded method match for 'System.IO.Stream.WriteByte(byte)' has some invalid arguments Argument '1': cannot convert from 'byte[]' to 'byte' is there something else im missing?

              living life on the flip side

              G Offline
              G Offline
              Giorgi Dalakishvili
              wrote on last edited by
              #6

              WriteByte writes just a single byte to the stream but you need to write byte[] so use the overload that writes byte[] to the stream. That's it :)

              Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

              A 1 Reply Last reply
              0
              • N N a v a n e e t h

                Agweet wrote:

                is there something else im missing?

                Yes. A good C# book. Seriously, the error message you got is self explanatory. It has some invalid arguments. Look at the documentation for that method and see what arguments it expects. BTW, this some different code you are showing than the one given in the original post.

                Navaneeth How to use google | Ask smart questions

                A Offline
                A Offline
                Agweet
                wrote on last edited by
                #7

                thanks once again for the reply, i know that i posted other code...im trying to retrieve a binary file stored in the database and write it to my local machine, with use of creating a folder and using a filestream. thanks for all the advise :)

                living life on the flip side

                1 Reply Last reply
                0
                • G Giorgi Dalakishvili

                  WriteByte writes just a single byte to the stream but you need to write byte[] so use the overload that writes byte[] to the stream. That's it :)

                  Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

                  A Offline
                  A Offline
                  Agweet
                  wrote on last edited by
                  #8

                  Hi Giorgi, sorry but i have never used filestreams or binary files before, how would i go about using the overload that writes byte[]?

                  living life on the flip side

                  G 1 Reply Last reply
                  0
                  • A Agweet

                    Hi Giorgi, sorry but i have never used filestreams or binary files before, how would i go about using the overload that writes byte[]?

                    living life on the flip side

                    G Offline
                    G Offline
                    Giorgi Dalakishvili
                    wrote on last edited by
                    #9

                    As suggested by Navaneeth you need a good C# book. As for FileStream, you can have a look at its method at this msdn link: FileStream Methods[^]

                    Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

                    A 1 Reply Last reply
                    0
                    • G Giorgi Dalakishvili

                      As suggested by Navaneeth you need a good C# book. As for FileStream, you can have a look at its method at this msdn link: FileStream Methods[^]

                      Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

                      A Offline
                      A Offline
                      Agweet
                      wrote on last edited by
                      #10

                      Hi Giorgi, thanks for everything, to all the people that replied :laugh:

                      living life on the flip side

                      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