二进制常数 [英] Binary constants

查看:182
本文介绍了二进制常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



硬编码到C / C ++程序二进制文件的最佳方法是什么?
值/常量/等等。从那以后:


int a;

a = 0b0111;


错了,我应该怎么做。我想过将它们首先转换为
十六进制或十进制,但是如果你有一个很长的字符串如下:


01010000101011101101011101011010 ......(64位数字) ,例如

__int64)


有办法解决这个问题吗?是的,我在某个地方看到了一个非常好的b $ b b智能宏,让0b01010 ...但它仅限于短串。

谢谢,

Valerij

Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it. I thought about converting them first to
hexadecimal or decimal, but what if you have a long string such as:

01010000101011101101011101011010 ... (64 digits long, for example for
__int64)

is there a way to handle this? And yes I have seen somewhere a very
smart macro that lets 0b01010 ... but it is limited to short strings.
Thanks,
Valerij

推荐答案



valerij napsal:

valerij napsal:



硬编码到C / C ++程序二进制文件的最佳方法是什么?
值/常量/等等。从那以后:


int a;

a = 0b0111;


错了,我应该怎么做。我想过将它们首先转换为
十六进制或十进制,但是如果你有一个很长的字符串如下:


01010000101011101101011101011010 ......(64位数字) ,例如

__int64)


有办法解决这个问题吗?是的,我在某个地方看到了一个非常好的b $ b b智能宏,让0b01010 ...但它仅限于短串。

谢谢,

Valerij
Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it. I thought about converting them first to
hexadecimal or decimal, but what if you have a long string such as:

01010000101011101101011101011010 ... (64 digits long, for example for
__int64)

is there a way to handle this? And yes I have seen somewhere a very
smart macro that lets 0b01010 ... but it is limited to short strings.
Thanks,
Valerij



我(个人)会使用十六进制模式。我不认为可能有任何好的

解决方案,不称为外部功能。 16(+ 2)

字符对于64位数字不是太多,我认为没问题

(它优于64位二进制表示)。

I (personally) would use hexadecimal mode. I do not think there may be
any "nice" solution, which doesn''t call external function. 16 (+ 2)
characters for 64-bit number is not too much, I think it is no problem
(it is better than 64-bit binary representation).


valerij napsal:
valerij napsal:



什么是硬编码到C / C ++程序二进制文件的最佳方法

值/常量/等。从那以后:


int a;

a = 0b0111;


错了,我应该怎么做。
Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it.



这是因为从0开始的任何常量都是八进制。 (它b / b
可能让人们知道,在一个简单的表达式中,例如

i = 0,0是八进制而不是十进制。而在i = 1,

1是十进制而不是八进制。)


您可以选择八进制,十进制和十六进制。

可能大多数系统程序员使用十六进制。我同意

这里有二进制文件有用,这就是为什么

它是一些其他语言的选项。


史蒂夫

That''s because any constant starting with 0 is octal. (It
may interest people to know that in a simple expression like
i = 0, the "0" is octal and not decimal. Whereas in i = 1,
the "1" is decimal and not octal.)

You have your choice of octal, decimal, and hexadecimal.
Probably most systems programmers use hexadecimal. I agree
there''s ocassions where binary would be useful, which is why
it is an option in some other languages.

Steve




valerij写道:

valerij wrote:



硬编码到C / C ++程序二进制文件的最佳方法是什么?
值/常量/等。从那以后:


int a;

a = 0b0111;


错了,我应该怎么做。我想过将它们首先转换为
十六进制或十进制,但是如果你有一个很长的字符串如下:


01010000101011101101011101011010 ......(64位数字) ,例如

__int64)


有办法解决这个问题吗?是的,我在某个地方看到了一个非常好的b $ b b智能宏,让0b01010 ...但它仅限于短串。

谢谢,

Valerij
Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it. I thought about converting them first to
hexadecimal or decimal, but what if you have a long string such as:

01010000101011101101011101011010 ... (64 digits long, for example for
__int64)

is there a way to handle this? And yes I have seen somewhere a very
smart macro that lets 0b01010 ... but it is limited to short strings.
Thanks,
Valerij



看看std :: bitset:
http://www.sgi.com/tech/stl/bitset.html


#include< ; iostream>

#include< ostream>

#include< bitset>


int main(){

std :: bitset< 64 bset(0xff);

std :: cout<< bset<< std :: endl;

}

/ *

00000000000000000000000000000000000000000000000000 00000011111111

* /


注意:std :: bitset< 64是*不是与std :: bitset相同的类型< 32>

或除64以外的任何其他尺寸。

它不是真正的STL容器,但功能强大,因为它可以很容易地转换成
到一个字符串,并提供有用的运算符。其

输入/输出也可以流式传输。

Take a look at std::bitset:
http://www.sgi.com/tech/stl/bitset.html

#include <iostream>
#include <ostream>
#include <bitset>

int main() {
std::bitset< 64 bset(0xff);
std::cout << bset << std::endl;
}
/*
00000000000000000000000000000000000000000000000000 00000011111111
*/

Note: a std::bitset< 64 is *not* the same type as std::bitset< 32 >
or any other size except 64.
Its not a true STL container but quite powerful in that it can easily
be converted to a string and has useful operators available. Its
input/output streameable too.


这篇关于二进制常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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