Last active
April 17, 2025 16:41
-
-
Save milnak/111bd78fa95a298496f8f285f97f5996 to your computer and use it in GitHub Desktop.
Convert text into C-like literal, escaping newlines, tab, double quotes, backslash.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
<html> | |
<head> | |
<meta name="keywords" content="C++ escaping, C++ special characters, C++ text escaping"> | |
<meta name="author" content="Tomasz Ostrowski"> | |
<meta name="description" content="Text -> C/C++ string converter (special characters escaping)."> | |
<title>Text -> C/C++ string converter</title> | |
<style> | |
body { | |
font-family: Arial, sans-serif; | |
background-color: #f4f4f4; | |
color: #333; | |
margin: 0; | |
padding: 0; | |
} | |
#main { | |
max-width: 800px; | |
margin: 20px auto; | |
padding: 20px; | |
background-color: #fff; | |
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | |
border-radius: 8px; | |
} | |
h1 { | |
color: #4CAF50; | |
text-align: center; | |
} | |
p { | |
font-size: 16px; | |
line-height: 1.6; | |
} | |
textarea { | |
width: 100%; | |
padding: 10px; | |
border: 1px solid #ccc; | |
border-radius: 4px; | |
font-family: monospace; | |
font-size: 14px; | |
} | |
button { | |
background-color: #4CAF50; | |
color: white; | |
padding: 10px 20px; | |
border: none; | |
border-radius: 4px; | |
cursor: pointer; | |
font-size: 16px; | |
} | |
button:hover { | |
background-color: #45a049; | |
} | |
input[type="checkbox"] { | |
margin-right: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
function insertAtCursor(myField, myValue) { | |
//IE support | |
if (document.selection) { | |
myField.focus(); | |
sel = document.selection.createRange(); | |
sel.text = myValue; | |
} | |
//MOZILLA/NETSCAPE support | |
else if (myField.selectionStart || myField.selectionStart == '0') { | |
var startPos = myField.selectionStart; | |
var endPos = myField.selectionEnd; | |
restoreTop = myField.scrollTop; | |
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); | |
myField.selectionStart = startPos + myValue.length; | |
myField.selectionEnd = startPos + myValue.length; | |
if (restoreTop > 0) { | |
myField.scrollTop = restoreTop; | |
} | |
} | |
else { | |
myField.value += myValue; | |
} | |
} | |
function interceptTabs(evt, control) { | |
key = evt.keyCode ? evt.keyCode : evt.which ? evt.which : evt.charCode; | |
if (key == 9) { | |
insertAtCursor(control, '\t'); | |
return false; | |
} | |
else { | |
return key; | |
} | |
} | |
function cstr_encode(input) { | |
var output = "\""; | |
var splitLines = document.frmConvert.chbSplitLines.checked; | |
var escapePercent = document.frmConvert.chbEscapePercent.checked; | |
for (i = 0; i < input.length; i++) { | |
switch (input[i]) { | |
case '\f': | |
output += "\\f"; | |
break; | |
case '\n': | |
if (splitLines) { | |
output += "\\n\"\n\""; | |
} else { | |
output += "\\n"; | |
} | |
break; | |
case '\r': | |
output += "\\r"; | |
break; | |
case '\t': | |
output += "\\t"; | |
break; | |
case '\"': | |
output += "\\\""; | |
break; | |
case '\\': | |
output += "\\\\"; | |
break; | |
case '%': | |
if (escapePercent) { | |
output += "%%"; | |
} else { | |
output += "%"; | |
} | |
break; | |
default: | |
output += input[i]; | |
break; | |
} | |
} | |
output += "\""; | |
return output; | |
} | |
function convert() { | |
var output = cstr_encode(document.frmConvert.ed_input.value); | |
document.frmConvert.ed_output.value = output; | |
} | |
</script> | |
<div id="main"> | |
<h1>Text -> C/C++ string converter</h1> | |
<p>Converting text into C-like literal, escaping newlines, tab, double quotes, backslash.</p> | |
<form name="frmConvert" action=""> | |
<p>Source text:</p> | |
<p><textarea onkeydown="return interceptTabs(event, this);" name="ed_input" rows="10" cols="67"></textarea></p> | |
<p>Options:</p> | |
<p><input type="checkbox" name="chbSplitLines" value="split" checked="yes">split output into multiple lines</p> | |
<p><input type="checkbox" name="chbEscapePercent" value="escapePercent">escape % character to %% (for printf formatting string)</p> | |
<p> | |
<button type="button" name="btnConvert" onclick="convert()"> | |
Convert | |
</button> | |
</p> | |
<p>C/C++ string</p> | |
<p> | |
<textarea name="ed_output" rows="10" cols="67"></textarea> | |
</p> | |
</form> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment