本文介绍了flash as3 - 我需要在byteArray数据中进行二进制搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个获取 ByteArray 数据部分的问题。 fileData $ b $ pre $ var fileData:ByteArray = new ByteArray(); //这里的代码用二进制数据填充这个变量 ..... readBytes(fileData,0,1000); // 数据如下: ©YYSYYS-CHK(<<<< start>>>><<所以,我需要找到<<<<<<< ;开始>>> 和<<< end>>>< / code& 。 但是搜索 fileData.toString()。indexOf('<<<<开始>>>') code>有时会得到这个字符串的错误位置,有时根本找不到。 我能做些什么才能正确地确定部分的位置我需要的数据? 解决方案 您不应该使用 fileData.toString()。indexOf() 因为你正在处理二进制数据,你必须搜索一个字节序列。 下面的函数检索指定模式的位置: public function indexOf(bytes:ByteArray,search:String,startOffset:uint = 0):void { if(bytes = = null || bytes.length == 0){抛出新的ArgumentError(字节参数不应该为null或空); if(search == null || search.length == 0){ throw new ArgumentError(search parameter not not null or empty); } //快速返回是搜索模式长度比字节短 if(bytes.length< startOffset + search.length){返回-1; } //创建模式 var pattern:ByteArray = new ByteArray(); pattern.writeUTFBytes(search); //初始化循环变量 var end:Boolean; var found:Boolean; var i:uint = startOffset; var j:uint = 0; var p:uint = pattern.length; var n:uint = bytes.length - p; //重复使用end do { //比较当前字节与模式中的第一个if(bytes [i] == pattern [ 0]){ found = true; j = p; //遍历模式的每个字节 while(--j){ if(bytes [i + j]!= pattern [j]){ found = false; break; //返回模式位置 if(found){ return i; } } //检查end是否到达 end =(++ i> n); } while(!end); //找不到模式 return -1; $ b然后你可以使用这个函数: var extractedBytes = new ByteArray(); var startPos:int = indexOf(fileData,<<<< start>>>); var endPos:int; $ b $ if(startPos == -1){ trace(<<<< start>>> not found); } else { endPos = indexOf(fileData,<<<< end-end>>,startPos + 11);如果(startPos == -1){ trace(<< start>>> ;. length = 11 } ;<< end>>>找不到); } else { //提取<<<开始>>>和<< end>>> fileData.readBytes(extractedBytes,startPos + 11,endPos); 免责声明: ! I have a problem with getting parts of the ByteArray data.There is a binary text in fileData:var fileData:ByteArray = new ByteArray();//..........here's code that fills this var with binary data.....readBytes(fileData,0,1000);//Data is like this:йYЯyeSВ–нkq(г<<<start>>>:xЪмЅdf"cйxЪsdfмЅ"cйdxЪмЅ"cй<<<end>>>В–нkВSo, I need to find position of <<< start >>> and <<< end >>> and copy data, that is between them.But searching fileData.toString().indexOf('<<< start >>>') sometimes gets wrong position of this string, and sometimes can't find it at all.What can I do to correctly determine the position of part of the data that I need? 解决方案 You should not use fileData.toString().indexOf() since you are working with binary data. You have to search a sequence of bytes.The following function retrieve the position of a specified pattern:public function indexOf(bytes:ByteArray, search:String, startOffset:uint = 0):void{ if (bytes == null || bytes.length == 0) { throw new ArgumentError("bytes parameter should not be null or empty"); } if (search == null || search.length == 0) { throw new ArgumentError("search parameter should not be null or empty"); } // Fast return is the search pattern length is shorter than the bytes one if (bytes.length < startOffset + search.length) { return -1; } // Create the pattern var pattern:ByteArray = new ByteArray(); pattern.writeUTFBytes(search); // Initialize loop variables var end:Boolean; var found:Boolean; var i:uint = startOffset; var j:uint = 0; var p:uint = pattern.length; var n:uint = bytes.length - p; // Repeat util end do { // Compare the current byte with the first one of the pattern if (bytes[i] == pattern[0]) { found = true; j = p; // Loop through every byte of the pattern while (--j) { if (bytes[i + j] != pattern[j]) { found = false; break; } } // Return the pattern position if (found) { return i; } } // Check if end is reach end = (++i > n); } while (!end); // Pattern not found return -1;}Then you can use the function this way:var extractedBytes = new ByteArray();var startPos:int = indexOf(fileData, "<<<start>>>");var endPos:int;if (startPos == -1) { trace("<<<start>>> not found");} else { endPos = indexOf(fileData, "<<<end>>>", startPos + 11); // "<<<start>>>".length = 11}if (startPos == -1) { trace("<<<end>>> not found");} else { // Extract the bytes between <<<start>>> and <<<end>>> fileData.readBytes(extractedBytes, startPos + 11, endPos);}Disclaimer: I haven't tested my code! 这篇关于flash as3 - 我需要在byteArray数据中进行二进制搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 06:38