2025-07-18 15:38:28 +08:00

604 lines
20 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Class BytesUtil | HiAPI-C# 2025 </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="title" content="Class BytesUtil | HiAPI-C# 2025 ">
<meta name="description" content="Utility class for byte array operations and memory size conversions.">
<link rel="icon" href="../img/HiAPI.favicon.ico">
<link rel="stylesheet" href="../public/docfx.min.css">
<link rel="stylesheet" href="../public/main.css">
<meta name="docfx:navrel" content="../toc.html">
<meta name="docfx:tocrel" content="toc.html">
<meta name="docfx:rel" content="../">
<meta name="loc:inThisArticle" content="In this article">
<meta name="loc:searchResultsCount" content="{count} results for &quot;{query}&quot;">
<meta name="loc:searchNoResults" content="No results for &quot;{query}&quot;">
<meta name="loc:tocFilter" content="Filter by title">
<meta name="loc:nextArticle" content="Next">
<meta name="loc:prevArticle" content="Previous">
<meta name="loc:themeLight" content="Light">
<meta name="loc:themeDark" content="Dark">
<meta name="loc:themeAuto" content="Auto">
<meta name="loc:changeTheme" content="Change theme">
<meta name="loc:copy" content="Copy">
<meta name="loc:downloadPdf" content="Download PDF">
<script type="module" src="./../public/docfx.min.js"></script>
<script>
const theme = localStorage.getItem('theme') || 'auto'
document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
</script>
</head>
<body class="tex2jax_ignore" data-layout="" data-yaml-mime="ManagedReference">
<header class="bg-body border-bottom">
<nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
<div class="container-xxl flex-nowrap">
<a class="navbar-brand" href="../index.html">
<img id="logo" class="svg" src="../img/HiAPI.logo.png" alt="">
</a>
<button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
<i class="bi bi-three-dots"></i>
</button>
<div class="collapse navbar-collapse" id="navpanel">
<div id="navbar">
<form class="search" role="search" id="search">
<i class="bi bi-search"></i>
<input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search">
</form>
</div>
</div>
</div>
</nav>
</header>
<main class="container-xxl">
<div class="toc-offcanvas">
<div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<nav class="toc" id="toc"></nav>
</div>
</div>
</div>
<div class="content">
<div class="actionbar">
<button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
<i class="bi bi-list"></i>
</button>
<nav id="breadcrumb"></nav>
</div>
<article data-uid="Hi.Common.BytesUtil">
<h1 id="Hi_Common_BytesUtil" data-uid="Hi.Common.BytesUtil" class="text-break">
Class BytesUtil
</h1>
<div class="facts text-secondary">
<dl><dt>Namespace</dt><dd><a class="xref" href="Hi.html">Hi</a>.<a class="xref" href="Hi.Common.html">Common</a></dd></dl>
<dl><dt>Assembly</dt><dd>HiGeom.dll</dd></dl>
</div>
<div class="markdown summary"><p>Utility class for byte array operations and memory size conversions.</p>
</div>
<div class="markdown conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static class BytesUtil</code></pre>
</div>
<dl class="typelist inheritance">
<dt>Inheritance</dt>
<dd>
<div><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object">object</a></div>
<div><span class="xref">BytesUtil</span></div>
</dd>
</dl>
<dl class="typelist inheritedMembers">
<dt>Inherited Members</dt>
<dd>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)">object.Equals(object)</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)">object.Equals(object, object)</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.gethashcode">object.GetHashCode()</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.gettype">object.GetType()</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone">object.MemberwiseClone()</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.referenceequals">object.ReferenceEquals(object, object)</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.tostring">object.ToString()</a>
</div>
</dd></dl>
<h2 class="section" id="methods">Methods
</h2>
<a id="Hi_Common_BytesUtil_ConcatByteArray_" data-uid="Hi.Common.BytesUtil.ConcatByteArray*"></a>
<h3 id="Hi_Common_BytesUtil_ConcatByteArray_System_Collections_Generic_IEnumerable_System_Byte____" data-uid="Hi.Common.BytesUtil.ConcatByteArray(System.Collections.Generic.IEnumerable{System.Byte[]})">
ConcatByteArray(IEnumerable&lt;byte[]&gt;)
</h3>
<div class="markdown level1 summary"><p>Concatenates multiple byte arrays into a single byte array.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static byte[] ConcatByteArray(this IEnumerable&lt;byte[]&gt; src_)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>src_</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1">IEnumerable</a>&lt;<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]&gt;</dt>
<dd><p>The source byte arrays to concatenate.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</dt>
<dd><p>A single byte array containing all the bytes from the source arrays.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_FromBytes_" data-uid="Hi.Common.BytesUtil.FromBytes*"></a>
<h3 id="Hi_Common_BytesUtil_FromBytes__1_System_Byte___" data-uid="Hi.Common.BytesUtil.FromBytes``1(System.Byte[])">
FromBytes&lt;T&gt;(byte[])
</h3>
<div class="markdown level1 summary"><p>Converts a byte array to a structure.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static T FromBytes&lt;T&gt;(byte[] arr) where T : new()</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>arr</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</dt>
<dd><p>The byte array containing the structure data.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><span class="xref">T</span></dt>
<dd><p>The structure created from the byte array.</p>
</dd>
</dl>
<h4 class="section">Type Parameters</h4>
<dl class="parameters">
<dt><code>T</code></dt>
<dd><p>The type of the structure to create.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_GetMemorySizeValueUnit_" data-uid="Hi.Common.BytesUtil.GetMemorySizeValueUnit*"></a>
<h3 id="Hi_Common_BytesUtil_GetMemorySizeValueUnit_System_Int64_" data-uid="Hi.Common.BytesUtil.GetMemorySizeValueUnit(System.Int64)">
GetMemorySizeValueUnit(long)
</h3>
<div class="markdown level1 summary"><p>Gets the value and unit for a memory size in bytes.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static (double val, string unit) GetMemorySizeValueUnit(long num)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>num</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int64">long</a></dt>
<dd><p>The size in bytes.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt>(<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.double">double</a> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.valuetuple-system.double,system.string-.val">val</a>, <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.valuetuple-system.double,system.string-.unit">unit</a>)</dt>
<dd><p>A tuple containing the converted value and the appropriate unit.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_SplitByteArray_" data-uid="Hi.Common.BytesUtil.SplitByteArray*"></a>
<h3 id="Hi_Common_BytesUtil_SplitByteArray_System_Collections_Generic_IEnumerable_System_Byte__System_Int32_System_Boolean_" data-uid="Hi.Common.BytesUtil.SplitByteArray(System.Collections.Generic.IEnumerable{System.Byte},System.Int32,System.Boolean)">
SplitByteArray(IEnumerable&lt;byte&gt;, int, bool)
</h3>
<div class="markdown level1 summary"><p>Splits a byte array into multiple arrays of a specified size.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static byte[][] SplitByteArray(this IEnumerable&lt;byte&gt; src_, int sliceSize, bool allowReferenceBySource = false)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>src_</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1">IEnumerable</a>&lt;<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>&gt;</dt>
<dd><p>The source byte enumerable to split.</p>
</dd>
<dt><code>sliceSize</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></dt>
<dd><p>The size of each slice.</p>
</dd>
<dt><code>allowReferenceBySource</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.boolean">bool</a></dt>
<dd><p>If true and the source array is smaller than or equal to the slice size, returns the source array directly.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[][]</dt>
<dd><p>An array of byte arrays, each containing a slice of the original array.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_ToBytes_" data-uid="Hi.Common.BytesUtil.ToBytes*"></a>
<h3 id="Hi_Common_BytesUtil_ToBytes_System_Collections_BitArray_" data-uid="Hi.Common.BytesUtil.ToBytes(System.Collections.BitArray)">
ToBytes(BitArray)
</h3>
<div class="markdown level1 summary"><p>Converts a BitArray to a byte array.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static byte[] ToBytes(this BitArray bitArray)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>bitArray</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.bitarray">BitArray</a></dt>
<dd><p>The BitArray to convert.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</dt>
<dd><p>A byte array representing the BitArray.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_ToBytes_" data-uid="Hi.Common.BytesUtil.ToBytes*"></a>
<h3 id="Hi_Common_BytesUtil_ToBytes__1___0_" data-uid="Hi.Common.BytesUtil.ToBytes``1(``0)">
ToBytes&lt;T&gt;(T)
</h3>
<div class="markdown level1 summary"><p>Converts a structure to a byte array.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static byte[] ToBytes&lt;T&gt;(T str)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>str</code> <span class="xref">T</span></dt>
<dd><p>The structure to convert.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</dt>
<dd><p>A byte array containing the structure data.</p>
</dd>
</dl>
<h4 class="section">Type Parameters</h4>
<dl class="parameters">
<dt><code>T</code></dt>
<dd><p>The type of the structure to convert.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_ToLongByMemorySizeString_" data-uid="Hi.Common.BytesUtil.ToLongByMemorySizeString*"></a>
<h3 id="Hi_Common_BytesUtil_ToLongByMemorySizeString_System_String_" data-uid="Hi.Common.BytesUtil.ToLongByMemorySizeString(System.String)">
ToLongByMemorySizeString(string)
</h3>
<div class="markdown level1 summary"><p>Parses a memory size string (e.g., &ldquo;10MB&rdquo;, &ldquo;2.5GB&rdquo;) into a long value representing bytes.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static long ToLongByMemorySizeString(this string memorySizeString)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>memorySizeString</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
<dd><p>The memory size string to parse.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int64">long</a></dt>
<dd><p>The parsed value in bytes.</p>
</dd>
</dl>
<h4 class="section">Exceptions</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.formatexception">FormatException</a></dt>
<dd><p>Thrown when the parsing fails.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_ToMemorySizeString_" data-uid="Hi.Common.BytesUtil.ToMemorySizeString*"></a>
<h3 id="Hi_Common_BytesUtil_ToMemorySizeString_System_Int32_System_String_" data-uid="Hi.Common.BytesUtil.ToMemorySizeString(System.Int32,System.String)">
ToMemorySizeString(int, string)
</h3>
<div class="markdown level1 summary"><p>Converts an integer value representing bytes to a formatted memory size string.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static string ToMemorySizeString(this int num, string format = &quot;{0,6:###.00} {1,-2:##}&quot;)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>num</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></dt>
<dd><p>The size in bytes.</p>
</dd>
<dt><code>format</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
<dd><p>The format string to use for formatting the output.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
<dd><p>A formatted string representing the memory size.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_ToMemorySizeString_" data-uid="Hi.Common.BytesUtil.ToMemorySizeString*"></a>
<h3 id="Hi_Common_BytesUtil_ToMemorySizeString_System_Int64_System_String_" data-uid="Hi.Common.BytesUtil.ToMemorySizeString(System.Int64,System.String)">
ToMemorySizeString(long, string)
</h3>
<div class="markdown level1 summary"><p>Converts a long value representing bytes to a formatted memory size string.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static string ToMemorySizeString(this long num, string format = &quot;{0,6:###.00} {1,-2:##}&quot;)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>num</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int64">long</a></dt>
<dd><p>The size in bytes.</p>
</dd>
<dt><code>format</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
<dd><p>The format string to use for formatting the output.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
<dd><p>A formatted string representing the memory size.</p>
</dd>
</dl>
<a id="Hi_Common_BytesUtil_TryParseLongByMemorySizeString_" data-uid="Hi.Common.BytesUtil.TryParseLongByMemorySizeString*"></a>
<h3 id="Hi_Common_BytesUtil_TryParseLongByMemorySizeString_System_String_System_Int64__" data-uid="Hi.Common.BytesUtil.TryParseLongByMemorySizeString(System.String,System.Int64@)">
TryParseLongByMemorySizeString(string, out long)
</h3>
<div class="markdown level1 summary"><p>Tries to parse a memory size string (e.g., &ldquo;10MB&rdquo;, &ldquo;2.5GB&rdquo;) into a long value representing bytes.</p>
</div>
<div class="markdown level1 conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static bool TryParseLongByMemorySizeString(this string memorySizeString, out long dst)</code></pre>
</div>
<h4 class="section">Parameters</h4>
<dl class="parameters">
<dt><code>memorySizeString</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
<dd><p>The memory size string to parse.</p>
</dd>
<dt><code>dst</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int64">long</a></dt>
<dd><p>When this method returns, contains the parsed value if the parsing succeeded, or long.MaxValue if the parsing failed.</p>
</dd>
</dl>
<h4 class="section">Returns</h4>
<dl class="parameters">
<dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.boolean">bool</a></dt>
<dd><p>true if the parsing succeeded; otherwise, false.</p>
</dd>
</dl>
</article>
<div class="contribution d-print-none">
</div>
</div>
<div class="affix">
<nav id="affix"></nav>
</div>
</main>
<div class="container-xxl search-results" id="search-results"></div>
<footer class="border-top text-secondary">
<div class="container-xxl">
<div class="flex-fill">
<span> Copyright © 2025 <a href='https://superhightech.com.tw'>Tech Coordinate</a>. All rights reserved. <a href='https://superhightech.com.tw'>超級高科技股份有限公司</a> © 2025 版權所有 </span>
</div>
</div>
</footer>
</body>
</html>