开发者

find every occurrence of char codes 255,251,178 respectively. In a string or array

开发者 https://www.devze.com 2023-01-21 04:09 出处:网络
Using indexOf(\'ÿ\')// char for Unicode 255 for example, doesn\'t work. Probably because in开发者_JAVA百科dexOf() needs the Unicode to be less than 127, maybe?.

Using indexOf('ÿ')// char for Unicode 255 for example, doesn't work. Probably because in开发者_JAVA百科dexOf() needs the Unicode to be less than 127, maybe?.

Anyway, I have a big string array from an Ajax binary. I need to find every occurrence of a byte sequence, let's say the sequence is: 255,251,178. Each occurrence's location (loc of the 255 in the sequence) should then be pushed into an array to create this: arr[0]=1st sequence occurrences location arr[1]=2nd sequence occurrences location etc. The search through the string should be able to start at a specified offset. My problem is I can't even get indexOf() to detect the char for unicode 255.

Any help would be appreciated, greatly.

Pat


Without evaluating the merit of your algorithm, you'll be better defining unicode chars like '\u00FF'

Second, indexOf will find any (Unicode) char you want, no such thing as "will not find less than 127".


It works as expected for me (using Node.js):

> var a = '\u00FF';
> console.log(a);
ÿ
> var string="hello, "+a+"there!";
> console.log(string);
hello, ÿthere!
> console.log(string.indexOf(a));
7
> console.log(string.indexOf("ÿ"));
7
> 

On the other hand, you keep mentioning "bytes". Javascript strings are Unicode characters, typically stored in more than one byte each. And you say "Ajax binary" - what's that supposed to mean? Most/all Ajax results are going to be a text format, not binary. Javascript doesn't handle binary data very well.

Could you maybe post a little more detail about what you're trying to do?

0

精彩评论

暂无评论...
验证码 换一张
取 消