Related
I am pulling down some data from my server and manipulating it and then trying to load the string into an object using JSON.parse(). However whenever I try to do it I get an Unexpected Token error. The weird thing is that usually chrome tells me what the unexpected token is (usually 'o' or something). This time it is just a space. Which makes me think that there is some type of ascii encoding error or something going on. If I print the json string to the console doing console.log() and then paste it directly into the code then it has no problem parsing the string.
var testString = '[ { "pk": 1663, "state": "IO", "group": "ALO", "species": "", "application_link": "" } ]';
var testObject = JSON.parse(testString);
alert(testObject);
Works exactly how I expect it to. But this doesn't:
function hex2ascii(hexx) { //stolen off stackoverflow
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
function decodeData(encoded){
var original_string = '';
for (var i = 0; i < encoded.length; i+=2) {
var index = i/2;
var hex_value = encoded[i]+encoded[i+1];
var ochar = hex2ascii(hex_value);
original_string += ochar;
}
return original_string;
}
$.get(url, function(data) {
var testString = decodeData(data); //decodeData grabs a hex string and then decodes it into a json formatted string
var testObject = JSON.parse(testString); //Unexpected Token
alert(testObject);
});
Anybody have any ideas on how to go about troubleshooting this?
Edit:
I don't know why but I have a bunch of null's in my string. When I iterate through the json string and convert it back to hex I get:
hex_string = ''
for(var i = 0; i < decoded_data.length; i++){
if (i < 10){
alert(decoded_data[i]);
}
hex_string += ascii2hex(decoded_data[i])+' ';
}
console.log(hex_string);
>>>0 0 5b 0 0 7b 0 0 22 0 0 70 0 0 6b 0 0 22 0 0 3a 0 0 20 0 0 31 0 0 36 0 0 36 0 0
Edit again:
Ok so I have pinned it down to my concatenation method. For some reason in the decodeData function when I concatenate it together doing
original_string += ochar;
It's throwing in a bunch of null characters. Is there another way to concatenate the string together?
Edit answer:
Ok, so the problem lied within the hex2ascii function. It was adding in a bunch of null characters. It was code I stole off of stack overflow and wasn't what I expected. I ended up changing it to this and now it's golden.
function hex2ascii(hexx) {
var hex = hexx.toString();//force conversion
return String.fromCharCode(parseInt(hex, 16));
}
you could not juz input break line in javascript string, it will make it invalid.
Invalid
var testString = '[ {
"pk": 1663,
"state": "IO",
"group": "ALO",
"species": ""
]}';
Valid
var testString = '[ {"pk": 1663,"state": "IO","group": "ALO","species": ""]}';
I think that the reason you are getting an Unexpected Token error is because you have that extra close parenthesis on the line that says
$.get(url, function(data)) {
and you are missing a parenthesis at the end.
Ok, so the problem lied within the hex2ascii function. It was adding in a bunch of null characters. It was code I stole off of stack overflow and wasn't what I expected. I ended up changing it to this and now it's golden.
function hex2ascii(hexx) {
var hex = hexx.toString();//force conversion
return String.fromCharCode(parseInt(hex, 16));
}
can any body tell me to split a string in java script with space which is not within single quote.
Like if string is
"0 60 120 180 'node name' 2 34 45 12"
then it will tokenize such that
arr[0]=0
arr[1]=60
arr[2]=120
arr[3]=180
arr[4]='node name'
arr[5]=2
arr[6]=34
arr[7]=45
arr[8]=12
During split if single quotes remove then also good as this is the legend name in chart and I have to fetch that name in single element
This regex will do it:
var arr = string.match(/'[^']*'|[^ ]+/g) || [];
You can do this :
var s = "0 60 120 180 'node name' 2 34 45 12";
var arr = [];
s.split("'").forEach(function(v,i){
arr = arr.concat(i%2 ? v : v.trim().split(' '))
});
It also removes the single quotes :
["0", "60", "120", "180", "node name", "2", "34", "45", "12"]
Demonstration
If you don't have nested quotes, this will work. First I searched for any single quote in the string, then splitting by space:
var s = "0 60 120 180 'node name' 2 34 45 12";
var a = s.split("'");
var r = [];
for (var i = 0, l = a.length; i <l; i++) {
if (i % 2) {
r.push("'" + a[i] + "'");
}
else {
var x = a[i].split(' ');
for (var j = 0, k = x.length; j < k; j++) {
if (x[j] != '') {
r.push(x[j]);
}
}
}
}
console.log(r);
// ["0", "60", "120", "180", "'node name'", "2", "34", "45", "12"]
Think I might be a bit late and it is a bit verbose, but it's one easy to understand and general solution. Works for all delimiters and 'join' characters. Also supports 'joined' words that are more than two words in length.... ie lists like "hello my name is 'jon delaware smith fred' I have a 'long name'"....
On #Bergi's advise I have made it a little less verbose...
function split(input, delimiter, joiner){
var output = [];
var joint = [];
input.split(delimiter).forEach(function(element){
if (joint.length > 0 && element.indexOf(joiner) === element.length - 1)
{
output.push(joint.join(delimiter) + delimiter + element);
joint = [];
}
if (joint.length > 0 || element.indexOf(joiner) === 0)
{
joint.push(element);
}
if (joint.length === 0 && element.indexOf(joiner) !== element.length - 1)
{
output.push(element);
joint = [];
}
});
return output;
}
ES6 solution supporting:
Split by space except for inside quotes
Removing quotes but not for backslash escaped quotes
Escaped quote become quote
Can put quotes anywhere
Code:
string.match(/\\?.|^$/g).reduce((p, c) => {
if(c === "'"){
p.quote ^= 1;
}else if(!p.quote && c === ' '){
p.a.push('');
}else{
p.a[p.a.length-1] += c.replace(/\\(.)/,"$1");
}
return p;
}, {a: ['']}).a
Output:
[ '0', '60', '120', '180', 'node name', '2', '34', '45', '12' ]
How do I convert a byte array into a string?
I have found these functions that do the reverse:
function string2Bin(s) {
var b = new Array();
var last = s.length;
for (var i = 0; i < last; i++) {
var d = s.charCodeAt(i);
if (d < 128)
b[i] = dec2Bin(d);
else {
var c = s.charAt(i);
alert(c + ' is NOT an ASCII character');
b[i] = -1;
}
}
return b;
}
function dec2Bin(d) {
var b = '';
for (var i = 0; i < 8; i++) {
b = (d%2) + b;
d = Math.floor(d/2);
}
return b;
}
But how do I get the functions working the other way?
Thanks.
Shao
You need to parse each octet back to number, and use that value to get a character, something like this:
function bin2String(array) {
var result = "";
for (var i = 0; i < array.length; i++) {
result += String.fromCharCode(parseInt(array[i], 2));
}
return result;
}
bin2String(["01100110", "01101111", "01101111"]); // "foo"
// Using your string2Bin function to test:
bin2String(string2Bin("hello world")) === "hello world";
Edit: Yes, your current string2Bin can be written more shortly:
function string2Bin(str) {
var result = [];
for (var i = 0; i < str.length; i++) {
result.push(str.charCodeAt(i).toString(2));
}
return result;
}
But by looking at the documentation you linked, I think that the setBytesParameter method expects that the blob array contains the decimal numbers, not a bit string, so you could write something like this:
function string2Bin(str) {
var result = [];
for (var i = 0; i < str.length; i++) {
result.push(str.charCodeAt(i));
}
return result;
}
function bin2String(array) {
return String.fromCharCode.apply(String, array);
}
string2Bin('foo'); // [102, 111, 111]
bin2String(string2Bin('foo')) === 'foo'; // true
ES6 update
Now, string 'foo' also equals
String.fromCharCode(...[102, 111, 111])
Original answer
Simply apply your byte array to String.fromCharCode. For example
String.fromCharCode.apply(null, [102, 111, 111])
equals 'foo'.
MDN docs here.
Caveat: works for arrays shorter than 65535 - MDN docs here.
Try the new Text Encoding API:
// create an array view of some valid bytes
let bytesView = new Uint8Array([104, 101, 108, 108, 111]);
console.log(bytesView);
// convert bytes to string
// encoding can be specfied, defaults to utf-8 which is ascii.
let str = new TextDecoder().decode(bytesView);
console.log(str);
// convert string to bytes
// encoding can be specfied, defaults to utf-8 which is ascii.
let bytes2 = new TextEncoder().encode(str);
// look, they're the same!
console.log(bytes2);
console.log(bytesView);
This should work:
String.fromCharCode(...array);
Or
String.fromCodePoint(...array)
That string2Bin can be written even more succinctly, and without any loops, to boot!
function string2Bin ( str ) {
return str.split("").map( function( val ) {
return val.charCodeAt( 0 );
} );
}
String to byte array: "FooBar".split('').map(c => c.charCodeAt(0));
Byte array to string: [102, 111, 111, 98, 97, 114].map(c => String.fromCharCode(c)).join('');
I think this would be more efficient:
function toBinString (arr) {
var uarr = new Uint8Array(arr.map(function(x){return parseInt(x,2)}));
var strings = [], chunksize = 0xffff;
// There is a maximum stack size. We cannot call String.fromCharCode with as many arguments as we want
for (var i=0; i*chunksize < uarr.length; i++){
strings.push(String.fromCharCode.apply(null, uarr.subarray(i*chunksize, (i+1)*chunksize)));
}
return strings.join('');
}
Even if I'm a bit late, I thought it would be interesting for future users to share some one-liners implementations I did using ES6.
One thing that I consider important depending on your environment or/and what you will do with with the data is to preserve the full byte value. For example, (5).toString(2) will give you 101, but the complete binary conversion is in reality 00000101, and that's why you might need to create a leftPad implementation to fill the string byte with leading zeros. But you may not need it at all, like other answers demonstrated.
If you run the below code snippet, you'll see the first output being the conversion of the abc string to a byte array and right after that the re-transformation of said array to it's corresponding string.
// For each byte in our array, retrieve the char code value of the binary value
const binArrayToString = array => array.map(byte => String.fromCharCode(parseInt(byte, 2))).join('')
// Basic left pad implementation to ensure string is on 8 bits
const leftPad = str => str.length < 8 ? (Array(8).join('0') + str).slice(-8) : str
// For each char of the string, get the int code and convert it to binary. Ensure 8 bits.
const stringToBinArray = str => str.split('').map(c => leftPad(c.charCodeAt().toString(2)))
const array = stringToBinArray('abc')
console.log(array)
console.log(binArrayToString(array))
If your array is encoded in UTF-8 and you can't use the TextDecoder API because it is not supported on IE:
You can use the FastestSmallestTextEncoderDecoder polyfill recommended by the Mozilla Developer Network website;
You can use this function also provided at the MDN website:
function utf8ArrayToString(aBytes) {
var sView = "";
for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) {
nPart = aBytes[nIdx];
sView += String.fromCharCode(
nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
/* (nPart - 252 << 30) may be not so safe in ECMAScript! So...: */
(nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
(nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
(nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
(nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
(nPart - 192 << 6) + aBytes[++nIdx] - 128
: /* nPart < 127 ? */ /* one byte */
nPart
);
}
return sView;
}
let str = utf8ArrayToString([50,72,226,130,130,32,43,32,79,226,130,130,32,226,135,140,32,50,72,226,130,130,79]);
// Must show 2H₂ + O₂ ⇌ 2H₂O
console.log(str);
If you are using node.js you can do this:
yourByteArray.toString('base64');
Too late to answer but if your input is in form of ASCII bytes, then you could try this solution:
function convertArrToString(rArr){
//Step 1: Convert each element to character
let tmpArr = new Array();
rArr.forEach(function(element,index){
tmpArr.push(String.fromCharCode(element));
});
//Step 2: Return the string by joining the elements
return(tmpArr.join(""));
}
function convertArrToHexNumber(rArr){
return(parseInt(convertArrToString(rArr),16));
}
I had some decrypted byte arrays with padding characters and other stuff I didn't need, so I did this (probably not perfect, but it works for my limited use)
var junk = String.fromCharCode.apply(null, res).split('').map(char => char.charCodeAt(0) <= 127 && char.charCodeAt(0) >= 32 ? char : '').join('');
> const stringToBin = (str) => [...str].map(item=>item.charCodeAt())
> undefined
> stringToBin('hello')
> (5) [104, 101, 108, 108, 111]
> const binToString = (array) => String.fromCharCode(...array)
> undefined
> binToString(stringToBin('hello'))
> 'hello'
What you are looking for is String.fromCharCode
What you want to do is loop through the array of bytes (represented as integers), create the string equivalent and add it to the result:
function bin2String(array) {
var result = "";
for (const char of array) {
result += String.fromCharCode(char);
}
return result;
}
console.log(bin2String([116, 104, 101, 32, 114, 101, 115, 117, 108, 116]));
You can also use the Array.Map function to convert the array of bytes into an array of strings, then join them all.
function string2Bin(array) {
return array.map(byte => String.fromCharCode(byte)).join("");
}
console.log(string2Bin([116, 104, 101, 32, 114, 101, 115, 117, 108, 116]));
UPDATE
#rosberg-linhares posted best solution so far to handle UTF8.
Didn't find any solution that would work with UTF-8 characters. String.fromCharCode is good until you meet 2 byte character.
For example word Hüser can come over the wire in form of arraybuffer as [0x48,0xc3,0xbc,0x73,0x65,0x72] (e.g. through websocket connection)
But if you go through it with String.fromCharCode you will have Hüser as each byte will be converted to a char separately, and letter ü is encoded in two bytes.
Solution
Currently I'm using following solution:
function pad(n) { return (n.length < 2 ? '0' + n : n); }
function decodeUtf8(data) {
return decodeURIComponent(
data.map(byte => ('%' + pad(byte.toString(16)))).join('')
);
}
The simplest solution I've found is:
var text = atob(byteArray);
Is there a JQuery Plugin available that facilitates NUMBER LOCALIZATION ?
That is , the plugin should translate numerals into their local glyphs.
Arabic | ٤٣٢١ | 1234
Indic (Telugu/Hindi) | ౧౨౩౪౫/१२३४५ | 12345
PS : My requirement is number CONVERSION , not formatting .
You could use a simple function like this one for the translation from Arabic numerals:
function translateNumerals(input, target) {
var systems = {
devanagari: 2406, tamil: 3046, kannada: 3302,
telugu: 3174, marathi: 2406, malayalam: 3430,
oriya: 2918, gurmukhi: 2662, nagari: 2534, gujarati: 2790
},
zero = 48, // char code for Arabic zero
nine = 57, // char code for Arabic nine
offset = (systems[target.toLowerCase()] || zero) - zero,
output = input.toString().split(""),
i, l = output.length, cc;
for (i = 0; i < l; i++) {
cc = output[i].charCodeAt(0);
if (cc >= zero && cc <= nine) {
output[i] = String.fromCharCode(cc + offset);
}
}
return output.join("");
}
And call it like this
translateNumerals(12345, "Telugu") // "౧౨౩౪౫"
translateNumerals(12345, "Devanagari") // "१२३४५"
translateNumerals("foo", "Devanagari") // "foo"
The translation to Arabic numerals is equally simple. It requires just a little more plumbing.
The above function knows the charCode of the Zero in any target system and does some simple math to do a character-by-character conversion from Arabic numerals to that system.
A reverse function would need to check each character of the input. If it falls the charCode range of any source system (i.e. the charCode of the respective Zero + 9) it knows the required offset. Subtracting the offset will get you into the Arabic range. This is pretty trivial and I'll leave it to you.
EDIT #1: Since you mentioned jQuery, the above could be made into a plugin like this:
jQuery.fn.extend({
textTranslateNumerals: (function () {
var translateNumerals = function (input, target) {
// the above function
};
return function (target) {
// for each selected element...
return this.each(function () {
// ...look at each child node and
$(this).contents().each(function () {
// ...translate text nodes, recurse into elements
if (this.nodeType === this.TEXT_NODE) {
$(this).text(translateNumerals($(this).text(), target));
} else if (this.nodeType === this.ELEMENT_NODE) {
$(this).textTranslateNumerals(target);
}
});
});
};
})()
});
EDIT #2: FWIW, here is a function that can convert from any source system into any target system:
function translateNumerals(input, source, target) {
var systems = {
arabic: 48,
devanagari: 2406, tamil: 3046, kannada: 3302, telugu: 3174, marathi: 2406,
malayalam: 3430, oriya: 2918, gurmukhi: 2662, nagari: 2534, gujarati: 2790,
},
output = [], offset = 0, zero = 0, nine = 0, char = 0;
source = source.toLowerCase();
target = target.toLowerCase();
if ( !(source in systems && target in systems)
|| input == null
|| typeof input == "undefined" || typeof input == "object" ) {
return input;
}
input = input.toString();
offset = systems[target] - systems[source];
zero = systems[source];
nine = systems[source] + 9;
for (var i=0, l=input.length; i<l; i++) {
var char = input.charCodeAt(i);
if (char >= zero && char <= nine) {
output.push( String.fromCharCode(char + offset) );
} else {
output.push( input[i] );
}
}
return output.join("");
}
Sample outputs
translateNumerals("0123456789", "arabic", "malayalam") // "൦൧൨൩൪൫൬൭൮൯"
translateNumerals("൦൧൨൩൪൫൬൭൮൯", "malayalam", "arabic") // "0123456789"
translateNumerals("൦൧൨൩൪൫൬൭൮൯", "malayalam", "kannada") // "೦೧೨೩೪೫೬೭೮೯"
translateNumerals("೦೧೨೩೪೫೬೭೮೯", "kannada", "nagari") // "০১২৩৪৫৬৭৮৯"
translateNumerals("೦೧೨೩೪೫೬೭೮೯", "kannada", "gujarati") // "૦૧૨૩૪૫૬૭૮૯"
translateNumerals("USD 13.56", "arabic", "nagari") // "USD ১৩.৫৬"
I think your best bet is the jQuery Globalize plugin: https://github.com/jquery/globalize
I want to create a string in JavaScript that contains all ascii characters. How can I do this?
var s = ' !"#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
My javascript is a bit rusty, but something like this:
s = '';
for( var i = 32; i <= 126; i++ )
{
s += String.fromCharCode( i );
}
Not sure if the range is correct though.
Edit:
Seems it should be 32 to 127 then. Adjusted.
Edit 2:
Since char 127 isn't a printable character either, we'll have to narrow it down to 32 <= c <= 126, in stead of 32 <= c <= 127.
Just loop the character codes and convert each to a character:
var s = '';
for (var i=32; i<=127;i++) s += String.fromCharCode(i);
Just wanted to put this here for reference. (takes about 13/100 to 26/100 of a ms on my computer to generate).
var allAsciiPrintables = JSON.stringify((Array.from(Array(126 + 32).keys()).slice(32).map((item) => {
return String.fromCharCode(item);
})).join(''));
Decomposed:
var allAsciiPrintables = (function() {
/* ArrayIterator */
var result = Array(126 + 32).keys();
/* [0, 126 + 32] */
result = Array.from(result);
/* [32, 126 + 32] */
result = result.slice(32);
/* transform each item from Number to its ASCII as String. */
result = result.map((item) => {
return String.fromCharCode(item);
});
/* convert from array of each string[1] to a single string */
result = result.join('');
/* create an escaped string so you can replace this code with the string
to avoid having to calculate this on each time the program runs */
result = JSON.stringify(result);
/* return the string */
return result;
})();
The most efficient solution(if you do want to generate the whole set each time the script runs, is probably)(takes around 3/100-35/100 of a millisecond on my computer to generate).
var allAsciiPrintables = (() => {
var result = new Array(126-32);
for (var i = 32; i <= 126; ++i) {
result[i - 32] = (String.fromCharCode(i));
}
return JSON.stringify(result.join(''));
})();
strangely, this is only 3-10 times slower than assigning the string literal directly(with backticks to tell javascript to avoid most backslash parsing).
var x;
var t;
t = performance.now();
x = '!\"#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
t = performance.now() - t;
console.log(t);
.
This is a version written in python. Gives all ASCII characters in order as a single string.
all_ascii = ''.join(chr(k) for k in range(128)) # 7 bits
all_chars = ''.join(chr(k) for k in range(256)) # 8 bits
printable_ascii = ''.join(chr(k) for k in range(128) if len(repr(chr(k))) == 3)
>>> print(printable_ascii)
' !"#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'
The last string here, printable_ascii contains only those characters that contain no escapes (i.e. have length == 1). The chars like: \x05, \x06 or \t, \n which does not have its own glyph in your system's font, are filtered out.
len(repr(chr(k))) == 3 includes 2 quotes that come from repr call.
Without doing several appends:
var s = Array.apply(null, Array(127-32))
.map(function(x,i) {
return String.fromCharCode(i+32);
}).join("");
document.write(s);
Here is an ES6 one liner:
asciiChars = Array.from({ length: 95 }, (e, i) => String.fromCharCode(i + 32)).join('');
console.log(asciiChars)
let str = '';// empty string declear
for( var i = 32; i <= 126; i++ )
{
str = str + String.fromCharCode( i ); /* this method received one integer and
convert it into a ascii characters and store it str variable one by one by using
string concatenation method. The loop start for 32 and end 126 */
}
Here is a version in coffeescript
require 'fluentnode'
all_Ascii = ->
(String.fromCharCode(c) for c in [0..255])
describe 'all Ascii', ->
it 'all_Ascii', ->
all_Ascii.assert_Is_Function()
all_Ascii().assert_Size_Is 256
all_Ascii()[0x41].assert_Is 'A'
all_Ascii()[66 ].assert_Is 'B'
all_Ascii()[50 ].assert_Is '2'
all_Ascii()[150 ].assert_Is String.fromCharCode(150)