Converting string of binary to hex [duplicate] - c#

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

Related

Convert.ToInt32 not accepting string hexadecimal values [duplicate]

This question already has answers here:
Convert integer to hexadecimal and back again
(11 answers)
How to Convert Hex String to Hex Number [duplicate]
(2 answers)
Closed 4 years ago.
I'm currently making a program which saves a bunch of memory addresses to a class automatically. However,
Convert.ToInt32(value)
doesn't seem to want to accept the string value "0xB24C" as a valid integer.
'Input string was not in a correct format.'
Actually I'm able to just save offsets as integers like this public const Int32 m_ArmorValue = 0xB24C;
Here's my code where I assign the integer;
hazedumper.netvars.m_ArmorValue = Convert.ToInt32(value);
value being the string offset "0xB24C"
Can anyone tell me why this error is occurring or is it not possible to convert a string memory address/hexadecimal value to an Int32.
Convert.ToInt32() takes the base as second parameter. Furthermore you must remove the 0x prefix. See the docs for details
var s="0xA123";
var i = Convert.ToInt32(s.Substring(2), 16);

How to convert UTF-8 to string in C# [duplicate]

This question already has answers here:
How to Decode "=?utf-8?B?...?=" to string in C#
(4 answers)
Closed 8 years ago.
H, I have this string that I got from a htm file
String s = "%u05d9%u05e8%u05d5%u05e9%u05dc%u05d9%u05dd"
It is in UTF8 code value Hebrew characters and I want to convert it to a real string that I can write to a file and have meaning (not just the code value of the char set).
I tried to do this but it does not work -
byte[] bytes = Encoding.UTF8.GetBytes(s);
addr = Encoding.UTF8.GetString(bytes);
The original string might have been UTF-8 encoded, but this is irrelevant because what you have right now is an URL encoded string. You can decode it using HttpUtility.UrlDecode:
System.Web.HttpUtility.UrlDecode("%u05d9%u05e8%u05d5%u05e9%u05dc%u05d9%u05dd")
returns ירושלים
try
System.Net.WebUtility.UrlDecode(s);

issue in converting hexadecimal to byte array [duplicate]

This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
Closed 8 years ago.
i have string like
string test = "0x527AE53437CAED39D3E2A8B3E90FFC3BA9073B6D933BC05FF6677B5521DC34"
How can i convert the string to byte[]?
use SoapHexBinary in namespace System.Runtime.Remoting.Metadata.W3cXsd2001
string s = "0x527AE53437CAED39D3E2A8B3E90FFC3BA9073B6D933BC05FF6677B5521DC34";
byte[] num = SoapHexBinary.Parse(s.Substring(2)).Value;
That string is hexadecimal.
If you want to convert it to a numeric you're going to need special handling - that's a very big number and will overflow the basic types.
If it was a reasonable size, all numeric types are agnostic of base-representation. To see a hexadecimal version of a number, you simply call .ToString("X") on it.
EDIT
My answer was based on the initial version of the question before the byte[] was specified. There is a previous SO question and answer for this: How can I convert a hex string to a byte array?

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.

Binary String to ASCII TEXT String [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Binary To Corresponding ASCII String Conversion
I posted a slimier question before but didn't got a correct answer..
my problem is i have a binary string as
00100000011101110110000101101110011101000010000001110100011011110010000001100100011011110111
Which im required to convert to "Normal ASCII String Text"
I used BinaryReader and read and encode System.Text.ASCIIEncoding.ASCII but im getting the same 0101010 string
byte[] bytearray = b.ReadBytes((int)length);
System.Text.Encoding encEncoder = System.Text.ASCIIEncoding.ASCII;
string bb = encEncoder.GetString(bytearray);
any help will be grateful !
first translate the binary string into a byte[] using How could I encode a string of 1s and 0s for transport?
next use the Convert.ToBase64String method to get the actual System.String

Categories