简体   繁体   English

将BYTE数组转换为unsigned long long int

[英]Convert BYTE array into unsigned long long int

I'm trying to convert a BYTE array into an equivalent unsigned long long int value but my coding is not working as expected. 我正在尝试将BYTE数组转换为等效的无符号long long int值,但我的编码没有按预期工作。 Please help with fixing it or suggest an alternative method for the same. 请帮助修复它或建议替代方法。

Extra Information: These 4 bytes are combined as a hexadecimal number and an equivalent decimal number is an output. 附加信息:这4个字节组合为十六进制数,等效十进制数是输出。 Say for a Given byteArray= {0x00, 0xa8, 0x4f, 0x00}, Hexadecimal number is 00a84f00 and it's equivalent decimal number is 11030272. 假设给定byteArray = {0x00,0xa8,0x4f,0x00},十六进制数是00a84f00,它的等效十进制数是11030272。

#include <iostream>
#include <string>

typedef unsigned char BYTE;

int main(int argc, char *argv[])
{
  BYTE byteArray[4] = { 0x00, 0x08, 0x00, 0x00 };
  std::string str(reinterpret_cast<char*>(&byteArray[0]), 4);
  std::cout << str << std::endl;

  unsigned long long ull = std::strtoull(str.c_str(), NULL, 0);
  printf ("The decimal equivalents are: %llu", ull);


  return EXIT_SUCCESS;
}

I'm getting the following output: 我得到以下输出:

The decimal equivalents are: 0

While the expected output was: 虽然预期产量是:

The decimal equivalents are: 2048

When you call std::strtoull(str.c_str(), NULL, 0); 当你调用std::strtoull(str.c_str(), NULL, 0); , its first argument supplied is equivalent to an empty string , as string is essentially a null-terminated sequence of characters. ,它提供的第一个参数相当于一个空字符串 ,因为string本质上是一个以null结尾的字符序列。

Second, std::strtoull() does not convert with byte sequences, it converts with the literal meaning of strings. 其次, std::strtoull()不会使用字节序列进行转换,而是使用字符串的字面含义进行转换。 ie you'll get 2048 with std::strtoull("2048", NULL, 10) . 即你将获得2048std::strtoull("2048", NULL, 10)

Another thing to note is that unsigned long long is a 64-bit data type, whereas your byte array only provides 32 bits. 另一点需要注意的是, unsigned long long是64位数据类型,而您的字节数组只提供32位。 You need to fill the other 32 bytes with zero to get the correct result. 您需要用零填充其他32个字节以获得正确的结果。 I use a direct assignment, but you could also use std::memset() here. 我使用直接赋值,但你也可以在这里使用std::memset()

What you want to do is: 你想要做的是:

ull = 0ULL;
std::memcpy(&ull, byteArray, 4);

Given your platform has little-endian , the result should be 2048 . 鉴于您的平台有little-endian ,结果应该是2048

What you first must remember is that a string, is really a null-terminated string. 你首先必须记住的是一个字符串,实际上是一个以空字符结尾的字符串。 Secondly, a string is a string of characters , which is not what you have. 其次,字符串是一串字符 ,而不是你拥有的字符串。 The third problem is that you have an array of four bytes, which corresponds to an unsigned 32-bit integer, and you want an (at least) 64-bit types which is 8 bytes. 第三个问题是你有一个四个字节的数组,它对应一个无符号的32位整数,你想要一个(至少)64位的类型,它是8个字节。

You can solve all these problems with a temporary variable, a simple call to std::memcpy , and an assignment: 您可以使用临时变量,对std::memcpy的简单调用以及赋值来解决所有这些问题:

uint32_t temp;
std::memcpy(&temp, byteArray, 4);
ull = temp;

Of course, this assumes that the endianness is correct. 当然,这假设字节顺序是正确的。


Note that I use std::memcpy instead of std::copy (or std::copy_n ) because std::memcpy is explicitly mentioned to be able to bypass strict aliasing this way, while I don't think the std::copy functions are. 请注意,我使用std::memcpy而不是std::copy (或std::copy_n ),因为明确提到std::memcpy能够以这种方式绕过严格别名,而我不认为std::copy功能是。 Also the std::copy functions are more for copying elements and not anonymous bytes (even if they can do that too, but with a clunkier syntax). std::copy函数更多的是复制元素而不是匿名字节(即使它们也可以这样做,但使用笨拙的语法)。

Given the answers are using std::memcpy , I want to point out that there's a more idiomatic way of doing this operation: 鉴于答案是使用std::memcpy ,我想指出有一种更惯用的方法来执行此操作:

char byteArray[] = { 0x00, 0x08, 0x00, 0x00 };
uint32_t cp;
std::copy(byteArray, byteArray + sizeof(cp), reinterpret_cast<char*>(&cp));

std::copy is similar to std::memcpy , but is the C++ way of doing it. std::copy类似于std::memcpy ,但它是C ++的做法。

Note that you need to cast the address of the output variable cp to one of: char * , unsigned char * , signed char * , or std::byte * , because otherwise the operation wouldn't be byte oriented. 请注意,您需要将输出变量cp的地址cp为以下值之一: char *unsigned char *signed char *std::byte * ,否则操作将不是面向字节的。

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

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