Converting long string of binary to hex c#
I just knocked this up. Maybe you can use as a starting point...
public static string BinaryStringToHexString(string binary)
{
if (string.IsNullOrEmpty(binary))
return binary;
StringBuilder result = new StringBuilder(binary.Length / 8 + 1);
// TODO: check all 1's or 0's... throw otherwise
int mod4Len = binary.Length % 8;
if (mod4Len != 0)
{
// pad to length multiple of 8
binary = binary.PadLeft(((binary.Length / 8) + 1) * 8, '0');
}
for (int i = 0; i < binary.Length; i += 8)
{
string eightBits = binary.Substring(i, 8);
result.AppendFormat("{0:X2}", Convert.ToByte(eightBits, 2));
}
return result.ToString();
}
Convert Binary string to Hex string without losing leading zeros
You can simply use
Convert.ToInt32(value,2).ToString("X3")
where the number following the X is the minimum number of digits you want in the final string. I used the number 3, because that was the output you included as an example. It's explained in more detail in this Microsoft documentation.
C# how convert large HEX string to binary
You can just convert each hexadecimal digit into four binary digits:
string binarystring = String.Join(String.Empty,
hexstring.Select(
c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')
)
);
You need a using System.Linq;
a the top of the file for this to work.
Converting long hex string to binary string throws OverflowException
You could convert a nibble at a time using a lookup array, for example:
public static string HexStringToBinaryString(string hexString)
{
var result = new StringBuilder();
string[] lookup =
{
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"
};
foreach (char nibble in hexString.Select(char.ToUpper))
result.Append((nibble > '9') ? lookup[10+nibble-'A'] : lookup[nibble-'0']);
return result.ToString();
}
Convert Hex string to Binary string C#
How about using String.PadLeft() ?
string value = "0x001";
string binary = Convert.ToString(Convert.ToInt32(value, 16), 2).PadLeft(12, '0');
Convert 64bit Binary to Long equivalent
Here you go:
http://msdn.microsoft.com/en-us/library/system.convert.toint64.aspx
And examples here: http://www.csharphelp.com/2007/09/converting-between-binary-and-decimal-in-c/
Specifically (where bin is a 'binary' string):
long l = Convert.ToInt64(bin,2);
Conversion of Decimal to Hexadecimal
Easiest way to achieve it would be to regard the digits as ASCII characters(0-9 have ASCII values 48-57) and convert it to Hex like you are already doing.
If you want to do it in another way, you would need to introduce new logic to your program. It depends upon your personal preference.
Related Topics
How to Ignore Get-Only Properties in JSON.Net Without Using JSONignore Attributes
How to Calculate Divide and Modulo for Integers in C#
Removing Hidden Characters from Within Strings
Wpf: Cannot Reuse Window After It Has Been Closed
"Nested Foreach" VS "Lambda/Linq Query" Performance(Linq-To-Objects)
How to Find the Parent Directory in C#
The Remote Server Returned an Unexpected Response: (413) Request Entity Too Large
How to Register Windows Forms with Simple Injector
Passing Parameters to Xslt Stylesheet via .Net
How to Disable Visual Styles for Just One Control, and Not Its Children
How to Read the Current Path of |Datadirectory| from Config Settings
Applying % Number Format to a Cell Value Using Openxml
.Net: Unable to Cast Object to Interface It Implements