Javascript analogue of Python's string.encode('UTF-8') - javascript

I have a string that represents some binary data, looking like:
\x89PNG\x1a\x00\x00\x00IHDR\x00\x00 etc
I need to post this string to some API, etc. AS IS but the problem is that Javascript automatically converts it to
PNG etc
.escape, .encodeURI.. etc don't help
In Python such conversion can be done like string.encode('UTF-8') but I can't find nothing like that in JS.
Maybe someone knows the library or something that may help?

In Javascript we usualy use Base64 for this.
You can do something like
var encodedData = window.btoa(stringToEncode);
var decodedData = window.atob(encodedData);
You may also find this interesting
function encode_utf8(s) {
return unescape(encodeURIComponent(s));
}
function decode_utf8(s) {
return decodeURIComponent(escape(s));
}
Or reference https://stackoverflow.com/a/22373061/6302200

Related

Reading windows registry Reg_SZ in javascript

first time posting here!:)
As title says I have a win reg fie (reg_sz) which contains "name" and "value"
this.reg = new Registry.Key(Registry.windef.HKEY.HKEY_CURRENT_USER, 'Path\Path\Path', Registry.windef.KEY_ACCESS.KEY_READ);
funct read(this.reg){
var value;
var pushes = [];
[
"food",
"veggy",
"etc",
"etc"].forEach(function(name) {
try {
value = key.getValue(name);
entries.push({name: name, value: value});
} catch (e) {
}
});
return pushes;
};
example: "food"="apple"
Which my code reads properly, however I came across a issue with special characters, example "ä"
"food"="äpple"
which my code reads as �pple.
My question is what kind of decoding/encoding should i use and what is with this win registry, what exactly are they using? Can it be raw JS preferably and if not what else? I tried using decodeURI/encodeURI but seems like its not the correct approach(dont know what encoding they are using and which decoding should I use)
TLDR: How can i type in "äpple" in win registry and when reading that file with JS get same "äpple" instead of "�pple"
It looks like you're using windows-registry-node. This is unfortunately a bug in that, #44. The reporter says:
If i return the raw buffer and use iconv to convert from "ISO-8859-1" to "UTF-8" i get the correct characters
Note that this is assuming the current code page of the system, though, and might not always be correct. (It might be possible to tell iconv to detect and use the current code page?)
The exact problem is in registry.js:
// READ VALUE
result = advApi.RegQueryValueExA(key.handle.deref(), valueName, null, pKeyType, value,
pKeyDataLength);
Here it uses RegQueryValueExA, which means fetch strings as the current Windows code page, as opposed to RegQueryValueExW which would use UTF-16. So value, which is a Node.JS Buffer, does not contain UTF-8. The code then calls Buffer.toString(), which assumes UTF-8 by default:
if (value.type === types.LPTSR) {
// TODO not sure why buffer's utf8 parsing leaves in the unicode null
// escape sequence. This is a work-around (at least on node 4.1)
value = value.toString().replace('\u0000', '');
}
So this is going to need a fix in windows-registry-node. The best fix is probably to set the code up for UTF-16, using the -W version of the function and value.toString('utf16le');

.NET Core Object JSON Serialization to Javascript format

I've some problems with serializing my C# objects into a plain JSON string.
I user JsonConvert ( Newtonsoft's one) to format a model into a JSON. The problem is that that JSON string get's used in some Javascript, but the format is not good as in a quote gets written down as "&quote;" instead of "'". Any idea's on how to fix this ?
//...
#{
var dataJson = JsonConvert.SerializeObject(Model);
}
//...
<script>
function ChangeGroup(type) {
$.ajax({
url: //...,
data: #dataJson
});
}
</script>
what I get is this:
Some formatting options I forget to set ?
There's a much shorter, easier to use and remember in ASP.NET Core:
#Json.Serialize(Model);
When assigned to a JavaScript value, the resulting JavaScript is valid:
<script>
var model = #Json.Serialize(model);
</script>
With this, you don't have to worry about HTML-escaping the characters.
You can do this:
#{
var dataJson = new HtmlString(JsonConvert.SerializeObject(Model));
}
By default ASP.Net Core will HTML encode before rendering an # expression unless the expression evaluates to a type with the interface IHtmlContent (which HtmlString has). Another way is to write
#Html.Raw(dataJson)
I have used the following to get data out of my model into a JS object. Thought I would post to possibly help someone in the future...
var items = #Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Items));

nodeJS, MySQL and UTF8

I am trying to write a custom String.Prototype function:
String.prototype.Apos = function () {
var string = this.innerHTML.toString();
return string.replace(/’/g,"'").replace(/“|â€/g,'"');
};
I really just want to write a utf8 string to the browser using javascript, however using decodeURIComponent wont work and so I have just resorted to replacing the apostrophes myself.
So from the examples I've seen I wrote the above function, however it doesnt seem to return anything. When I run the following:
$("span").html(string.Apos);
I don't get a response. I've never written a custom prototype function before so could someone help me out and tell me where Im going wrong?
Do you really need to mess with string.prototype?
You can write a function to do the specific job you want to perform, i.e., replace text.
function replaceQuotes(i, oldHtml) {
return oldHtml.toString().replace(/’/g,"'").replace(/“|â€/g,'"');
}
And then:
$("span").html(replaceQuotes);

Decoding Javascript File

need to know wich kind of encoded is this:
$(_0x6b88[150])[_0x6b88[149]]();
$(_0x6b88[154])[_0x6b88[153]](function () {
$(this)[_0x6b88[151]](_0x6b88[14]);
})[_0x6b88[152]](function () {
$(this)[_0x6b88[151]](_0x6b88[12]);
});
I had encoded my js code some months ago and now I dont know where I saved the beautified version.
Can anyone help me? How can I decode this? It look like Hexadecimal or something (_0x6b88[14]) but the rect [] looks very strange too me.
Thanks.
0x6b88 is a number in hexadecimal.
so _0x6b88 is like writing _27528, or myinteger. It is a variable name, the name doesn't really matter.
(_27528 is a different variable to _0x6b88, though)
It looks like an array, so lets replace it with alist to make it easier to understand:
$(alist[150])[alist[149]](); // runs an element with id alist[149]
$(alist[154])[alist[153]]( // runs an element and gives it a function
function () {
$(this)[alist[151]](alist[14]);
}
)
[alist[152]]( // gets an element from what ever is returned by the function
function () { // Passes a function to the element gotten
$(this)[alist[151]](alist8[12]);
}
);
alist[0] is the first element in the array. Inorder to work out what this does, you need to know what is in the cells of those arrays.
It is probably best to rewrite your code; it will be easier that way. It has been obfuscated, so you'll have a hard time working it out.
You probably won't be able to get the original variables names, although that depends on the software you used for obfuscating the code.

custom specifier in javascript/jquery

i want to format the numeric value in java script /jquery like that doing in C#. please refer below link.
http://msdn.microsoft.com/en-us/library/0c899ak8%28v=VS.90%29.aspx#SpecifierD
what are the formats currently we can do in javascript (currency, percentange,numeric).
i know that jquery.globalize script will do some formats like ($,%) i want to know what are the formats we can perform or implement in javascript.
is there any in build function for format the date time like format in C#
(dd/mm/yy)
Thanks,
Siva
Personally, I am not a great fan of the "plugins with jam" and "plugins with ketchup" approach to solving problems. As a general rule when you find yourself thinking "How can I do X in JavaScript like I do it in C# or PHP" it is well worth checking out PHP JS. In the present instance I suspect the sprintf function will do the trick.
Another useful function is this
String.prototype.format = function (args)
{
var newStr = this;
for (var key in args){newStr = newStr.replace('{' + key + '}', args[key]);}
return newStr;
}
//for example
console.log("You owe me ${aa}".format({aa:100}));
The string prototype extension is not my own work - I found it somewhere. Maybe here on SO, I cannot remember.

Categories

Resources