将整数转换为位

发布于 2024-11-07 21:36:10 字数 390 浏览 4 评论 0原文

我有字节到二进制字符串的函数,

std::string byte_to_binary(unsigned char byte)
{
    int x = 128;
    std::ostringstream oss;
    oss << ((byte & 255) != 0);

    for (int i = 0; i < 7; i++, x/=2)
       oss << ((byte & x) != 0);

    return oss.str();
}

如何以相同的方式将 int 写入位?我不想在二进制字符串的开头有额外的 0,所以这就是为什么我无法弄清楚如何每次创建一个可变长度。 另外,我没有使用 std::bitset。

I have byte to binary string function,

std::string byte_to_binary(unsigned char byte)
{
    int x = 128;
    std::ostringstream oss;
    oss << ((byte & 255) != 0);

    for (int i = 0; i < 7; i++, x/=2)
       oss << ((byte & x) != 0);

    return oss.str();
}

How can i write an int to bits in same way? I don't want extra 0's at the beginning of binary string so that is why i can't figure out how to create a variable length each time.
Also, i'm not using std::bitset.

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

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

发布评论

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

评论(4

时光与爱终年不遇 2024-11-14 21:36:10

我将将此作为答案发布。它更短、更安全,而且最重要的是,它完成了

#include <string>
#include <bitset>
#include <type_traits>

// SFINAE for safety. Sue me for putting it in a macro for brevity on the function
#define IS_INTEGRAL(T) typename std::enable_if< std::is_integral<T>::value >::type* = 0

template<class T>
std::string integral_to_binary_string(T byte, IS_INTEGRAL(T))
{
    std::bitset<sizeof(T) * CHAR_BIT> bs(byte);
    return bs.to_string();
}

int main(){
    unsigned char byte = 0x03; // 0000 0011
    std::cout << integral_to_binary_string(byte);
    std::cin.get();
}

输出:

00000011

更改了函数名称,尽管我对此不满意......有人有一个好主意吗?

I'll just post this as an answer. It is shorter, safer and, what's most important, it is done.

#include <string>
#include <bitset>
#include <type_traits>

// SFINAE for safety. Sue me for putting it in a macro for brevity on the function
#define IS_INTEGRAL(T) typename std::enable_if< std::is_integral<T>::value >::type* = 0

template<class T>
std::string integral_to_binary_string(T byte, IS_INTEGRAL(T))
{
    std::bitset<sizeof(T) * CHAR_BIT> bs(byte);
    return bs.to_string();
}

int main(){
    unsigned char byte = 0x03; // 0000 0011
    std::cout << integral_to_binary_string(byte);
    std::cin.get();
}

Output:

00000011

Changed function name, though I'm not happy with that one... anyone got a nice idea?

另类 2024-11-14 21:36:10

像这样的东西应该可以工作(尽管我很快就破解了它并且还没有测试):

#include <string>
#include <climits>

template<typename T>
std::string to_binary(T val)
{
  std::size_t sz = sizeof(val)*CHAR_BIT;
  std::string ret(sz, ' ');
  while( sz-- )
  {
    ret[sz] = '0'+(val&1);
    val >>= 1;
  }
  return ret;
}

Something like this should work (though I hacked it up quickly and haven't tested):

#include <string>
#include <climits>

template<typename T>
std::string to_binary(T val)
{
  std::size_t sz = sizeof(val)*CHAR_BIT;
  std::string ret(sz, ' ');
  while( sz-- )
  {
    ret[sz] = '0'+(val&1);
    val >>= 1;
  }
  return ret;
}
胡大本事 2024-11-14 21:36:10

您可以使用 std:bitset 并将任何数字转换为任何大小的位字符串,例如 64

#include <string>
#include <iostream>
#include <bitset>
using namespace std;

int main() {

 std::bitset<64> b(836); //convent number into bit array
 std::cout << "836 in binary is " <<  b << std::endl;

 //make it string
 string mystring = b.to_string<char,char_traits<char>,allocator<char> >();
 std::cout << "binary as string " << mystring << endl;
}

You can do it using std:bitset and convert any number into bit string of any size, for example 64

#include <string>
#include <iostream>
#include <bitset>
using namespace std;

int main() {

 std::bitset<64> b(836); //convent number into bit array
 std::cout << "836 in binary is " <<  b << std::endl;

 //make it string
 string mystring = b.to_string<char,char_traits<char>,allocator<char> >();
 std::cout << "binary as string " << mystring << endl;
}
秋风の叶未落 2024-11-14 21:36:10

既然您在评论中提到了对 C 风格的希望,您可以考虑使用 itoa (或 _itoa)如果您不担心 ANSI-C 标准。许多编译器在 stdlib.h 中支持它。它还删除前导 0:

unsigned char yourGoldenNumber = 42;
char binCode[64];
itoa(yourGoldenNumber,binCode,2); // third parameter is the radix
puts(binCode); // 101010

Since you mentioned your wish for C style in the comments, you might consider using itoa (or _itoa) if you are not worried about ANSI-C standard. Many compilers support it in stdlib.h. It also strips the leading 0's:

unsigned char yourGoldenNumber = 42;
char binCode[64];
itoa(yourGoldenNumber,binCode,2); // third parameter is the radix
puts(binCode); // 101010
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文