简体   繁体   English

我想将 int 转换为 char 数组,其中 int 的 0-7 位是第一个 char 8-15 第二个等

[英]I want to convert int to char array where 0-7 bits of int are 1st char 8-15 2nd etc

So here is the code but for some reason intToChar function returns "abcd" and some gibberish at the end.所以这里是代码,但由于某种原因 intToChar function 返回“abcd”,最后还有一些乱码。 I was trying to change my newly created array to 0 first but then I'm getting access violation errors.我试图首先将我新创建的数组更改为 0,但随后出现访问冲突错误。

int charToInt(char x[])
{
    int z = 0;
    for (int i = 0; i < sizeof(int) / sizeof(char) && x[i]; i++)
    {
        z <<= sizeof(char) * 8;
        z |= x[i];
    }
    return z;
}
char* intToChar(int x)
{
    char* z = new char[5];
    for (int i = (sizeof(z) / sizeof(z[0]) - 1); i >= 0; i--)
    {
        z[i] = x;
        x >>= sizeof(char) * 8;
    }
    return z;
}

int main()
{
    char xd[5] = "abcd";
    cout << xd;
    cout << endl;
    int x = charToInt(xd);
    cout << x;
    cout << endl;
    char* y = intToChar(x);
    cout << y;
    cout << endl;
    return 0;
}

As noted in the comments, your use of sizeof(z) is sizeof(a_pointer) -- not what you want.如评论中所述,您对sizeof(z)的使用是sizeof(a_pointer) - 不是您想要的。 Instead, given that sizeof(char) is always 1 , you can simply do:相反,鉴于sizeof(char)始终为1 ,您可以简单地执行以下操作:

#include <iostream>

int charToInt (char *x)
{
    int z = 0;
    
    for (size_t i = 0; i < sizeof(int) && x[i]; i++)
    {
        z <<= 8;
        z |= x[i];
    }
    
    return z;
}

char *intToChar (int x)
{
    char *z = new char[5];
    
    for (int i = (int)sizeof (int) - 1; i >= 0; i--)
    {
        z[i] = x;
        x >>= 8;
    }
    
    return z;
}

int main()
{
    char xd[5] = "abcd";
    std::cout << xd << '\n';
    
    int x = charToInt (xd);
    std::cout << x << '\n';
    
    char *y = intToChar(x);
    std::cout << y << '\n';
}

( note: the loop limits have been adjusted to reflect that fact you are utilizing only 4-bytes in your conversion either way) 注意:循环限制已调整以反映您在转换中仅使用 4 字节的事实)

Example Use/Output示例使用/输出

$ ./bin/chr2int2char
abcd
1633837924
abcd

Alternatively, you can write intToChar() as follows:或者,您可以编写intToChar()如下:

char *intToChar (int x)
{
    char *z = new char[5];
    int i  = sizeof (int);
    z[i] = 0;               /* affirmatively nul-terminate z */
    
    while (i--)
    {
        z[i] = x;
        x >>= 8;
    }
    
    return z;
}

Also, if you were calling from a function other than main() , you would want to ensure you delete[] y;此外,如果您从除main()以外的 function 调用,则需要确保delete[] y; to avoid a memory leak, eg避免 memory 泄漏,例如

    char *y = intToChar(x);
    std::cout << y << '\n';
    
    delete[] y;

Let me know if you have further questions.如果您还有其他问题,请告诉我。

You have couple of problems in the code you have write:您编写的代码中有几个问题:

  1. You do: "(sizeof(z) / sizeof(z[0]) - 1)" but size of 'z' is the size of pointer which is 8 bytes in most PCs today.你这样做: "(sizeof(z) / sizeof(z[0]) - 1)"但“z”的大小是指针的大小,在当今大多数PC中为8字节。

  2. You haven't set the null terminator in the string, in the last byte of the char*您尚未在 char* 的最后一个字节中的字符串中设置 null 终止符

z[4] = '\0'; z[4] = '\0';

  1. You forgot to delete the dynamic allocation of the char*, you should delete it in the main function after printing it.忘记删除char*的动态分配了,打印后应该在主function中删除。

If you will fix all of that, it will work as expected, but here you have a simple code to perform what you meant:如果您将解决所有这些问题,它将按预期工作,但在这里您有一个简单的代码来执行您的意思:

int charToInt(char x[])
{
    int z = 0;
    for (int i = 0; i < 4; i++)
    {
      z <<=  8;        
      z |= x[i];
    }
    return z;
}


char* intToChar(int x)
{  
  char* z = new char[5];
  for(int i = 0; i < 4; ++i) {
    z[i] = x >> (4 - i - 1) * 8;
  }
  z[4] = '\0';
  
  return z; 
}


int main()
{
  char xd[5] = "abcd";
  cout << xd << endl;
  
  int x = charToInt(xd);
  cout << x <<endl;
  
  char* y = intToChar(x);
  cout << y << endl;
  delete[] y;
  return 0;
}

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

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