我正在尝试将以下python提取转换为C
tvip = "192.168.0.3"
myip = "192.168.0.7"
mymac = "00-0c-29-3e-b1-4f"
appstring = "iphone..iapp.samsung"
tvappstring = "iphone.UE55C8000.iapp.samsung"
remotename = "Python Samsung Remote"
ipencoded = base64.b64encode(myip)
macencoded = base64.b64encode(mymac)
messagepart1 = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \
+ chr(0x00) + ipencoded + chr(len(macencoded)) + chr(0x00) \
+ macencoded + chr(len(base64.b64encode(remotename))) + chr(0x00) \
+ base64.b64encode(remotename)
part1 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
+ chr(len(messagepart1)) + chr(0x00) + messagepart1
我实际上并不知道messagepart1
在数据结构方面实际代表什么。
这是我的尝试:
var myip_e = myip.ToBase64();
var mymac_e = mymac.ToBase64();
var m1 = (char)0x64 + (char)0x00 + (char)myip_e.Length + (char)0x00 + myip_e +
(char)mymac_e.Length + (char)0x00 + mymac_e + (char)remotename.ToBase64().Length + (char)0x00 +remotename.ToBase64();
var p1 = (char)0x00 + (char)appstring.Length + (char)0x00 + appstring + (char)m1.Length + (char)0x00 + m1;
//var b1 = p1.GetBytes(); // this is to write to the socket.
public static string ToBase64(this string source)
{
return Convert.ToBase64String(source.GetBytes());
}
public static byte[] GetBytes(this string source)
{
byte[] bytes = new byte[source.Length * sizeof(char)];
System.Buffer.BlockCopy(source.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
我比较的方式是我打印到控制台,如果正确的话,期望它们是相同的 - 显然我做错了。
答案 0 :(得分:0)
尝试查看SharpDevelop,它们支持C#和IronPython,并且在这个IDE中有不同语言之间的转换器。
http://www.icsharpcode.net/OpenSource/SD/Features.aspx
不确定,它完全解决了你的问题,但可能会有所帮助。
答案 1 :(得分:0)
因此,在考虑了评论后,我现在有了一些工作代码。以下是更改
var builderM1 = new StringBuilder();
builderM1.Append((char)0x64);
builderM1.Append((char)0x00);
builderM1.Append((char)myip_e.Length);
builderM1.Append((char)0x00);
builderM1.Append(myip_e);
builderM1.Append((char)mymac_e.Length);
builderM1.Append((char)0x00);
builderM1.Append(mymac_e);
builderM1.Append((char)remotename.ToBase64().Length);
builderM1.Append((char)0x00);
builderM1.Append(remotename.ToBase64());
var m1 = builderM1.ToString();
var builderP1 = new StringBuilder();
builderP1.Append((char)(0x00));
builderP1.Append((char)appstring.Length);
builderP1.Append((char)(0x00));
builderP1.Append(appstring);
builderP1.Append((char)m1.Length);
builderP1.Append((char)(0x00));
builderP1.Append(m1);
Console.WriteLine(builderP1.ToString());
public static string ToBase64(this string source)
{
return Convert.ToBase64String(Encoding.ASCII.GetBytes(source));
}
答案 2 :(得分:0)
这是基于@ Bakuriu评论的替代解决方案:
var m1 = string.Format("\x64\x00{0}\x00{1}{2}\x00{3}{4}\x00{5}",
(char)myip_e.Length, myip_e, (char)mymac_e.Length, mymac_e,
(char)remotename.ToBase64().Length, remotename.ToBase64());
var p1 = string.Format("\x00{0}\x00{1}{2}\x00{3}",
(char)appstring.Length, appstring, (char)m1.Length, m1);