In javascript I would like to convert this string: '0x7f, 0x88, 0x88, 0x88, 0x88, 0x7f'
to an object that look like this:
['B00000000','B01111111','B10001000','B10001000','B10001000','B10001000','B01111111','B00000000']
Both are displaying char A on a 8*8 Led matrix. How to do this in Javascript?
I won't write your code as you should really figure it out for yourself, but I will give you the (possible) process...
Split the string using .split function into an array
Loop through the
array and convert the Hex to int and store in a new array
Loop
through the new array and convert the ints to the B1111111 format
required. You can do this by starting with 128 and shifting right (or
halving) on each iteration. Compare to the int, if larger you get a 1
and subtract from the int. If not larger you get a 0. Add these 1s
and 0s onto a string and you have the binary representation. Add these strings into a new array, which will be your final result.
You can create an array with var arrayName = []; and then add to it with arrayName.push(someString);
Good luck!
Related
I am getting MongoDB ObjectIds, which are 12 bytes and their hex string looks like this: 62d34be4f8cd489f6d5b8e0e.
I want to use the object id as a map key like this:
new Map().set(ObjectId('62d34be4f8cd489f6d5b8e0e'),"value")
That doesn't really work because each ObjectId object is unique so two with the same id are not equal objects.
There are a few ways I can think of:
Use the hex string as map key:
new Map().set("62d34be4f8cd489f6d5b8e0e", "value")
Use a buffer / Uint8Array, and use some special Map which works with arrays as keys
new Map().set(Buffer.from("62d34be4f8cd489f6d5b8e0e", "hex"), "value")
Turn the buffer into a 12 byte string which looks like b�K���H�m[�\x0E
new Map().set(Buffer.from("62d34be4f8cd489f6d5b8e0e", "hex").toString(), "value")
Turn the buffer into a number (this turns a 12 byte buffer into a 16 byte number so it seems a little wierd)
new Map().set(Buffer.from("62d34be4f8cd489f6d5b8e0e", 'hex').readInt16BE(), "value")
Which method has the best performance?
i have a string format like this:
var a=18,19,20
i want to seperate the numbers before comma and push them to my array which named temp:
while(a.indexOf(',')>-1){
var x=a.split(',');
temp.push(x);
}
but this is all came across my mind which does not work, the string length may vary from time to time,but the format is always the same(separated with a comma)unless i get a single number which does have comma
So, let's say I have a string composed a very large number, say "123456789101112131415" for example. How do I convert that string into an array with those values in it?
Quite Easily Done:
"123456789101112131415".split("")
Is there a way to force .getRange().getValues() to return an int? Although only numbers exist in my range, it is returning them as strings. I would like to avoid using parseInt in every one of my statements or creating a separate array with converted values.
Or is that the only solution, to get the array and then parseInt the entire array in a loop?
you can do this easily using the unary '+' operator as follows:
First get your values from your spreadsheet using getValue() or getValues(). Suppose you get two such values, and store them in A = 1 and B = 2. You can force them to be recognized as numbers by using any math binary operator except for +, which concatenates strings, so A - B = -1, while A + B will return '12'.
You can force the variables to be numbers simply by using the + unary operator with any variable that might be interpreted as a string. For example, +A + +B will return the correct value of 3.
You can use parseInt() or Number()
example
var A='12';var B=5
A+B = 125
parseInt(A)+B = 17
Number(A)+B = 17
That said, getValues() is not supposed to return strings unless values have some space or other non-numeric characters in it... are these values entered manually or come as a result of some function ?
getValues() returns a 2D array of Objects - so these are Strings, Integers or Date objects depending on what these are formatted as in your spreadsheet.
Go back to your spreadsheet and see what the cells that have integer values are formatted as. Format them as integers and you should get back integers.
I have a huge "binary" string, like: 1110 0010 1000 1111 0000 1100 1010 0111....
It's length is 0 modulo 4, and may reach 500,000.
I have also a corresponding array: {14, 2, 8, 15, 0, 12, 10, 7, ...}
(every number in the array corresponds to 4 bits in the string)
Given this string, this array, and a number N, I need to calculate the following substring string.substr(4*N, 4), i.e.:
for N=0 the result should be 1110
for N=1 the result should be 0010
I need to perform this task many many times, and my question is what would be the fastest method to calculate this substring ?
One method is to calculate the substring straight forward: string.substr(4*N, 4). I'm afraid this one is not efficient for such huge strings.
Another method is to use array[N].toString(2) and then wrap the result with zeros if needed. I'm not sure how fast is this.
May be you have any other ideas ?
Where does the string come from? Why not represent the string not as binary, but as hex, and then you can store each four-binary-digit section as a single character? (You could obviously pack it twice that densely if you wanted, or actually now that I think of it, 4 times, since Javascript strings are 16-bit Unicode). Then finding a single group would be a single call to "charAt()", and you'd just have to expand to the binary form via a lookup table.
edit — oh well duhh, you already have an array. In that case don't do the substring work at all; it's crazy. Just grab the array element and translate it through a lookup array into the 4-binary-digit string.
You could consider representing your huge string as a Rope data structure. A rope is basically a binary tree whose leaves are arrays of characters. A node in the tree has a left child and a right child, the left child being the first part of the string, while the right child the final part.
By using a rope, substring operations become logarithmic in complexity, rather then linear, as they are for regular strings.
If you want it padded, you could do this:
var elem = array[N]
var str = "" + ((elem>>3)&1) + ((elem>>2)&1) + ((elem>>1)&1) + (elem&1);
The array already has exactly what you need, does it not, save that you need to print it in binary format. Fortunately, sprintf for javascript is available.