迭代的版本,其中包含元素的“组”(Version of iteration which takes “groups” of elements)
我正在看一个旧的帮助器方法,我已经使用了一段时间来跟踪字节数组到输出。 我很久以前写过它并且它工作正常,但我想知道是否有更好的方法(少用代码)。 Linq浮现在我的脑海中,但我所拥有的解决方案非常低效。 我需要的是“foreach16”,或者一些枚举器,而不是一次返回1个元素,返回一组可枚举的元素。 除了我创建自己的枚举器类之外,还有内置的方法吗?
以下示例提供了有关我要完成的更多信息。
原始代码
static void PrintBytes(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { if (i > 0 && ((i % 16) == 0)) { // end of line, flushes bytes and resets buffer Console.WriteLine(" {0}", sb.ToString()); sb.Length = 0; } else if (i > 0 && ((i % 8) == 0)) { Console.Write(" "); sb.Append(' '); } Console.Write(" {0:X2}", (int)bytes[i]); if (' ' <= bytes[i] && bytes[i] <= '~') { sb.Append((char)bytes[i]); } else { // non-ASCII or control chars are printed as '.' sb.Append('.'); } } // flushes the last few bytes if ((bytes.Length % 16) > 0) { // prints spaces where the missing bytes would be int spacesToPrint = 3 * (16 - (bytes.Length % 16)); if ((bytes.Length % 16) <= 8) { spacesToPrint++; } Console.Write(new string(' ', spacesToPrint)); } Console.WriteLine(" {0}", sb.ToString()); }
我现在拥有的 - 这就是我试图简化代码的原因。 但是我正在做很多Skip / Take,这会将代码的复杂性从线性增加到二次。
static void PrintBytesV2(byte[] bytes) { for (int i = 0; i < bytes.Length; i += 16) { PrintLineV2(bytes, i, Math.Min(16, bytes.Length - i)); } } static void PrintLineV2(byte[] array, int offset, int count) { Console.Write( string.Join( " ", array .Skip(offset) .Take(count) .Select((b, i) => ((i == 8) ? " " : "") + string.Format("{0:X2}", (int)b)))); Console.Write( new string( ' ', (16 - count) * 3 + (count <= 8 ? 1 : 0)) + " "); Console.WriteLine( string.Join( "", array .Skip(offset) .Take(count) .Select(b => (' ' <= b && b <= '~') ? (char)b : '.'))); }
请注意,即使新代码是线性的,我也可能会坚持使用原始代码,因为1)它的工作原理; 2)我认为它更清晰。 但我不禁想知道是否有一些迭代群体的方法。
I'm looking at an old helper method which I've been using for a while to trace byte arrays to the output. I wrote it a long time ago and it's been working fine, but I was wondering if there was a better way to do that (with less code). Linq came to my mind, but the solution which I have is awfully inefficient. What I'd need would be something along the lines of a "foreach16", or some enumerator which instead of returning 1 element at a time, returned a group of elements of an enumerable. Besides me creating my own enumerator class, is there a built-in way of doing that?
The examples below have more information on what I'm trying to accomplish.
Original code
static void PrintBytes(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { if (i > 0 && ((i % 16) == 0)) { // end of line, flushes bytes and resets buffer Console.WriteLine(" {0}", sb.ToString()); sb.Length = 0; } else if (i > 0 && ((i % 8) == 0)) { Console.Write(" "); sb.Append(' '); } Console.Write(" {0:X2}", (int)bytes[i]); if (' ' <= bytes[i] && bytes[i] <= '~') { sb.Append((char)bytes[i]); } else { // non-ASCII or control chars are printed as '.' sb.Append('.'); } } // flushes the last few bytes if ((bytes.Length % 16) > 0) { // prints spaces where the missing bytes would be int spacesToPrint = 3 * (16 - (bytes.Length % 16)); if ((bytes.Length % 16) <= 8) { spacesToPrint++; } Console.Write(new string(' ', spacesToPrint)); } Console.WriteLine(" {0}", sb.ToString()); }
What I have now - this is what I tried to simplify the code. But I'm doing many Skip/Take, which increases the complexity of the code from linear to quadratic.
static void PrintBytesV2(byte[] bytes) { for (int i = 0; i < bytes.Length; i += 16) { PrintLineV2(bytes, i, Math.Min(16, bytes.Length - i)); } } static void PrintLineV2(byte[] array, int offset, int count) { Console.Write( string.Join( " ", array .Skip(offset) .Take(count) .Select((b, i) => ((i == 8) ? " " : "") + string.Format("{0:X2}", (int)b)))); Console.Write( new string( ' ', (16 - count) * 3 + (count <= 8 ? 1 : 0)) + " "); Console.WriteLine( string.Join( "", array .Skip(offset) .Take(count) .Select(b => (' ' <= b && b <= '~') ? (char)b : '.'))); }
Notice that even if the new code as it is were linear, I'd likely stick with the original code since 1) it works; and 2) I think it's more legible. But I can't help wondering whether there is some way of iterating over groups.
原文:https://stackoverflow.com/questions/10679245
最满意答案
如果将页面保存为“网页,完整”,则会保存翻译后的页面。
如果将页面保存为“仅网页,仅HTML”,则它将简单地获取网页的源代码(使用Web服务翻译后显然保持不变)。
If you save the page as 'Webpage, Complete', it saves the translated page as well as other files related to it, e.g. images, etc...
If you save the page as 'Webpage, HTML Only', it will simply get the web page's source code (which obviously remains unchanged after translating it using a web service).
相关问答
更多-
如果将页面保存为“网页,完整”,则会保存翻译后的页面。 如果将页面保存为“仅网页,仅HTML”,则它将简单地获取网页的源代码(使用Web服务翻译后显然保持不变)。 If you save the page as 'Webpage, Complete', it saves the translated page as well as other files related to it, e.g. images, etc... If you save the page as 'Webpage, HTML Onl ...
-
来自页面和页面链接的数据将发送到https://translate.googleapis.com/translate_a/ ...查看开发人员工具中的网络请求。 我认为这个数据谷歌只用于训练他们翻译IOT。 期望chrome运输并安装了大量字典和翻译算法是不合理的。 对我来说,这个事实并不阻止我使用服务,但如果数据是高度机密的,... Data from page and page link are sent to https://translate.googleapis.com/translate_a/. ...
-
的manifest.json { "name": "Background", "permissions" : ["http://*/*", "https://*/*"], "version": "1.0", "content_scripts": [{ "matches": ["http://*/*", "https://*/*"], "js": ["script.js"], "all_frames": false, ...
-
IE与Google Chrome(IE vs Google Chrome)[2022-01-01]
我跑了一个jsFiddle,似乎Chrome在第一次眨眼后没有正确地注册功能。 当您在函数中将它定义为e时,您也正在使用keyPress函数中的事件。 这对我有效: var blinkOn =false; blinkCursor = function() { if (blinkOn) { document.getElementById("blink").style.display="none"; } else { ... -
chrome.storage API专门为此目的而创建。 它可用于扩展脚本(例如后台)和内容脚本。 请注意,此存储不被认为是安全的(除了使用chrome.identity存储OAuth令牌之外,还有其他安全的替代方法) chrome.storage API was created specifically for that purpose. It's available both to extension scripts (e.g. background) and content scripts. Note ...
-
打开开发工具,右键单击并选择“检查元素”或Ctrl / Cmd + Shift + J 点击该窗口右下角的Cog图标。 选择常规选项卡 勾选“禁用缓存” 这将禁止Chrome缓存(本地存储)js文件的副本 Open Developer Tools, by right clicking and choosing "Inspect Element" or Ctrl/Cmd + Shift + J Click the Cog icon in the bottom right of that window. Cho ...
-
您的计划的致命弱点来自对文件系统API的误解。 关于文件系统API的最常见的误解是它可能以某种方式给脚本直接读/写访问客户端的本地文件系统(例如,C://无论如何)。 正如已广泛记载的那样,包括在Stackoverflow上的HTML5-FileSystem标签Wiki中 ,“文件系统API 无法直接访问本地文件系统” 。 API提供对“虚拟”文件系统的访问, 而不是用户的本地文件系统。 因此,您不能使用文件系统API“询问用户是否有权访问他们选择的特定目录中的文件”,因为您正在寻找基于浏览器的IDE项目。 ...
-
它称为“The :URI Scheme ”。 It called "The about: URI Scheme".
-
无论何时导入自定义字体,请务必包含所需的所有权重和样式。 普通字体重量通常为400,粗体通常为700.下面的样式表将包含Roboto字体所需的几乎所有内容。 Whenever you import custom fonts, be sure to include al ...
-
Google翻译API目前不提供语音翻译,尽管可以通过网络界面访问。 您可以按照本论坛中解释相同问题的程序提交要包含在API中的要求。 Google Translation API does not currently offer phonetic translation, despite being available in the web interface. You can file a request for that feature to be included in the API by fol ...