I'm creating my own DNS server and host blocker, I want to get host from DNS request message byte[]
dns message hex dump:
e07901000001000000000000057961686f6f03636f6d0000010001
.y...........yahoo.com.....
code:
using System;
using System.Text;
public class Program
{
public static void Main()
{
string b64 = "4HkBAAABAAAAAAAABXlhaG9vA2NvbQAAAQAB";
int pad = b64.Length % 4;
if (pad > 0 )
{
b64 += new string('=', 4 - pad);
}
byte[] decoded = Convert.FromBase64String(b64);
int start = 13;
int end = start;
while(decoded[end] != 0){
end++;
}
int hostLength = end-start;
byte[] byteHost = new byte[hostLength];
Array.Copy(decoded, start, byteHost, 0, hostLength);
string host = Encoding.Default.GetString(byteHost);
Console.WriteLine(host); // yahoo?com
}
}
The questions:
- is my method above to get host name right/efficient/fastest ?
- why I get weird character replacing the dot
yahoo?com
?
change to Encoding.ASCII or Encoding.UTF8 has no effect
question from:
https://stackoverflow.com/questions/65933992/how-to-get-a-string-from-a-bytes-array 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…