SymmetricsWeb
SymmetricsWeb

Reputation: 636

Javascript Escape (\x) + Variable,

Been playing with Javascript and discover the escaped(\x) which seems interesting.

To clarify I just discover I can convert two digit number into a letter using escape + x,

like "\x51" output is Q

Been searching about this specific topic but not really sure where to start as I don't even know what they call it, like I know backslash purpose in PHP and Javascript but I don't know what it does when it combines with x

Now my question is, is it possible to add a dynamic number value after the X?

Like I'm trying to create a function like this but It doesn't seem possible, as it looks like it requires to have two characters (number or letter) after x.

function __num_string( num ) {
    return "\x"+num;
}

expectation __num_string( 51 ) would return Q

reality Uncaught SyntaxError: Unexpected token ILLEGAL

I would appreciate any clarification if its possible or not,

Thanks

Upvotes: 1

Views: 2810

Answers (3)

Alex R.
Alex R.

Reputation: 1

tldr

  • Pass 51₁₆ to String.fromCharCode not 51₁₀
  • 51₁₆ is equal to 81₁₀
  • Any of the following can be used for conversion
    • (81).toString(16)
    • 81..toString(16)
    • 81.0.toString(16)
    • numberVar.toString(16)

After reading the answers and comments in this question an investigation ensued.

Bearing in mind a hypothesis I'd formed from the question

Why do the main solutions, namely String.fromCharCode, seem not to answer the asker's question when tried.

The hypothesis was that the issue lay within the type of argument passed to String.fromCharCode.

The following demonstrates in a node repl the reason why '\x51' === 'Q' returns true yet String.fromCharCode(51) === 3 also returns true.

> 0x51 // es6 hexadecimal literal
81

> 0x51.toString(16) // Same as (81).toString(16)
'51'

> String.fromCharCode(0x51) // Same as String.fromCharCode(81)
'Q'

> (81).toString(16) // Same as 0x51.toString(16)
51

> String.fromCharCode(81) // Same as String.fromCharCode(0x51)
'Q'

> 0x33 // es6 hexadecimal literal
51

> 0x33.toString(16) // literal to base 16 string
'33'

> String.fromCharCode(0x33)
'3'

> String.fromCharCode(51)
'3'

Just the statements if you wanna try them yourself.

0x51
0x51.toString(16)
String.fromCharCode(0x51)
(81).toString(16)
String.fromCharCode(81)
0x33
0x33.toString(16)
String.fromCharCode(0x33)
String.fromCharCode(51)

It seems as if the method in question takes a hexadecimal string.

If it's a number it's toString() with 16 passed as base is called implicitly.

Below is a function which does the conversion.

function toHex(n) {
  if (typeof n === 'string') return `${parseInt(n, 16)}`;
  return `0x${n}`;
}

It can be used with a number

> String.fromCharCode(toHex(33))
'3'


> String.fromCharCode(toHex(51))
'Q'

It can be used with a string

> String.fromCharCode(toHex('33'))
'3'


> String.fromCharCode(toHex('51'))
'Q'

It can be used with hex digits but only as a string...

> String.fromCharCode(toHex('3f'))
'?'

because hex numeric literals are fine without it.

> String.fromCharCode(0x3f)
'?'

Upvotes: 0

raina77ow
raina77ow

Reputation: 106385

Yes, it's possible, but there's a better way to do it:

return String.fromCharCode(num);

... where num is a Unicode value of a character you want to get.

Still, technically it's possible to implement your function the way you originally intended, using eval:

function __num_string( num ) {
   if (/^[a-f0-9]{1,2}$/i.test(num)) { 
     return eval('"\\x' + num + '"');
   }
}

Note the difference: num should be a string representing a hexadecimal value.


Now about the error you got: this...

'\x'

... is technically invalid string. When parsing string literal, the parser expects the sequence \x to be followed by [0-9a-f] character range. Yet in your case there's nothing similar there, hence Unexpected token ILLEGAL.

Upvotes: 3

SLaks
SLaks

Reputation: 887415

String escaping is a syntactic feature, part of string literals.
It doesn't make sense to do that at runtime.

You're looking for String.fromCharCode, which does exactly what it sounds like.

Upvotes: 1

Related Questions