Converting Byte to Decimal
-
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?
Your answer worked very well. I think I was over complicating the problem. Anyway thank you very much for your help.
-
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.
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
-
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?
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?
-
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?
-
The bytes in a file are exactly what they are, the bits of those bytes are not mixed in weird ways.
//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(); } }
}
-
//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(); } }
}
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.
-
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.
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.
-
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.
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?
-
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?
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?
-
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?