c++ - How to define 0x0001 (and such) in Javascript? - Stack Overflow

I'm looking at the source code of a C++ project similar to one that I am doing in Javascript for r

I'm looking at the source code of a C++ project similar to one that I am doing in Javascript for reference.

In C++, I have

#define FIRST_THING 0x0001
#define SECOND_THING 0x0002
...

What do these values mean? And how would I define this in Javascript? Things break if I try to just use 0x0001 and such, so could I just do

var FIRST_THING = 1
var SECOND_THING = 2

or is that pletely different?

I'm looking at the source code of a C++ project similar to one that I am doing in Javascript for reference.

In C++, I have

#define FIRST_THING 0x0001
#define SECOND_THING 0x0002
...

What do these values mean? And how would I define this in Javascript? Things break if I try to just use 0x0001 and such, so could I just do

var FIRST_THING = 1
var SECOND_THING = 2

or is that pletely different?

Share Improve this question asked Feb 1, 2012 at 19:33 KalinaKalina 5,59418 gold badges67 silver badges104 bronze badges 11
  • 1 var FIRST_THING = 0x000001; works fine – qwertymk Commented Feb 1, 2012 at 19:34
  • 1 There is nothing similar between your C++ project and your Javascript project. Ever. – Lightness Races in Orbit Commented Feb 1, 2012 at 19:36
  • Yes. You say "thinks break", but what things? Care to give an example? – Mr Lister Commented Feb 1, 2012 at 19:36
  • Define "things break" because, well, they don't. – Lightness Races in Orbit Commented Feb 1, 2012 at 19:36
  • 1 upvoted as to not discourage the OP. This is obviously a beginner, she is confused, and the question is valid (if perhaps trivial to most of us, but we all start somewhere). I realize it's a bit unclear, but again, she's confused. – Ed Swangren Commented Feb 1, 2012 at 19:45
 |  Show 6 more ments

3 Answers 3

Reset to default 5

0x0001 is an integral constant in base 16, i.e., hexadecimal. It is still 1 in base 10. So yes, your example is equivalent, but do you know how to mentally parse 0xBC? If not then you need to study up on arbitrary base arithmetic or at least get fortable with hex as any programmer should know this stuff.

Sometimes it is easier to view numbers in hex form as they represent bit patterns. In hex, two digits correspond to a byte, so you know at a glance that 0xFF is 255 base 10 and 11111111 base 2. Work on some lower level projects for a while and it will bee second nature.

In your C++ example the integral constants are textually replaced by the preprocessor (i.e., all occurrences of FIRST_THING are replaced by 0x0001 before the code is piled), you don't have such a tool in javascript, so just assign the values to variables directly.

You cannot create 'constants' in javascript, so it's up to you to make sure that you don't change them. However, you can simply write

firstThing = 0x0001;

And it will work just as the C++ example does, i.e., firstThing takes on the value of 1.

In the C++, those are preprocessor constants in hexadecimal.

In javascript, there is no preprocessor.

have a look at Are there constants in JavaScript?

There aren't any constants, but convention can help.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745484313a4629697.html

相关推荐

  • c++ - How to define 0x0001 (and such) in Javascript? - Stack Overflow

    I'm looking at the source code of a C++ project similar to one that I am doing in Javascript for r

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信