我有一个字符数组:
char message[];
和一个8位整数
uint8_t remainder
我想将它们都视为位数组,并像这样减去它们:
message - remainder
并将结果视为char数组:
一个例子是
char* message = "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ";
// Equivalent to a 512-bit array of only 1s
uint8_t remainder = 1;
// Substract here message-remainder
printf("%s", message)
// Output: "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþ"
// As the 512 bit array would still be just 1s except for the first bit which now is 0, so the first char would be 254 instead of 255
有没有可能的方法?我考虑过将char数组转换为int,但问题是它通常是64字节数组,因此我不能将其视为int。我认为该方法可以使用按位运算符解决,但是我还没有弄清楚如何将它们相减。
有什么建议吗?
根据要求:
如果数据是未对齐的,不是字的大小等,则可能需要按字节执行此操作,但是您已经声明不是这种情况。
享受。
((注:强烈建议您使用BigNum类型库,而不要自己动手。总有一天,可能需要移植此代码,并且这种方法很可能在发生这种情况时中断...)
首先这样做通常不是一个好主意:-((通常会导致一些缓冲区溢出)