1

How do I break a long Hexadecimal Value in Coffeescript so that it spans multiple lines?

authKey = 0xe6b86ae8bdf696009c90e0e650a92c63d52a4b3232cca36e0ff2f5911e93bd0067df904dc21ba87d29c32bf17dc88da3cc20ba65c6c63f21eaab5bdb29036b83

to something like

authKey = 0xe6b86ae8bdf696009c90e0e650a92c63d52a4b323\
          2cca36e0ff2f5911e93bd0067df904dc21ba87d29c3\
          2bf17dc88da3cc20ba65c6c63f21eaab5bdb29036b83

Using \ results in an Unexpected 'NUMBER' Error, using line break in an Unexpected 'INDENT' Error

4
  • Why don't you store this in a string or a byte array?
    – Ray Toal
    Apr 5, 2013 at 9:11
  • True, that would work. I just want to know if it can be done in this notation.
    – dmb
    Apr 5, 2013 at 9:16
  • Good point, thanks for explaining in your answer.
    – dmb
    Apr 5, 2013 at 9:22
  • 1
    Good question; I looked for the lexical syntax here but didn't find anything precise. The backslash at the end of line will give a space, though (try this inside a string literal) so your attempted assignment parses as authKey = 123 456 which explains the "Unexpected NUMBER" error.
    – Ray Toal
    Apr 5, 2013 at 9:27

2 Answers 2

1

There's actually no point in doing this in CoffeeScript because numbers are stored as 64-bit IEEE 754 values and you have too many bits of precision for the value to be stored as a number.

If you write

authKey = 0xe6b86ae8bdf696009c90e0e650a92c63d52a4b3232cca36e0ff2f5911e93bd0067df904dc21ba87d29c32bf17dc88da3cc20ba65c6c63f21eaab5bdb29036b83
console.log(authKey)

then the value logged is

1.2083806867379407e+154

You want to store your authKey as a string or byte array, both of which are trivial to write across multiple lines.

0

Like others have said, this doesn't really make a whole lot of sense to be stored in a number, as opposed to a string; however, I decided to throw something together to allow it anyway:

stringToNumber = ( str ) -> parseInt( str.replace( /\n/g, '' ) )

authKey = stringToNumber """
  0xe6b86ae8bdf696009c90e0e650a92c63d52a4b323
  2cca36e0ff2f5911e93bd0067df904dc21ba87d29c3
  2bf17dc88da3cc20ba65c6c63f21eaab5bdb29036b83
"""

Like Ray said, this will just result in:

1.2083806867379407e+154

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.