简体   繁体   English

指向char指针的int数组

[英]int array to char pointer

I'm working on C and I have a question.. I'm writing a program with Mcrypt and the decryption function require the chipertext at a char *pointer. 我正在使用C语言,但我有一个问题。.我正在用Mcrypt编写程序,并且解密函数需要char * pointer处的chipertext。 The problem is that the chipertext is filled with unknown ASCII symbols.. So I printf the chipertext with %d and I get chipertext in numbers like this.. 问题是该Chipertext充满了未知的ASCII符号。因此,我用%d printf该Chipertext,并得到像这样的数字的Chipertext。

23 -83 -48 -36 -49 -26 -16 -42 101 111 127 -46 -10 -3 -33 110 -106 29 -112 123 -21 43 50 81 70 -101 -71 94 -63 -122 52 76

My question is.. I take this chipertext and store it in to a int array[32] .. How can I copy the contents of this int array to my char *pointer? 我的问题是..我将这个Chipertext并将其存储到int array[32] 。如何将此int数组的内容复制到char * pointer?

我认为最简单的解决方案是将密文存储在char array [32]中

How to copy an array of int to an array of char : 如何将int数组复制到char数组:

int iarray[32];
char carray[sizeof(iarray)];

memcpy(carray, iarray, sizeof(iarray));

If carray is a pointer instead of an array, the code remains largely the same: 如果carray是指针而不是数组,则代码大致相同:

int iarray[32];
char *carray = addressOfSomeMemory;

memcpy(carray, iarray, sizeof(iarray));

However, the code above assumes your data is stored as char s packed into int s. 但是,上面的代码假定您的数据以char形式存储在int If instead your data is stored one char per int , you need more code: 相反,如果您的数据每个int存储一个char ,则需要更多代码:

for (int i = 0; i < 32; i++)
    *(carray+i) = (char)iarray[i];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM