Byte Buffer To String - c#

Reading a Byte buffer:
while (...)
{
builder.Append(Encoding.ASCII.GetString(buffer, index, 1));
++index;
}
I'm getting the following result: "20202020202020202020202057363253304b4358", which looks like ASCII or HTML character codes. What is the best and faster way to obtain the real string out of that value in C#?

Although I think there is something wrong in your code while getting that string, anyway, you can use
byte[] buf = SoapHexBinary.Parse("20202020202020202020202057363253304b4358").Value;
var str = Encoding.ASCII.GetString(buf);
which would return            W62S0KCX
PS: SoapHexBinary is in wellknown System.Runtime.Remoting.Metadata.W3cXsd2001 namespace :)

If you have the entire buffer available already, then simply try:
var myString = Encoding.Default.GetString(byteBuffer);

Related

Save byte array to SQLite database c#

please I can not solve it, some ideas? I see in DB only "System.Byte []" but no values from array.
Thank you.
Calling ToString() on a byte[] yields
System.Byte[]
But assuming that your byte[] contains a (UTF8 formatted) string, you could use
byte[] array= ...;
string myString = System.Text.Encoding.UTF8.GetString(array, 0, array.Length);
instead.

How to encode text in C#?

I faced a question in an interview test but i got confused and didnt answer that.Can anyone tell me how to encode a text in C#?
The question is :
How to encode a following text
a. U0VDUkVUIENPREUgSVMgTkVYR0VOIElTIFRFU1RJTkc=
I tried following code on my own just for try but didnt get any good output:
static void Main(string[] args)
{
string str= "U0VDUkVUIENPREUgSVMgTkVYR0VOIElTIFRFU1RJTkc=";
byte[] _telnetData;
_telnetData = new byte[1024];
_telnetData = Encoding.ASCII.GetBytes(str);
Console.WriteLine(_telnetData);
// _networkstream.Write(_telnetData, 0, _telnetData.Length);
Console.ReadLine();
}
I got following output:
Any help would be highly appreciable.
That is Base64 encoded text, so you should use Convert.ToBase64String(byte[]).
string text = "SECRET CODE IS NEXGEN IS TESTING";
Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(text)));
The dead giveaway is the characters not being limited to A-F and 0-9, the "=" on the end is not always present, but further suggests base64.
You already encoded it with this line
telnetData = Encoding.ASCII.GetBytes(str);
Now you have the encoded text in a byte array. By definition encoding is converting text to byte array containing the appropriate byte values for the current text.

Bit Array to String and back to Bit Array

Possible Duplicate Converting byte array to string and back again in C#
I am using Huffman Coding for compression and decompression of some text from here
The code in there builds a huffman tree to use it for encoding and decoding. Everything works fine when I use the code directly.
For my situation, i need to get the compressed content, store it and decompress it when ever need.
The output from the encoder and the input to the decoder are BitArray.
When I tried convert this BitArray to String and back to BitArray and decode it using the following code, I get a weird answer.
Tree huffmanTree = new Tree();
huffmanTree.Build(input);
string input = Console.ReadLine();
BitArray encoded = huffmanTree.Encode(input);
// Print the bits
Console.Write("Encoded Bits: ");
foreach (bool bit in encoded)
{
Console.Write((bit ? 1 : 0) + "");
}
Console.WriteLine();
// Convert the bit array to bytes
Byte[] e = new Byte[(encoded.Length / 8 + (encoded.Length % 8 == 0 ? 0 : 1))];
encoded.CopyTo(e, 0);
// Convert the bytes to string
string output = Encoding.UTF8.GetString(e);
// Convert string back to bytes
e = new Byte[d.Length];
e = Encoding.UTF8.GetBytes(d);
// Convert bytes back to bit array
BitArray todecode = new BitArray(e);
string decoded = huffmanTree.Decode(todecode);
Console.WriteLine("Decoded: " + decoded);
Console.ReadLine();
The Output of Original code from the tutorial is:
The Output of My Code is:
Where am I wrong friends? Help me, Thanks in advance.
You cannot stuff arbitrary bytes into a string. That concept is just undefined. Conversions happen using Encoding.
string output = Encoding.UTF8.GetString(e);
e is just binary garbage at this point, it is not a UTF8 string. So calling UTF8 methods on it does not make sense.
Solution: Don't convert and back-convert to/from string. This does not round-trip. Why are you doing that in the first place? If you need a string use a round-trippable format like base-64 or base-85.
I'm pretty sure Encoding doesn't roundtrip - that is you can't encode an arbitrary sequence of bytes to a string, and then use the same Encoding to get bytes back and always expect them to be the same.
If you want to be able to roundtrip from your raw bytes to string and back to the same raw bytes, you'd need to use base64 encoding e.g.
http://blogs.microsoft.co.il/blogs/mneiter/archive/2009/03/22/how-to-encoding-and-decoding-base64-strings-in-c.aspx

C# Byte[] Byte array to Unicode string

I need very fast conversion from byte array to string.
Byte array is Unicode string.
From byte[] array to string
var mystring = Encoding.Unicode.GetString(myarray);
From string to byte[]
var myarray2 = Encoding.Unicode.GetBytes(mystring);
Try this
System.Text.UnicodeEncoding.Unicode.GetString
UTF8 (I think you mean "UTF8" instead of "Unicode"). Because, U'll get just Chinese Symbols. ;)
Maybe it helps to change...
var mystring = Encoding.Unicode.GetString(myarray);
...to...
var mystring = Encoding.UTF8.GetString(myarray);
:)

how to response.write bytearray?

This is not working:
byte[] tgtBytes = ...
Response.Write(tgtBytes);
You're probably looking for:
Response.BinaryWrite(tgtBytes);
MSDN documentation here.
Response.OutputStream.Write(tgtBytes, 0, tgtBytes.Length);
If you want to output hex values
byte[] tgtBytes = ...
foreach (byte b in tgtBytes)
Response.Write("{0:2x}", b);
Or do you want to do;
Response.Write(System.Text.Encoding.ASCII.GetString(tgtBytes));
To convert the bytes to ASCII text and output a string.

Categories

Resources