i saw a code and that code had all string in a array.. And each array index was like: "\x31\x32\x33", etc..
How i can convert for example "hello" to that encode format?
if is possible, there a online encoder?
As the #nikjohn said, you can decoded the string by console.log.
And the following code I found from this question. And I made some changes, the output string will be in a \x48 \x65 form.
It will convert the string in a hex coding, and each character will be separated by a space:
String.prototype.hexEncode = function(){
var hex, i;
var result = "";
for (i=0; i<this.length; i++) {
hex = this.charCodeAt(i).toString(16);
result += ("\\x"+hex).slice(-4) + " ";
}
return result;
};
var str = "Hello";
console.log(str.hexEncode());
The result of the above code is \x48 \x65 \x6c \x6c \x6f。
It is an hex coding encoding.
www.unphp.net
http://ddecode.com/hexdecoder/
http://string-functions.com/hex-string.aspx
are the few sites that can give you encoding and decoding using hex coding.
If you console log the string sequence, you get the decoded strings. So it's as simple as
console.log('\x31\x32\x33'); // 123
For encoding said string, you can extend the String prototype:
String.prototype.hexEncode = function(){
var hex, i;
var result = "";
for (i=0; i<this.length; i++) {
hex = this.charCodeAt(i).toString(16);
result += ("\\x"+hex).slice(-4);
}
return result
}
Now,
var a = 'hello';
a.hexEncode(); //\x68\x65\x6c\x6c\x6f
Related
I'm trying to make a Unicode Converter from UTF-8 to UTF-16, but I want to only convert strings and objects and stuff like that in JSON, not the structure of JSON because that would make it unreadable to programs.
Entered UTF-8:
{
"hi":{
"this":["is","just","some","example"]
}
}
Expected UTF-16:
{
"\u0068\u0069":{
"\u0074\u0068\u0069\u0073":["\u0069\u0073","\u006a\u0075\u0073\u0074","\u0073\u006f\u006d\u0065","\u0065\u0078\u0061\u006d\u0070\u006c\u0065"]
}
}
Can anyone help? Also, I'm pretty new to Javascript, so don't be too harsh.
This is a pretty good way to do what you need:
let json_string = '{ "hi": {"this": ["is", "just", "some", "example"]}}'
function encode_to_utf16(x) {
var res = "";
for (var i = 0; i < x.length; i++) res+= "\\u" + ("000" + x[i].charCodeAt(0).toString(16)).substr(-4);
return res;
}
// get all the values present between the double quotes
let arr = json_string.match(/"[^"]+"/gi)
arr.forEach((str)=>{
// replace double quotes with empty string
str = str.replace(/"/gi,"");
// encode and replace them
json_string = json_string.replace(str,encode_to_utf16(str))
})
console.log(json_string)
However, if there is a double quote with escape character (\") somewhere in a string, then this'll break down. Maybe, you can improvise on that.
I want to write a simple encryption function that will encrypt the input text and produce a decryptable output that is also safe to transport over a URL query as a parameter.
This site provides an excellent starting point, however, some of the outputs contain '=' and '?' which would not play nice when sent as a parameter in a query. Code reproduced below:
var jsEncode = {
encode: function(s, k) {
var enc = "";
var str = "";
// make sure that input is string
str = s.toString();
for (var i = 0; i < s.length; i++) {
// create block
var a = s.charCodeAt(i);
// bitwise XOR
var b = a ^ k;
enc = enc + String.fromCharCode(b);
}
return enc;
}
};
var code = '1';
var e = jsEncode.encode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789#,.-", code);
console.log(e);
var d = jsEncode.encode(e, code);
console.log(d);
I won't be able to use any external libraries, only vanilla js.
The inputs would only ever be email and thus these are the only characters I need to worry about:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789#,.-
encodeURIComponent() should be your desired function
see more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
// encodes characters such as ?,=,/,&,:
console.log(encodeURIComponent('?x=шеллы'));
// expected output: "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"
console.log(encodeURIComponent('?x=test'));
// expected output: "%3Fx%3Dtest"
how to convert this string to float?
i want result 900.50
case-1: var convertThis = 'any string here 900,50 also any string here';
case-2: var convertThis = 'any string here 900.50 also any string here';
How to do this?
Try following code:
var text = 'any string here 900,50 also any string here';
var matched = text.match(/\d+[,.]\d+/)[0].replace(',', '.');
var num = parseFloat(matched, 10);
console.log(matched);
console.log(num);
prints:
900.50
900.5
You could do this :
var num = parseFloat(convertThis.replace(/[^\d\.,]/g,'').replace(/,/,'.'));
But be aware that this would break as soon as you have more than one number or dot in your text. If you want something reliable, you need to be more precise about what the string can be.
Supposing you'd want to extract all numbers from a more complex strings, you could do
var numbers = convertThis.split(/\s/).map(function(s){
return parseFloat(s.replace(',','.'))
}).filter(function(v) { return v });
Here, you'd get [900.5]
var myFloat = +(convertThis.match(/\d+[,\.]?\d+/)[0].replace(",","."))
I have this string:
"'California',51.2154,-95.2135464,'data'"
I want to convert it into a JavaScript array like this:
var data = ['California',51.2154,-95.2135464,'data'];
How do I do this?
I don't have jQuery. And I don't want to use jQuery.
Try:
var initialString = "'California',51.2154,-95.2135464,'data'";
var dataArray = initialString .split(",");
Use the split function which is available for strings and convert the numbers to actual numbers, not strings.
var ar = "'California',51.2154,-95.2135464,'data'".split(",");
for (var i = ar.length; i--;) {
var tmp = parseFloat(ar[i]);
ar[i] = (!isNaN(tmp)) ? tmp : ar[i].replace(/['"]/g, "");
}
console.log(ar)
Beware, this will fail if your string contains arrays/objects.
Since you format almost conforms to JSON syntax you could do the following :
var dataArray = JSON.parse ('[' + initialString.replace (/'/g, '"') + ']');
That is add '[' and ']' characters to be beginning and end and replace all "'' characters with '"'. than perform a JSON parse.
I want to send the string to an encryption function which accepts an array of four (32-bit) integers.
So how to convert string to array of 32 bit integers in javascript and divide it to send it to function?
This smells of homework, but here you go.
Method 1:
Assuming you want to convert four characters in a string to ints, this will work:
// Declare your values.
var myString = "1234";
// Convert your string array to an int array.
var numberArray[myString.length];
for (var i = 0; i < myString.length]; i++)
{
numberArray[i] = int.parseInt(myString[i]);
}
// Call your function.
MyEncryptionFunction(numberArray);
Method 2:
Assuming you want to convert four characters to the numeric values of their chars, this will work:
// Declare your values.
var myString = "1,2,3,4";
// Convert your string array to an int array.
var numberArray[myString.length];
for (var i = 0; i < myString.length]; i++)
{
numberArray[i] = myString.charCodeAt(i);
}
// Call your function.
MyEncryptionFunction(numberArray);
Method 3:
Assuming you want to split a group of four numbers separated by a consistent delimiter, this will work.
// Declare your values.
var splitter = ",";
var myString = "1,2,3,4";
// Convert myString to a string array.
var stringArray[] = myString.split(splitter);
// Convert your string array to an int array.
var numberArray[stringArray.length];
for (var i = 0; i < stringArray.length]; i++)
{
numberArray[i] = int.parseInt(stringArray[i]);
}
// Call your function.
MyEncryptionFunction(numberArray);
Use string.charCodeAt(i), to get the numeric char code of string string at position i. Depending on your used encryption, you can apply an own compression method, to combine multiple char codes (most char codes are far smaller than 32 bits).
Example of separating a string in an array consisting of pairs (4 chars):
var string = "A sstring dum doo foo bar";
var result = [];
string += Array((5-(string.length%4))%5).join(" "); //Adding padding at the end
for(var i=3, len=string.length; i<len; i+=4){
result.push([string.charCodeAt(i-3), string.charCodeAt(i-2),
string.charCodeAt(i-1), string.charCodeAt(i)]);
}