ReadByte function does it work correctly - c#

In FileStream class we have ReadByte method well actually it does not work as I supposed
why ReadByte ignore "nonsignificant" zeros from beginning of byte? How could I avoid that ?
as someone mentioned in comments, yes im trying to convert it to string
dane.Append(Convert.ToString((byte)w,2));
So is it ToString function skipping zeros?

The numbers are the same; only the ToString is the issue
dane.Append(Convert.ToString((byte)w,2).PadLeft(8, '0'));

ReadByte reads a byte. The result it returns is a number. When you say "nonsignificant zeroes", presumably you mean zero digits. But digits are not properties of numbers, they're properties of textual depictions of numbers in base ten (or sixteen).
"8" and "08" are two different ways to depict in digits the same number. If you aren't seeing the number depicted the way you want it, the issue is in the code that chose who to depict it.

Related

Which data structure(or type) would be useful for defining the digit amount of a number?

Hi there fellow programmers,
I know that should be easy but I need to define the digit amount of a number for trying all the combinations in a project. The digit number shouldn't be affected by users actions because the change of the digit amount causes "Index out of range" error. (Yes, I am using arrays for this)
Let's say, I have to use four digit number.
int Nmr=1000;
Nmr--;
Console.Write(Nmr);// The output will be 999 but I need 0999
Using string type and if statements could lead to an alternative solution...
int Nmr=1000;
Nmr--;
string number=Nmr.ToString();
if (Nmr<1000) number="0"+number;
if (Nmr<100) number="00"+number;
if (Nmr<10) number="000"+number;
Console.Write(Nmr); //That gives me 0999
But then, it gives me complexity and unneccessary time loss which I wouldn't want to encounter. I am not even talking about the greater values.
So, what would you suggest?
Edit: Both ToString("0000") and PadLeft methods are useful.
Thank you Mateus Coutinho Martino and Blorgbeard. =)
You can specify a format when calling ToString - e.g.
string number = Nmr.ToString("0000");
See the docs: Int32.ToString(string) and Custom Numeric Format Strings.
Well, do with PadLeft method of String class ...
int Nmr=1000;
Nmr--;
Console.Write(Nmr.ToString().PadLeft(4,'0')); //That gives you ever four digits.
Or if you prefer better explained ...
int Nmr=1000;
Nmr--;
String number = Nmr.ToString();
Console.Write(number.PadLeft(4,'0')); // four digits again;
The "0" custom format specifier serves as a zero-placeholder symbol.
Here's a little idea for you:
double numberS;
numberS = 123;
Console.WriteLine(numberS.ToString("00000"));
Console.WriteLine(String.Format("{0:00000}", value));
// Displays 00123
You can look at https://msdn.microsoft.com/en-us/library/0c899ak8.aspx for more.

BitConverter VS ToString for Hex

Just wondering if someone could explain why the two following lines of code return "different" results? What causes the reversed values? Is this something to do with endianness?
int.MaxValue.ToString("X") //Result: 7FFFFFFF
BitConverter.ToString(BitConverter.GetBytes(int.MaxValue)) //Result: FF-FF-FF-7F
int.MaxValue.ToString("X") outputs 7FFFFFFF, that is, the number 2147483647 as a whole.
On the other hand, BitConverter.GetBytes returns an array of bytes representing 2147483647 in memory. On your machine, this number is stored in little-endian (highest byte last). And BitConverter.ToString operates separately on each byte, therefore not reordering output to give the same as above, thus preserving the memory order.
However the two values are the same : 7F-FF-FF-FF for int.MaxValue, in big-endian, and FF-FF-FF-7F for BitConverter, in little-endian. Same number.
I would guess because GetBytes returns an array of bytes which BitConverter.ToString formatted - in my opinion - rather nicely
And also keep in mind that the bitwise represantattion may be different from the value! This depends where the most signigicant byte sits!
hth

converting a string of binary into a string of decimal c#

problem is to convert a string of binary digits into its decimal representation. Easy eh?
well, it needs to be able to handle input of over 64 bits in length and convert it without using any external libraries or data types like big integer.
How can I do this?
so far i have a string called input which handles the binary
I then access each digit using input[0] etc to get a char representing that digit.
Now I manipulate it and multiply by the corresponding power of 2 that its index represents, and move through the array storing the total as i go.
I use a big integer to store the total as for large numbers the primative types dont work.
My first solution works perfectly, how can I do this without using anything to store the total, i.e only using strings to store answers.
Any Ideas?
Thanks
You will need an array of digits to hold the result. An array of int's would be easier but you can also use the final string. You can calculate it's length from the length of the inputstring, you may have to remove leading zero's in the end.
Calculate your result as before but do the adding (including the carrying) in the result array.
I think this is a (homework) assignment that wants you to implement a version of the "pen & paper" addition method.

Getting a string, int, etc in binary representation?

Is it possible to get strings, ints, etc in binary format? What I mean is that assume I have the string:
"Hello" and I want to store it in binary format, so assume "Hello" is
11110000110011001111111100000000 in binary (I know it not, I just typed something quickly).
Can I store the above binary not as a string, but in the actual format with the bits.
In addition to this, is it actually possible to store less than 8 bits. What I am getting at is if the letter A is the most frequent letter used in a text, can I use 1 bit to store it with regards to compression instead of building a binary tree.
Is it possible to get strings, ints,
etc in binary format?
Yes. There are several different methods for doing so. One common method is to make a MemoryStream out of an array of bytes, and then make a BinaryWriter on top of that memory stream, and then write ints, bools, chars, strings, whatever, to the BinaryWriter. That will fill the array with the bytes that represent the data you wrote. There are other ways to do this too.
Can I store the above binary not as a string, but in the actual format with the bits.
Sure, you can store an array of bytes.
is it actually possible to store less than 8 bits.
No. The smallest unit of storage in C# is a byte. However, there are classes that will let you treat an array of bytes as an array of bits. You should read about the BitArray class.
What encoding would you be assuming?
What you are looking for is something like Huffman coding, it's used to represent more common values with a shorter bit pattern.
How you store the bit codes is still limited to whole bytes. There is no data type that uses less than a byte. The way that you store variable width bit values is to pack them end to end in a byte array. That way you have a stream of bit values, but that also means that you can only read the stream from start to end, there is no random access to the values like you have with the byte values in a byte array.
What I am getting at is if the letter
A is the most frequent letter used in
a text, can I use 1 bit to store it
with regards to compression instead of
building a binary tree.
The algorithm you're describing is known as Huffman coding. To relate to your example, if 'A' appears frequently in the data, then the algorithm will represent 'A' as simply 1. If 'B' also appears frequently (but less frequently than A), the algorithm usually would represent 'B' as 01. Then, the rest of the characters would be 00xxxxx... etc.
In essence, the algorithm performs statistical analysis on the data and generates a code that will give you the most compression.
You can use things like:
Convert.ToBytes(1);
ASCII.GetBytes("text");
Unicode.GetBytes("text");
Once you have the bytes, you can do all the bit twiddling you want. You would need an algorithm of some sort before we can give you much more useful information.
The string is actually stored in binary format, as are all strings.
The difference between a string and another data type is that when your program displays the string, it retrieves the binary and shows the corresponding (ASCII) characters.
If you were to store data in a compressed format, you would need to assign more than 1 bit per character. How else would you identify which character is the mose frequent?
If 1 represents an 'A', what does 0 mean? all the other characters?

String.Format() split integer value

I'm wondering if it's possible for .Net's String.Format() to split an integer apart into two sub strings. For example I have a number 3234 and I want to format it as 32X34. My integer will always have 4 or 6 digits. Is this possible using String.Format()? If so what format string would work?
P.S.
I know there is other ways to do this, but i'm specifically interested to know if String.Format() can handle this.
You can specify your own format when calling String.Format
String.Format("{0:00x00}", 2398) // = "23x93"
James, I'm not sure you've completely specified the problem. If your goal is to put the 'x' in the center of the string, Samuel's answer won't work for 6 digit numbers. String.Format("{0:00x00}", 239851) returns "2398x51" instead of "239x851"
Instead, try:
String.Format(val<10000 ? "{0:00x00}" : "{0:000x000}", val)
In either case, the method is called Composite Formatting.
(I'm assuming the numbers will be between 1000 and 999999 inclusive. Even then, numbers between 1000 and 1009 inclusive will report the number after the 'x' with an unnecessary leading '0'. So maybe this approach is valid for values between 1010 and 999999 inclusive.)
No, it can't.
In fact, it seems that your integers aren't integers. Perhaps they should be stored in a class, with its own ToString() method that will format them this way.

Categories

Resources