将 NSData 转换为 int 时出现问题

发布于 2024-09-06 04:01:53 字数 1087 浏览 4 评论 0原文

在 Mac 上使用基础和可可框架,我试图将 NSData 对象转换为人类可以理解的数字。 假设 NSData 对象是 NPIXEL 的图像。我知道二进制数据以大端编码并表示 32 位整数(更准确地说是 32 位二进制补码整数)。我编写了下面的代码将 NSData 转换为 int 数组。但我得到的值是完全错误的(这并不意味着测量不好,我使用特殊的软件读取数据,软件给出的值与我用代码得到的值不同)。

-(int *) GetArrayOfLongInt
{
  //Get the total number of element into the Array
  int Nelements=[self NPIXEL];
  //CREATE THE ARRAY
  int array[Nelements];
  //FILL THE ARRAY
  int32_t intValue;
  int32_t swappedValue;
  double Value;
  int Nbit = abs(BITPIX)*GCOUNT*(PCOUNT + Nelements); Nbit/=sizeof(int32_t);
  int i=0;
  int step=sizeof(int32_t);
  for(int bit=0; bit < Nbit; bit+=step)
  {
    [Img getBytes:&swappedValue range:NSMakeRange(bit,step)];
    intValue= NSSwapBigIntToHost(swappedValue);
    array[i]=intValue;
    i++;
  }
  return array;
}

当二进制数据表示 float 或 double 时,这段代码(稍作更改)可以完美工作,但当它是 16,32 或 64 位整数时,我不会。我还尝试将 NSSapBigIntToHost更改为NSSwapLittleInttoHost`。我什至尝试了很长的时间,但结果仍然是一样的,我得到了不好的值。我做错了什么?

PS:我的代码中的一些变量已经在我的程序中的其他地方设置了。 BITPIX 是每个像素的位大小。在本例中,32。GCOUNT 等于 1,PCOUNT 0,Nelements 是图像中应具有的像素总数。

Using foundation and cocoa frameworks on Mac, I am trying to convert an NSData object in humanly understandable number.
Let say the NSData object is an image of NPIXEL. I know the binary data are coded in big endian and represent 32 bit integer (to be more precise 32 bit two complements integer). I write the piece of code bellow to convert the NSData into an int array. But the value I got are completely wrong (this does not means the measurement are bad, I used a special software to read the data and the value given by the software are different from the one I got with my code).

-(int *) GetArrayOfLongInt
{
  //Get the total number of element into the Array
  int Nelements=[self NPIXEL];
  //CREATE THE ARRAY
  int array[Nelements];
  //FILL THE ARRAY
  int32_t intValue;
  int32_t swappedValue;
  double Value;
  int Nbit = abs(BITPIX)*GCOUNT*(PCOUNT + Nelements); Nbit/=sizeof(int32_t);
  int i=0;
  int step=sizeof(int32_t);
  for(int bit=0; bit < Nbit; bit+=step)
  {
    [Img getBytes:&swappedValue range:NSMakeRange(bit,step)];
    intValue= NSSwapBigIntToHost(swappedValue);
    array[i]=intValue;
    i++;
  }
  return array;
}

This piece of code (with minor change) work perfectly when the binary data represent float or double, but I dont when it is 16,32 or 64 bit integer. I also tried changingNSSapBigIntToHostintoNSSwapLittleInttoHost`. I even tried with long, but the results is still the same, I got bad values. What wrong I am doing ?

PS: Some of the variable in my code are already set elsewhere in my program. BITPIX is the bit size of each pixel. In this case 32. GCOUNT is equal to 1, PCOUNT 0 and Nelements is the total number of pixel I should have in my image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2024-09-13 04:01:53

返回指向局部变量的指针是一个非常糟糕的主意array 可能随时被覆盖(或者如果您要通过指针写入,则可能会损坏堆栈)。您可能想要这样的东西:

// CREATE THE ARRAY
int *array = malloc(Nelements * sizeof(int));

您的算法似乎也有点矫枉过正。为什么不直接从 NSData 对象中复制整个数组,然后就地字节交换条目呢?像这样的东西:

int32_t length = [Img length];
int32_t *array = malloc(length);

[Img getBytes:array length:length];

for (i = 0; i < length/sizeof(int32_t); i++)
{
    array[i] = NSSwapBigIntToHost(array[i]);
}

Returning a pointer to a local variable is a very bad idea. array could get overwritten at any time (or if you were to write through the pointer, you could corrupt the stack). You probably want something like:

// CREATE THE ARRAY
int *array = malloc(Nelements * sizeof(int));

Your algorithm seems a bit overkill, too. Why not just copy out the whole array from the NSData object, and then byteswap the entries in place? Something like:

int32_t length = [Img length];
int32_t *array = malloc(length);

[Img getBytes:array length:length];

for (i = 0; i < length/sizeof(int32_t); i++)
{
    array[i] = NSSwapBigIntToHost(array[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文