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.
Related
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.
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);
I have the following code:
string s = "2563MNBJP89256666666685755854";
Byte[] bytes = encoding.GetBytes(s);
string hex = "";
foreach (byte b in bytes)
{
int c=b;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(c.ToString()));
}
It prints the hex values . How can I add it in a vector ob bytes like this?
new byte [0x32,0x35..]
In hex I have : 323536....and so on. The next step is to add then in a byte[] vector in the following format 0x32,0x35..and so on; How to do this?
THX
Isn't bytes already the list of bytes you want?
C#: System.Text.Encoding.ASCII.GetBytes("test")
For C# you could try
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(strVar);//strVar is the string variable
in C# you can use Encoding.GetBytes Method
I need to convert a Byte[] to a string which contain a binary number sequence. I cannot use Encoding because it just encodes bytes into characters!
For example, having this array:
new byte[] { 254, 1 };
I want this output:
"1111111000000001"
You can convert any numeric integer primitive to its binary representation as a string with Convert.ToString. Doing this for each byte in your array and concatenating the results is very easy with LINQ:
var input = new byte[] { 254 }; // put as many bytes as you want in here
var result = string.Concat(input.Select(b => Convert.ToString(b, 2)));
Update:
The code above will not produce a result having exactly 8 characters per input byte, and this will make it impossible to know what the input was just by looking at the result. To get exactly 8 chars per byte, a small change is needed:
var result = string.Concat(input.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
string StringIWant = BitConverter.ToString(byteData);
But i suggest work with Encoding..
string System.Text.Encoding.UTF8.GetString(byte[])
EDIT: For like 10101011010
From #Quintin Robinson's answer;
StringBuilder sb = new StringBuilder();
foreach (byte b in myByteArray)
sb.Append(b.ToString("X2"));
string hexString = sb.ToString();
Perhaps you can use the BitArray class to accomplish what you are looking for? They have some sample code in the reference which should be pretty easy to convert to what you are looking for.
Will BitConverter.ToString work for you?
byte[] bList = { 0, 1, 2, 3, 4, 5 };
string s1 = BitConverter.ToString(bList);
string s2 = "";
foreach (byte b in bList)
{
s2 += b.ToString();
}
in this case s1 ="01-02-03-04-05"
and s2= "012345"
I don't really get what are you trying to achieve.
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);
:)