6

In Javascript I can type '\u00A3' to get a character using its char code. I can do this programatically to with String.fromCharCode(parseInt('00A3', 16)).

But I can't find a way to do the same for a control character. I can type them in my source code but I want a way to generate them in code.

3
  • 1
    ?? But "control" characters are just characters; if you know the numeric value for the character, you use "fromCharCode()" in exactly the same way. Commented Dec 5, 2011 at 14:43
  • I know the numeric value for \u00A3, but not for Ctrl + B. I have to look it up. I was wondering if there was a way Javascript could look that up for me. Commented Dec 5, 2011 at 14:52
  • 1
    Control characters "Control A" through "Control Z" are just characters 1 through 26 (decimal); in other words, Ctrl-A is 0x0001, Ctrl-B is 0x0002, etc. Thus, you could find the code for the letter ("A") and subtract 64 from its code value. Commented Dec 5, 2011 at 14:56

2 Answers 2

9

Sounds to me like you could just use this list: http://en.wikipedia.org/wiki/C0_and_C1_control_codes and use the character points defined there to insert them with \u or String.fromCharCode as in your example?

PS: instead of the parseInt, you could use a literal: 0x00A3

Sign up to request clarification or add additional context in comments.

Comments

3

You can easily embed octal numbers:

var crlf = '\013' + '\012'; // octal numbers
alert('hello' + crlf + 'there'); // shows hello\n\rthere

Doesn't work the same for hex, though:

var clrf = '\0xD' + '\0xA'; // hex
alert('hello' + crlf + 'there'); // shows helloxDxAthere

1 Comment

Octal escape sequences are not standard and disallowed in "strict" mode. Hex escape sequences look like \0x000D, with four-digit hex values.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.