我如何把一个无符号的字符数组为unsigned long long? [英] how do i convert an unsigned char array into an unsigned long long?

查看:828
本文介绍了我如何把一个无符号的字符数组为unsigned long long?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我有8个无符号的字符,我要转换为unsigned long long。

let's say that i have 8 unsigned char, that i want to convert to an unsigned long long.

例如,如果所有字符都等于为0xFF,无符号长长将等于至0xFFFFFFFFFFFFFFFF。

for example, if all char are equals to 0xFF, the unsigned long long would be equal to 0xFFFFFFFFFFFFFFFF.

什么是最有效的方式来做到这一点与C或C ++?

what's the most efficient way to do that with C or C++?

推荐答案

的memcpy的相反,您可以直接分配位

Instead of memcpy, you can directly assign the bits

  unsigned char buffer[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  unsigned long long l = 0;
  for (int i = 0; i < 8; ++i) {
    l = l | ((unsigned long long)buffer[i] << (8 * i));
  }

我相信这是免疫字节序。

I believe this is immune to endianness.

这篇关于我如何把一个无符号的字符数组为unsigned long long?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆