【发布时间】:2013-11-21 01:22:05
【问题描述】:
考虑以下情况:(伪代码)
//All our luscious data
char theChar = 123;
int theInt = 4324;
char[] theCharArray = "sometext";
//Make an array to hold all of that data.
byte[] allTheVars = new *byte[sizeOfArray];
//Copy all vars into "allTheVars"
copyToEndOfArray(theChar, allTheVars);
copyToEndOfArray(theInt, allTheVars);
copyToEndOfArray(theCharArray, allTheVars);
因此,我们的想法是最终将一堆变量串在一起形成同一个字节数组。然后通过 Internet 传递该数组。现在假设所有这些变量都被发送到调用远程函数,如下所示。
//This is the function that will take in the data we sent over the network.
void remotelyCalledInternetFunction(char aChar, int anInt, char[] aCharArray)
{
}
不是通过繁琐的从字节数组中复制来手动将每个变量拆分为指定的类型,您可以通过执行这样的操作来“自动拆分”变量吗?
//Pass the byte array. The method knows what types it needs, maybe it will auto-split the data correctly?
remotelyCalledInternetFunction(allTheVars);
如果没有,我可以做类似的事情吗?
编辑:有什么办法可以做这样的事情吗?
remotelyCalledInternetFunction(allTheVars);
//Takes first 2 bytes of the array, the next 4 bytes, and the rest for the char[]?
void remotelyCalledInternetFunction(char aChar, int anInt, char[] aCharArray)
{
}
【问题讨论】:
-
我不知道你想达到什么目的,但你不能使用套接字和字节缓冲区吗?
-
您所描述的称为“编组”参数。所有 RPC 库都提供了自动执行此操作的方法。使用其中之一,而不是重新发明轮子。
-
优秀的信息 Barmar!我会四处寻找一个好的,有什么建议吗?
标签: c++ c methods arguments bytearray