C中64位十六进制到十进制

时间:2015-08-17 16:03:15

标签: c hex decimal converter wireshark

我正在从Wireshark读取一个pcap文件,并使用u_char以8位存储数据。一部分数据是64位,所以我需要将8个8位数组合成/读取为一个64位数,然后将其转换为十进制数。 例如,这就是我目前所拥有的

long long int a;
a = (data[j] << 56) | (data[j+1] << 48) | (data[j+2] << 40) | (data[j+3] << 32) | (data[j+4] << 24) | (data[j+5] << 16) | (data[j+6] << 8) | data[j+7];
printf("%lld", a);

output: -1073815553

这就是我现在想要的,

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  PhotoCell *cell = (PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    UIImage *image = [UIImage imageWithContentsOfFile:fileName];
    cell.photoView.image = image;
    return cell;
}

要合并8个1字节,我尝试过:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  PhotoCell *cell = (PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [self setImageAsync:indexPath fileName:myTrumbImageFileName collectionView:collectionView];
    return cell;
}

-(void) setImageAsync:(NSIndexPath*)indexPath fileName:(NSString*) fileName collectionView:(UICollectionView*)collectionView
{
  dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
    UIImage *image = [UIImage imageWithContentsOfFile:fileName];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIImage *smallImage = [image resizedImageToFitInSize:CGSizeMake(screenRect.size.width*2, screenRect.size.height*2) scaleIfSmaller:YES];

    dispatch_async(dispatch_get_main_queue(), ^{ // 1
      PhotoCell *cell = (PhotoCell*)[collectionView cellForItemAtIndexPath:indexPath];
      if(cell)
      {
        cell.photoView.image = smallImage;
        [[cell photoView] setAlpha:0.0];
        [UIView transitionWithView:[cell photoView] duration:0.35 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [[cell photoView] setAlpha:1.0]; } completion:nil];
      }
    });        
  });
}

非常感谢帮助。感谢。

1 个答案:

答案 0 :(得分:2)

看起来OP希望用binary64 double覆盖8字节整数。

这样的代码可以移植到许多机器上,但肯定不能移植到所有C机器上,因为它假设很多东西,例如double的形式。

int main(void) {
  union {
    double d;
    unsigned long long ull;
  } u;
  u.d = 0.589134;
  printf("%016llX\n", u.ull);
  u.ull = 0x3fe2da2f8bdec5f4;
  printf("%lf\n", u.d);
  return 0;
}

3FE2DA2F8BDEC5F4
0.589134