我有一组静态库(.lib)文件,其中一个文件可能是使用不同版本的Visual Studio构建的.这导致链接所有这些项目的项目的代码生成失败.有没有办法确定使用哪个版本的Visual Studio来编译静态库?
我正在编写与外部API接口的代码,我无法更改:
public class ExternalAPI
{
public static void Read(byte[] buffer);
public static void Read(int[] buffer);
public static void Read(float[] buffer);
public static void Read(double[] buffer);
}
Run Code Online (Sandbox Code Playgroud)
在读取数据时调用正确的重载方法很重要buffer,但是一旦读入数据,我将一般地处理它.我在代码中的第一次传递是:
public class Foo<T>
{
T[] buffer;
public void Stuff()
{
ExternalAPI.Foo(buffer);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,C#不会转换T[]为byte[].有没有办法枚举T可以明确表示的类型?我尝试过使用一个where T :条款,但似乎没有办法说出来where T : {byte, int, float, double and nothing else ever}?
按照这里的建议:匹配数字类型的通用约束,我向泛型添加了约束,并且还为我的模拟API添加了一个泛型方法,该方法将object参数作为参数
public class ExternalAPI
{
public static void Read(object buffer);
public …Run Code Online (Sandbox Code Playgroud) 我的一个类声明了一个模板化的函数:
template<class A, class B>
A do_something(const std::vector<B> &data)
Run Code Online (Sandbox Code Playgroud)
我想部分专注于typename A.B是一个实现非常小的接口的类型系列,我们使用了很多,所以我希望我的专业化是通用的B.我怀疑这是双重烦恼,因为typename A它只用作返回类型.
从互联网上,我发现我不能部分专门化一个函数,所以我创建了一个类,如下所示:
template<class A, class B>
class do_something_implementation {
public:
do_something_implementation(const std::vector<B> &data_) {
data = data_;
}
int do_something_implementation<int, B>::operator()() {
/* Complicated algorithm goes here... */
}
double do_something_implementation<double, B>::operator()() {
/* Different complicated algorithm goes here... */
}
private:
std::vector<B> data;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它(使用Visual Studio 2008)时,编译器崩溃(!),我收到以下错误:
fatal error C1001: An internal error has occurred in the compiler.
Run Code Online (Sandbox Code Playgroud)
我认为这是我的问题,而不是编译器.有没有更好的方式来表达我的目标部分专业化?
我正在研究C++中的一个问题,它涉及大量数据的大量子集和转换操作.为此,我创建了一个map函数和list comprehensions之类的东西.我发现我写的一堆谓词也有反转,所以我需要写:
template <typename type_t>
bool HasTenFoo(const type_t &t) {
return t.foo >= 10.0;
}
Run Code Online (Sandbox Code Playgroud)
和
template <typename type_t>
bool DoesntHaveTenFoo(const type_t &t) {
return t.foo < 10.0;
}
Run Code Online (Sandbox Code Playgroud)
这些都不是一个真实的例子,但它们具有代表性.我也使用了相当数量的仿函数:
class HasEnoughFoo {
public:
HasEnoughFoo (double bar) { this->bar = bar; }
template<typename type_t>
bool operator()(const type_t &t) const { return t.foo >= bar; }
private:
double bar;
};
Run Code Online (Sandbox Code Playgroud)
其中一些也应该反转.我不想不必要地重复代码,而是编写一个将谓词作为参数并将返回该谓词的(反转值)的仿函数.我的拳头切成一个在下面:
/* -- Returns the opposite of some other predicate -------------------------- */
template<typename predicate_t>
class Not {
public:
template <typename predicate_t> …Run Code Online (Sandbox Code Playgroud) 我正在编写一个Android应用程序,希望验证C#程序生成的字符串是否可信(即由我编写的另一个应用程序生成).为此,我正在签署字符串并将字符串和字符串的哈希值传输到Android设备.我可以在Android应用程序中包含用于创建散列字符串的公钥,因此我不必传输它.产生所有这些的C#代码类似于以下内容:
bytes = ASCIIEncoding.ASCII.GetBytes(someString);
provider = new RSACryptoServiceProvider();
signature = provider.SignData(bytes, new SHA1CryptoServiceProvider());
keyParameters = cryptoProvider.ExportParameters(false);
Run Code Online (Sandbox Code Playgroud)
为了传输数据,我使用以下命令将签名编码为base64:
signatureString = System.Convert.ToBase64String(signature);
我从关键参数手动提取模数和公共指数.它们似乎都是Base64编码.在android应用程序中,我试图验证签名如下:
String modulus = "<modulus string here...>";
String exponent = "<exponent string here...>";
BigInteger BIModulus = new BigInteger(1, Base64.decode(modulus));
BigInteger BIExponent = new BigInteger(1, Base64.decode(exponent));
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(BIModulus, BIExponent);
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] decodedBytes = cipher.doFinal(Base64.decode(signature));
Run Code Online (Sandbox Code Playgroud)
当我与decodedBytes原始字符串的SHA1哈希进行比较时,它们是不同的.试图找出原因,我通过大量的记录声明来完成整个过程.似乎模数的BigInteger与C#代码使用的模数不同.指数也是如此.我怀疑这是因为Java的字节类型是无符号的?
那么,有两个问题:
1)我在做什么(或不做)错了?有没有办法手动将模数和指数从一个设备移动到另一个设备以验证签名?
2)有没有办法实现我的目标,那就是在更合适的抽象层次上工作?