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. Converting Byte to Decimal

Converting Byte to Decimal

Scheduled Pinned Locked Moved C#
csharpvisual-studiolinqcomdata-structures
14 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.
  • C Offline
    C Offline
    computerpublic
    wrote on last edited by
    #1

    http://msdn.microsoft.com/en-us/library/aa326741%28v=vs.71%29.aspx[^] //I WAS READING FROM THE ABOVE MICROSOFT LINK ABOUT CONVERTING //FROM BYTE TO TO DECIMAL IN C#, BUT I AM JUST NOT GETTING MY ////CODE TO WORK. I AM TRYING TO CONVERT AN ARRAY OF BYTES TO AN //ARRAY OF DECIMALS. THIS IS WHAT I AM DOING..PLEASE KINDLY SEE //BELOW.

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Applica
    {
    class Program
    {
    static void Main(string[] args)
    {
    long Totbyte = 0;
    string filePath = null;
    DirectoryInfo da = new DirectoryInfo("C:\\Folder");
    FileInfo[] Arr = da.GetFiles();

            foreach (FileInfo ap in Arr)
            {
                Totbyte = ap.Length;
                filePath = ap.FullName;
            }
    
            string temPath = Path.GetTempFileName();
            byte\[\] data = new byte\[Totbyte\];
    
            if (File.Exists(temPath))
            {
                data = File.ReadAllBytes(filePath);
                File.WriteAllBytes(temPath, data);
            }       
            decimal decVal;
            int\[\] arry = new int\[Totbyte\];
            for (int count = 0; count < data.Length; count++)
            {
                arry\[count\] = decimal.GetBits((data\[count\])decVal);//THIS LINE DOES NOT WORK.
                Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
            }
        }
    }
    

    }

    L 1 Reply Last reply
    0
    • C computerpublic

      http://msdn.microsoft.com/en-us/library/aa326741%28v=vs.71%29.aspx[^] //I WAS READING FROM THE ABOVE MICROSOFT LINK ABOUT CONVERTING //FROM BYTE TO TO DECIMAL IN C#, BUT I AM JUST NOT GETTING MY ////CODE TO WORK. I AM TRYING TO CONVERT AN ARRAY OF BYTES TO AN //ARRAY OF DECIMALS. THIS IS WHAT I AM DOING..PLEASE KINDLY SEE //BELOW.

      using System;
      using System.IO;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace Applica
      {
      class Program
      {
      static void Main(string[] args)
      {
      long Totbyte = 0;
      string filePath = null;
      DirectoryInfo da = new DirectoryInfo("C:\\Folder");
      FileInfo[] Arr = da.GetFiles();

              foreach (FileInfo ap in Arr)
              {
                  Totbyte = ap.Length;
                  filePath = ap.FullName;
              }
      
              string temPath = Path.GetTempFileName();
              byte\[\] data = new byte\[Totbyte\];
      
              if (File.Exists(temPath))
              {
                  data = File.ReadAllBytes(filePath);
                  File.WriteAllBytes(temPath, data);
              }       
              decimal decVal;
              int\[\] arry = new int\[Totbyte\];
              for (int count = 0; count < data.Length; count++)
              {
                  arry\[count\] = decimal.GetBits((data\[count\])decVal);//THIS LINE DOES NOT WORK.
                  Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
              }
          }
      }
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You don't have an array of decimals. You have an array of ints. Also, I'm pretty sure this was answered in the thread below, and the answer is much simpler than anything you've been trying to do.

      C 1 Reply Last reply
      0
      • L Lost User

        You don't have an array of decimals. You have an array of ints. Also, I'm pretty sure this was answered in the thread below, and the answer is much simpler than anything you've been trying to do.

        C Offline
        C Offline
        computerpublic
        wrote on last edited by
        #3

        I tried using an array of decimal and it still does not work. Also the question i ask this morning was about Bitconverter. There is no Bitconverter in this question. You keep saying you don't know what I am trying to do. I think it is pretty obvious that i am having trouble converting from byte to decimal. This is what I am trying to do.

        L Richard DeemingR 2 Replies Last reply
        0
        • C computerpublic

          I tried using an array of decimal and it still does not work. Also the question i ask this morning was about Bitconverter. There is no Bitconverter in this question. You keep saying you don't know what I am trying to do. I think it is pretty obvious that i am having trouble converting from byte to decimal. This is what I am trying to do.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          computerpublic wrote:

          i am having trouble converting from byte to decimal.

          Ok, good. That's a pretty clear description. Here you go:

          using System;
          using System.IO;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace Applica
          {
          class Program
          {
          static void Main(string[] args)
          {
          long Totbyte = 0;
          string filePath = null;
          DirectoryInfo da = new DirectoryInfo("C:\\Folder");
          FileInfo[] Arr = da.GetFiles();

                  foreach (FileInfo ap in Arr)
                  {
                      Totbyte = ap.Length;
                      filePath = ap.FullName;
                  }
          
                  string temPath = Path.GetTempFileName();
                  byte\[\] data = new byte\[Totbyte\];
          
                  if (File.Exists(temPath))
                  {
                      data = File.ReadAllBytes(filePath);
                      File.WriteAllBytes(temPath, data);
                  }
                  decimal\[\] arry = new decimal\[Totbyte\];
                  for (int count = 0; count < data.Length; count++)
                  {
                      arry\[count\] = data\[count\];
                      Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
                  }
              }
          }
          

          }

          That's what you meant, right?

          C 2 Replies Last reply
          0
          • L Lost User

            computerpublic wrote:

            i am having trouble converting from byte to decimal.

            Ok, good. That's a pretty clear description. Here you go:

            using System;
            using System.IO;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;

            namespace Applica
            {
            class Program
            {
            static void Main(string[] args)
            {
            long Totbyte = 0;
            string filePath = null;
            DirectoryInfo da = new DirectoryInfo("C:\\Folder");
            FileInfo[] Arr = da.GetFiles();

                    foreach (FileInfo ap in Arr)
                    {
                        Totbyte = ap.Length;
                        filePath = ap.FullName;
                    }
            
                    string temPath = Path.GetTempFileName();
                    byte\[\] data = new byte\[Totbyte\];
            
                    if (File.Exists(temPath))
                    {
                        data = File.ReadAllBytes(filePath);
                        File.WriteAllBytes(temPath, data);
                    }
                    decimal\[\] arry = new decimal\[Totbyte\];
                    for (int count = 0; count < data.Length; count++)
                    {
                        arry\[count\] = data\[count\];
                        Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
                    }
                }
            }
            

            }

            That's what you meant, right?

            C Offline
            C Offline
            computerpublic
            wrote on last edited by
            #5

            Your answer worked very well. I think I was over complicating the problem. Anyway thank you very much for your help.

            1 Reply Last reply
            0
            • C computerpublic

              I tried using an array of decimal and it still does not work. Also the question i ask this morning was about Bitconverter. There is no Bitconverter in this question. You keep saying you don't know what I am trying to do. I think it is pretty obvious that i am having trouble converting from byte to decimal. This is what I am trying to do.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              computerpublic wrote:

              You keep saying you don't know what I am trying to do. I think it is pretty obvious that ...

              Of course it's obvious to you - you're the one who's having the problems. The trick is to clearly explain the problem to someone else, remembering that we can't see your screen, access your hard-drive, or read your mind. None of the questions you've posted in the last couple of days have had a clear explanation of what you're trying to do. They've mostly been code-dumps with a few cryptic comments along the lines of "this line doesn't work". The code you've posted doesn't seem to make any logical sense, because you haven't explained what you're trying to do. The code you posted in this question is almost identical to the code you posted in the question below[^]. It seems you either didn't read Pete's answer, or you didn't understand it, because now you're just calling a random method and trying to assign the result to an incompatible type. Reposting a virtually identical question whilst ignoring the previous answers is not a good way to get people to help you!


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • L Lost User

                computerpublic wrote:

                i am having trouble converting from byte to decimal.

                Ok, good. That's a pretty clear description. Here you go:

                using System;
                using System.IO;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;

                namespace Applica
                {
                class Program
                {
                static void Main(string[] args)
                {
                long Totbyte = 0;
                string filePath = null;
                DirectoryInfo da = new DirectoryInfo("C:\\Folder");
                FileInfo[] Arr = da.GetFiles();

                        foreach (FileInfo ap in Arr)
                        {
                            Totbyte = ap.Length;
                            filePath = ap.FullName;
                        }
                
                        string temPath = Path.GetTempFileName();
                        byte\[\] data = new byte\[Totbyte\];
                
                        if (File.Exists(temPath))
                        {
                            data = File.ReadAllBytes(filePath);
                            File.WriteAllBytes(temPath, data);
                        }
                        decimal\[\] arry = new decimal\[Totbyte\];
                        for (int count = 0; count < data.Length; count++)
                        {
                            arry\[count\] = data\[count\];
                            Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
                        }
                    }
                }
                

                }

                That's what you meant, right?

                C Offline
                C Offline
                computerpublic
                wrote on last edited by
                #7

                You are correct. I did ask the question improperly. I do have one final question: In a file transfer OUT: Does LSB of the FIRST byte transfer out before MSB? Followed by the LSB of the SECOND byte, then the MSB? Followed by the LSB of the LAST byte , then the MSB? In a file transfer work the same way when I file comes IN?

                L 1 Reply Last reply
                0
                • C computerpublic

                  You are correct. I did ask the question improperly. I do have one final question: In a file transfer OUT: Does LSB of the FIRST byte transfer out before MSB? Followed by the LSB of the SECOND byte, then the MSB? Followed by the LSB of the LAST byte , then the MSB? In a file transfer work the same way when I file comes IN?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  The bytes in a file are exactly what they are, the bits of those bytes are not mixed in weird ways.

                  C 1 Reply Last reply
                  0
                  • L Lost User

                    The bytes in a file are exactly what they are, the bits of those bytes are not mixed in weird ways.

                    C Offline
                    C Offline
                    computerpublic
                    wrote on last edited by
                    #9

                    //I AM TRYING TO REVERSE THE PROCESS OF BYTE TO DECIMAL CONVERSION WHICH WAS SUCCESSFUL. I THOUGHT THAT REVERSING THE CODE WOULD REVERSE THE PROCESS AND DECIMAL TO BYTE CONVERSION, BUT IT IS NOT WORKING.

                    using System;//done
                    using System.IO;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Text;

                    namespace Applica
                    {
                    class Program
                    {
                    static void Main(string[] args)
                    {
                    long Totbyte = 0;
                    string filePath = null;
                    DirectoryInfo da = new DirectoryInfo("C:\\Folder");
                    FileInfo[] Arr = da.GetFiles();

                            foreach (FileInfo ap in Arr)
                            {
                                Totbyte = ap.Length;
                                filePath = ap.FullName;
                            }
                            Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
                            string temPath = Path.GetTempFileName();
                            byte\[\] data = new byte\[Totbyte\];
                    
                            if (File.Exists(temPath))
                            {
                                data = File.ReadAllBytes(filePath);
                                File.WriteAllBytes(temPath, data);
                            }
                            decimal\[\] arry = new decimal\[Totbyte\];
                            for (int count = 0; count < data.Length; count++)
                            {
                                arry\[count\] = data\[count\];
                             //   Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
                            }
                            ///////////ABOVE: READ IN FILE AND CHANGE EACH BYTE TO DECIMAL
                            ///////////BELOW: TRYING TO REVERSE THE ABOVE PROCESS
                            byte\[\] data2 = new byte\[Totbyte\];
                            for (int count = 0; count < arry.Length; count++)
                            {
                                data2\[count\] = arry\[count\];
                            }
                            FileStream file = new FileStream(filePath, FileMode.Create);
                            BinaryWriter binarystream = new BinaryWriter(file);
                            binarystream.Write(data2);
                            binarystream.Close();
                        }
                    }
                    

                    }

                    L 1 Reply Last reply
                    0
                    • C computerpublic

                      //I AM TRYING TO REVERSE THE PROCESS OF BYTE TO DECIMAL CONVERSION WHICH WAS SUCCESSFUL. I THOUGHT THAT REVERSING THE CODE WOULD REVERSE THE PROCESS AND DECIMAL TO BYTE CONVERSION, BUT IT IS NOT WORKING.

                      using System;//done
                      using System.IO;
                      using System.Collections.Generic;
                      using System.Linq;
                      using System.Text;

                      namespace Applica
                      {
                      class Program
                      {
                      static void Main(string[] args)
                      {
                      long Totbyte = 0;
                      string filePath = null;
                      DirectoryInfo da = new DirectoryInfo("C:\\Folder");
                      FileInfo[] Arr = da.GetFiles();

                              foreach (FileInfo ap in Arr)
                              {
                                  Totbyte = ap.Length;
                                  filePath = ap.FullName;
                              }
                              Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
                              string temPath = Path.GetTempFileName();
                              byte\[\] data = new byte\[Totbyte\];
                      
                              if (File.Exists(temPath))
                              {
                                  data = File.ReadAllBytes(filePath);
                                  File.WriteAllBytes(temPath, data);
                              }
                              decimal\[\] arry = new decimal\[Totbyte\];
                              for (int count = 0; count < data.Length; count++)
                              {
                                  arry\[count\] = data\[count\];
                               //   Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
                              }
                              ///////////ABOVE: READ IN FILE AND CHANGE EACH BYTE TO DECIMAL
                              ///////////BELOW: TRYING TO REVERSE THE ABOVE PROCESS
                              byte\[\] data2 = new byte\[Totbyte\];
                              for (int count = 0; count < arry.Length; count++)
                              {
                                  data2\[count\] = arry\[count\];
                              }
                              FileStream file = new FileStream(filePath, FileMode.Create);
                              BinaryWriter binarystream = new BinaryWriter(file);
                              binarystream.Write(data2);
                              binarystream.Close();
                          }
                      }
                      

                      }

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Follow the error message. It tells you what to do. But I'm a nice guy (well, sometimes), so here you go:

                      using System;
                      using System.IO;
                      using System.Collections.Generic;
                      using System.Linq;
                      using System.Text;

                      namespace Applica
                      {
                      class Program
                      {
                      static void Main(string[] args)
                      {
                      long Totbyte = 0;
                      string filePath = null;
                      DirectoryInfo da = new DirectoryInfo("C:\\Folder");
                      FileInfo[] Arr = da.GetFiles();

                              foreach (FileInfo ap in Arr)
                              {
                                  Totbyte = ap.Length;
                                  filePath = ap.FullName;
                              }
                              Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
                              string temPath = Path.GetTempFileName();
                              byte\[\] data = new byte\[Totbyte\];
                      
                              if (File.Exists(temPath))
                              {
                                  data = File.ReadAllBytes(filePath);
                                  File.WriteAllBytes(temPath, data);
                              }
                              decimal\[\] arry = new decimal\[Totbyte\];
                              for (int count = 0; count < data.Length; count++)
                              {
                                  arry\[count\] = data\[count\];
                               //   Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
                              }
                              ///////////ABOVE: READ IN FILE AND CHANGE EACH BYTE TO DECIMAL
                              ///////////BELOW: TRYING TO REVERSE THE ABOVE PROCESS
                              byte\[\] data2 = new byte\[Totbyte\];
                              for (int count = 0; count < arry.Length; count++)
                              {
                                  data2\[count\] = (byte)arry\[count\];
                              }
                              FileStream file = new FileStream(filePath, FileMode.Create);
                              BinaryWriter binarystream = new BinaryWriter(file);
                              binarystream.Write(data2);
                              binarystream.Close();
                          }
                      }
                      

                      }

                      By the way, may I ask what this is for? Converting all the bytes of a file to decimals is a pretty weird thing to do. I have a gut feeling there might be a nicer solution.

                      C 1 Reply Last reply
                      0
                      • L Lost User

                        Follow the error message. It tells you what to do. But I'm a nice guy (well, sometimes), so here you go:

                        using System;
                        using System.IO;
                        using System.Collections.Generic;
                        using System.Linq;
                        using System.Text;

                        namespace Applica
                        {
                        class Program
                        {
                        static void Main(string[] args)
                        {
                        long Totbyte = 0;
                        string filePath = null;
                        DirectoryInfo da = new DirectoryInfo("C:\\Folder");
                        FileInfo[] Arr = da.GetFiles();

                                foreach (FileInfo ap in Arr)
                                {
                                    Totbyte = ap.Length;
                                    filePath = ap.FullName;
                                }
                                Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
                                string temPath = Path.GetTempFileName();
                                byte\[\] data = new byte\[Totbyte\];
                        
                                if (File.Exists(temPath))
                                {
                                    data = File.ReadAllBytes(filePath);
                                    File.WriteAllBytes(temPath, data);
                                }
                                decimal\[\] arry = new decimal\[Totbyte\];
                                for (int count = 0; count < data.Length; count++)
                                {
                                    arry\[count\] = data\[count\];
                                 //   Console.WriteLine("Byte to Decimal = {0},,,,,, count = {1}", arry\[count\], count);
                                }
                                ///////////ABOVE: READ IN FILE AND CHANGE EACH BYTE TO DECIMAL
                                ///////////BELOW: TRYING TO REVERSE THE ABOVE PROCESS
                                byte\[\] data2 = new byte\[Totbyte\];
                                for (int count = 0; count < arry.Length; count++)
                                {
                                    data2\[count\] = (byte)arry\[count\];
                                }
                                FileStream file = new FileStream(filePath, FileMode.Create);
                                BinaryWriter binarystream = new BinaryWriter(file);
                                binarystream.Write(data2);
                                binarystream.Close();
                            }
                        }
                        

                        }

                        By the way, may I ask what this is for? Converting all the bytes of a file to decimals is a pretty weird thing to do. I have a gut feeling there might be a nicer solution.

                        C Offline
                        C Offline
                        computerpublic
                        wrote on last edited by
                        #11

                        I did figure out the casting (byte) from one of my books. Also filePath (FileStream file = new FileStream(filePath, FileMode.Create)) did not work. I had to replace filePath with an actual path. When I open the file, it was looked like garbage. It was not the same file that I started with. It got corrupted at some point. What is it for: Well it is a very long story and really don't mind sharing my thoughts after I prove my theory. I would be more than happy to explore a nicer, shorter, more stream line and robust solution. Please assist.

                        L 1 Reply Last reply
                        0
                        • C computerpublic

                          I did figure out the casting (byte) from one of my books. Also filePath (FileStream file = new FileStream(filePath, FileMode.Create)) did not work. I had to replace filePath with an actual path. When I open the file, it was looked like garbage. It was not the same file that I started with. It got corrupted at some point. What is it for: Well it is a very long story and really don't mind sharing my thoughts after I prove my theory. I would be more than happy to explore a nicer, shorter, more stream line and robust solution. Please assist.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          You might be interested in File.WriteAllBytes(path, bytes) to making writing them back a little simpler.

                          computerpublic wrote:

                          When I open the file, it was looked like garbage. It was not the same file that I started with. It got corrupted at some point.

                          Is that with the code I posted? Or else, with what code?

                          C 1 Reply Last reply
                          0
                          • L Lost User

                            You might be interested in File.WriteAllBytes(path, bytes) to making writing them back a little simpler.

                            computerpublic wrote:

                            When I open the file, it was looked like garbage. It was not the same file that I started with. It got corrupted at some point.

                            Is that with the code I posted? Or else, with what code?

                            C Offline
                            C Offline
                            computerpublic
                            wrote on last edited by
                            #13

                            My first idea was to use WriteAllBytes, but books did not talk about it. And the online resources did not show me properly how to code it. I found the other solution which i used, but I am getting garbage. Can you please show me how to make it work?

                            L 1 Reply Last reply
                            0
                            • C computerpublic

                              My first idea was to use WriteAllBytes, but books did not talk about it. And the online resources did not show me properly how to code it. I found the other solution which i used, but I am getting garbage. Can you please show me how to make it work?

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              It should work without special effort. Did you verify that the bytes you give it are OK?

                              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