简体   繁体   中英

How to do you insert new line character ie “↵” as a character (ASCII VALUE 10) in a javascript string without breaking it into a new line?

I am writing a string into a file that uses '↵' character explicity. However upon writing into a file the file reads it as an indicator to begin a new line how do i tackle this?

I am making a huffman encoder. the format of the encoded text is- Line 1- huffman tree in a preorder format eg- "00^a1^b10^↵^1c where ^ indicates the leaf node. Line 2- contains an integer which is essentially padding to be removed in the decoding phase Line 3- is the actual text to be decoded.

Now the issue i am facing is that is the decoding phase, when i am splitting the entire text using new line to get tree, padding and actual string to be decoded, i should get 3 things separately. However when splitting with('\n') my stringfied representation of the tree is also getting split making parsing impossible.

An example 1)String to be encoded-

Hello. This is a trial text. Hello World!

2)Binary Mapping of each char is string "↵": "0000" " ": "100":. "00010":: "0100" H: "0110" T: "00110" W: "01010" a: "11111" d: "01011" e: "1011" h: "00111" i: "1010" l: "110" o: "1110" r: "0010" s: "11110" t: "0111" x: "00011" proto : Object

3) Encoded text-

0000^ 10^.1^x10^r10^T1^h100^,10^W1^d10^H1^t100^ 10^i1^e10^l10^o10^s1^a 5 kÛzô¯OÇ*þct½º+,@

notice the line break after "0000^" if it can be "0000^↵" then my problem would be solved

I'm afraid that "\n" has the ASCII value of 10.

Therefore your string actually looks like this:

00^a1^b10^(ASCII_VALUE_10)^1c(ASCII_VALUE_10)(INTEGER)(ASCII_VALUE_10)Text to be encoded

Or more conventionally it is usually displayed as the following as you have discovered:

00^a1^b10^
^1c
(INTEGER)
Text to be encoded

You want to use ASCII VALUE 10 as both a value in the first line and the line delimiter ("\n"). This is a contradiction in logic as well as a contradiction in the English language.

But all is not lost. What you have created is just a data format encoded in 4 lines instead of 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM