Binary String to ASCII TEXT String [duplicate] - c#

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

Related

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?

convert string to UCS2 encoding [duplicate]

This question already has answers here:
Convert to UCS2
(4 answers)
Closed 9 years ago.
I am working on a C# based application and came up with this problem of converting a string to UCS2 encoding. Basically, I have to take some text from XML and convert it to UCS2 and write this UCS2 code to a *.dat file.
Is there a way to do this (convert a string to UCS2 encoding) in C#.NET? if not, what are the options then?
Thanks,
There isn't an included Encoding in .NET that will convert the string to UCS-2 encoding. Your best bet is BigEndianUnicode or UTF-16, but these won't work very well.

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 to byte[] and vice versa? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
.NET String to byte Array C#
How do I convert String to byte[] array and vice versa? I need strings to be stored in some binary storage. Please show example in both directions. And one more thing: each string maybe bigger than 90Kb.
If you want to use UTF-8 encoding:
// string to byte[]
byte[] bytes = Encoding.UTF8.GetBytes(someString);
// byte[] to string
string anotherString = Encoding.UTF8.GetString(bytes);
Before you march off and use one of the examples someone's already given you should be aware that there is, in general, no unique mapping between a string and a sequence of bytes. How the string is mapped to binary (and vice versa) is determined by the encoding that you use. Joel Spolsky wrote an awesome article on this subject.
When decoding binary to get a string, you need to use the same encoding as was used to produce the binary in the first place, otherwise you'll run into problems.
Use the Encoding class.
How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

Categories