在C ++中将int转换为字节数组

时间:2022-07-17 11:49:45

I am trying to use implement the LSB lookup method suggested by Andrew Grant in an answer to this question: Position of least significant bit that is set

我试图使用实现安德鲁·格兰特建议的LSB查找方法来回答这个问题:设置的最低有效位的位置

However, it's resulting in a segmentation fault. Here is a small program demonstrating the problem:

但是,它会导致分段错误。这是一个展示问题的小程序:

#include <iostream>

typedef unsigned char Byte;

int main()  
{  
    int value = 300;  
    Byte* byteArray = (Byte*)value;  
    if (byteArray[0] > 0)  
    {  
        std::cout<< "This line is never reached. Trying to access the array index results in a seg-fault." << std::endl;  
    }  
    return 0;  
}  

What am I doing wrong?
I've read that it's not good practice to use 'C-Style' casts in C++. Should I use reinterpret_cast<Byte*>(value) instead? This still results in a segmentation fault, though.

我究竟做错了什么?我已经读过在C ++中使用'C-Style'强制转换是不好的做法。我应该使用reinterpret_cast (value)吗?但是,这仍会导致分段错误。

5 个解决方案

#1


11  

Use this:

(Byte*) &value;

You don't want a pointer to address 300, you want a pointer to where 300 is stored. So, you use the address-of operator & to get the address of value.

你不希望指向地址300的指针,你想要一个指向存储300的指针。因此,您使用address-of运算符&来获取值的地址。

#2


6  

While Erik answered your overall question, as a followup I would say emphatically -- yes, reinterpret_cast should be used rather than a C-style cast.

虽然Erik回答了你的整体问题,但作为后续内容我会强调说 - 是的,应该使用reinterpret_cast而不是C风格的演员。

Byte* byteArray = reinterpret_cast<Byte*>(&value);

#3


1  

The line should be: Byte* byteArray = (Byte*)&value;

该行应为:Byte * byteArray =(Byte *)&value;

You should not have to put the (void *) in front of it.

你不应该把(void *)放在它前面。

-Chert

#4


0  

char *array=(char*)(void*)&value;

Basically you take a pointer to the beginning of the string and recast it to a pointer to a byte.

基本上,您将指针指向字符串的开头并将其重新转换为指向字节的指针。

#5


0  

@Erik already fixed your primary problem, but there is a subtle one that you still have. If you are only looking for the least significant bit, there is no need to bother with the cast at all.

@Erik已经解决了你的主要问题,但你仍然有一个微妙的问题。如果您只是寻找最不重要的位,则根本不需要为演员而烦恼。

int main()
{
    int value = 300;       
    if (value & 0x00000001)       
    {           
        std::cout<< "LSB is set" << std::endl;
    }
    return 0;
}

#1


11  

Use this:

(Byte*) &value;

You don't want a pointer to address 300, you want a pointer to where 300 is stored. So, you use the address-of operator & to get the address of value.

你不希望指向地址300的指针,你想要一个指向存储300的指针。因此,您使用address-of运算符&来获取值的地址。

#2


6  

While Erik answered your overall question, as a followup I would say emphatically -- yes, reinterpret_cast should be used rather than a C-style cast.

虽然Erik回答了你的整体问题,但作为后续内容我会强调说 - 是的,应该使用reinterpret_cast而不是C风格的演员。

Byte* byteArray = reinterpret_cast<Byte*>(&value);

#3


1  

The line should be: Byte* byteArray = (Byte*)&value;

该行应为:Byte * byteArray =(Byte *)&value;

You should not have to put the (void *) in front of it.

你不应该把(void *)放在它前面。

-Chert

#4


0  

char *array=(char*)(void*)&value;

Basically you take a pointer to the beginning of the string and recast it to a pointer to a byte.

基本上,您将指针指向字符串的开头并将其重新转换为指向字节的指针。

#5


0  

@Erik already fixed your primary problem, but there is a subtle one that you still have. If you are only looking for the least significant bit, there is no need to bother with the cast at all.

@Erik已经解决了你的主要问题,但你仍然有一个微妙的问题。如果您只是寻找最不重要的位,则根本不需要为演员而烦恼。

int main()
{
    int value = 300;       
    if (value & 0x00000001)       
    {           
        std::cout<< "LSB is set" << std::endl;
    }
    return 0;
}