将指针整数(int**)转换为c中的String
convert a pointer integer (int**) to String in c
我使用以下函数在c中生成一个动态2d数组:
int** Make2DintArray(int arraySizeX, int arraySizeY) { // From http://pleasemakeanote.blogspot.com/2008/06/2d-arrays-in-c-using-malloc.html
int** theArray;
theArray = (int**) malloc(arraySizeX*sizeof(int*));
for (int i = 0; i < arraySizeX; i++)
theArray[i] = (int*) malloc(arraySizeY*sizeof(int));
return theArray;
}
我想把矩阵(2d - array)的所有值连接起来,形成一个用#
分隔的字符串我用int hi11cipherSize = 20;
std::ostringstream text2Encrypt;///ERROR SOURCE
// i used the above function to creat a 2D array and fill it
// then i used the code below to loop through the record and create the string with delimiter #
for(int i =0; i<hi11cipherSize;i++){
for(int j =0; j<hi11cipherSize;j++){
printf("%d t",HillCipherMatrix[i][j]);
temp = HillCipherMatrix[i][j];
text2Encrypt<<(char)temp<<"#"; //// First error cannot concatenate int with char ???
} printf("n");
}
QString tempHLBP = QString::fromStdString(text2Encrypt.str()) ;
qDebug()<<"Text to encrypt "<<tempHLBP<<"n";
qDebug()<<"Length of string "<<tempHLBP.length();
我得到以下错误错误:
22:37:59: Running build steps for project StringManJVN...
22:37:59: Configuration unchanged, skipping qmake step.
22:37:59: Starting: "C:QtSDKQtCreatorbinjom.exe"
main.cpp
main.cpp(17) : error C2079: 'text2Encrypt' uses undefined class 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>
]
main.cpp(27) : error C2297: '<<' : illegal, right operand has type 'const char [2]'
main.cpp(27) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
main.cpp(32) : error C2228: left of '.str' must have class/struct/union
type is 'int'
jom: C:QTProjectStringManJVNMakefile.Debug [debugmain.obj] Error 2
cl -c -nologo -Zm200 -Zc:wchar_t- -Zi -MDd -GR -EHsc -W3 -w34100 -w34189 -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_SQL_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I"....QtSDKDesktopQt4.8.1msvc2010includeQtCore" -I"....QtSDKDesktopQt4.8.1msvc2010includeQtSql" -I"....QtSDKDesktopQt4.8.1msvc2010include" -I"....QtSDKDesktopQt4.8.1msvc2010includeActiveQt" -I"debug" -I"....QtSDKDesktopQt4.8.1msvc2010mkspecswin32-msvc2010" -Fodebug @C:UsersSONUTAppDataLocalTempmain.obj.3948.0.jom
C:QtSDKQtCreatorbinjom.exe -f Makefile.Debug
jom 1.0.8 - empower your cores
jom: C:QTProjectStringManJVNMakefile [debug] Error 2
22:38:02: The process "C:QtSDKQtCreatorbinjom.exe" exited with code 2.
Error while building project StringManJVN (target: Desktop)
When executing build step 'Make'
提前感谢;-))
试着把这个放到顶部:
#include <sstream>
阅读你上面的问题,你说你正试图把它放入一个字符串。如果您指的是std::string,那么这样做很容易。确保包含正确的字符串库
#include <string>
,然后你可以这样做:
int x = 4;
string s = "";
s += x + 48; //this will convert the number 4 into the char value for 4.
//it works for all ascii numbers
s += "#"; //puts the # sign in there
那或多或少会让你得到你想要的。我不确定这是不是最好的方法,但肯定很容易。显然,上面的步骤是为了清晰而展开的。你当然可以把它压缩一下
相关文章:
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- <string> 结构中的矢量无法正常工作
- 为什么一个表达式中的 std::string 连接给出的结果与逐个字符不同的结果?
- 比较存储在 std::string 中的数据中的字节数
- C++中的"string()+char"
- 连接到MySQL时如何修复xdev C++ api中的'string too long'错误
- android-ndk-r6-crystax-2 错误:命名空间'std'中的'string'未命名类型
- 命名空间中的"string is not a type"
- 使用C++中的String To Class Lookup表来实例化类
- 如何调用返回char* (Java中的String和c++中的char*)的方法
- 将指针整数(int**)转换为c中的String
- GetFunctionPointerForDelegate将委托中的String^参数转换成什么?
- c++中的string类可以有多大?
- 如何在C或c++中获得与Java中的toLowerCase或Python中的string.lower()相同的结果
- 从c++中的string继承
- c++中的String *[]转换为c#中的String[]
- NDK 构建错误:命名空间'std'中的'string'未命名类型
- 将二进制数据转换为ASCII等效,而不使用Arduino中的String对象