Convert decimal to binary but in an 8 bit format [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pad a binary string with zeros?
I am able to convert decimal numbers to binary but not in an 8 bit format. For example if I input 5 the result is 101 which is correct but how can I show the result in an 8 bit format such as 00000101?
string result = Convert.ToString(num, 2).PadLeft(8, '0');

The simplest way would be to use
string result = Convert.ToString(num, 2).PadLeft(8, '0');

Related

C# How to split advanced? [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 6 years ago.
I need to split a number with decimal places.
An example: I need to do 14525/1024/1024 but in my application, it says
0 MB
How to calculate 14525/1024/1024 to
00,01 MB
like
40.61 MB
(I need to convert bytes to megabytes)
You need to use decimal and not int:
decimal result = Math.Round((decimal)14525 / 1024 / 1024, 2);
cast any value to Double before to do the division

format decimal number using ToString [duplicate]

This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 9 years ago.
If I got decimal number like 14.50 and I want to be represented like decimal 10.2
0000000014.50
how can I do this?
Thank you
Use custom numeric format string:
var value = 14.50m;
string valueString = value.ToString("0000000000.00");
0 is a placeholder: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you don't have an issue with the data type being converted to string then you could use Padding in c#.
Refer the link below :
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.100).aspx

Converting string of binary to hex [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting long string of binary to hex c#
nyI'm looking for a way to convert a string of binary to a hex string.
the binary string has four positions.
the binary string looks something like this
string binarystring= "1011";
output string should be like this
output string="B";
is there any way to convert a string of binary into hex?
Convert.ToInt32("1011", 2).ToString("X");
For more information about the string value used with ToString() as its parameter, check the following documentation:
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

String conversion C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert from scientific notation string to float in C#
Is it there an build-in function which converts string in format "2.71e+006" to a number or I have to write my custom algorithm?
The Decimal Parse method has an overload you can use:
decimal d = Decimal.Parse("2.71e+006", System.Globalization.NumberStyles.Float);
You can also do the same for a Double.

How to Convert Hex String to Hex Number [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I convert hex string into signed integer?
example:"3A" convert to 0x3A,thanks a lot!
Convert.ToInt32("3A", 16)
int.Parse("3A",NumberStyles.HexNumber)
or
long.Parse("3A",NumberStyles.HexNumber)
etc...

Categories