{"id":103955,"date":"2024-01-14T09:57:38","date_gmt":"2024-01-14T08:57:38","guid":{"rendered":"https:\/\/code-maze.com\/?p=103955"},"modified":"2024-01-14T09:57:38","modified_gmt":"2024-01-14T08:57:38","slug":"csharp-converting-string-to-byte-array","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/","title":{"rendered":"Converting String to Byte Array in C#"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">In this article, we&#8217;ll explore the concept of converting a string to a byte array in C#. We will also talk about why this conversion is necessary and explore the methods we can use for that.<\/span><\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/collections-arrays\/ConvertingStringToByteArray\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p><span style=\"font-weight: 400;\">So let\u2019s begin.<\/span><\/p>\n<h2>Why Conversion Is Necessary<\/h2>\n<p><span style=\"font-weight: 400;\">We\u2019ll most likely encounter the need to convert a string to a byte array to achieve efficient image processing, network communication, or encryption and decryption in our application.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But why is this conversion necessary? Well, strings represent human-readable characters, while computers process information as binary data. This means we need to translate strings into a format machines can understand and efficiently process. And <strong>byte arrays offer such a format for storing binary data in a compact and optimized way<\/strong>.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Knowing this, let&#8217;s see the various methods for converting a string to a byte array.<\/span><\/p>\n<h3>Before We Begin<\/h3>\n<p><span style=\"font-weight: 400;\">For all of the code samples in this article, let&#8217;s create a string literal that we&#8217;ll use to convert to a byte array:<\/span><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var message = \"Welcome to CodeMaze!\";<\/code><\/p>\n<p>With this string, we expect the same output for each conversion method:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">87,101,108,99,111,109,101,32,116,111,32,67,111,100,101,109,97,122,101,33  <\/code><\/p>\n<h2>Converting Using Encoding.GetBytes() Method<\/h2>\n<p><span style=\"font-weight: 400;\"><strong>This method is the most common and recommended way for converting a string to a byte array in C#<\/strong>. It is a method that resides in the <code>System.Text<\/code> namespace, and it provides fine-grained control over the encoding used, ensuring proper interpretation of the data across multiple systems.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are several encoding options and we&#8217;ll talk about choosing the right one later on, but now let\u2019s see how to do the conversion using the <a href=\"https:\/\/code-maze.com\/csharp-efficiently-converting-strings-with-utf-8-string-literals\/\" target=\"_blank\" rel=\"noopener\"><code>UTF8<\/code><\/a> encoding option:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">static byte[] ConvertStringToUTF8Bytes(string message)\r\n{\r\n    return Encoding.UTF8.GetBytes(message);\r\n}<\/pre>\n<p>We define a static method and promptly return the byte array which now holds the encoded string. It does this by using the <code class=\"\">Encoding.GetBytes()<\/code> method that performs the <code>UTF8<\/code> encoding on our <code>message<\/code> variable, converting the string&#8217;s characters into their corresponding byte representations.<\/p>\n<h3>Choosing the Right Encoding Option<\/h3>\n<p>Selecting the &#8220;right&#8221; encoding for converting strings to byte arrays largely depends on context. Factors like the target platform, audience, storage and transmission efficiency, and interoperability, should guide our decision.<\/p>\n<p>The <code>UTF-8<\/code> format, <strong>widely used on the web, is ideal for internationalization and varied character sets (web APIs, JSON, network protocols)<\/strong>, while the <code>ASCII<\/code> format <strong>is best for legacy systems, ASCII-reliant formats (some CSV files), and minimal file size needs (English-only text)<\/strong>.<\/p>\n<p>There&#8217;s also the <code>UTF-16<\/code> format, <strong>which is fitting for internal data in applications dealing with European and Asian languages<\/strong>, and last but not least, <code>UTF-32<\/code>, whose rare use <strong>is limited to applications requiring the full range of Unicode characters, like specialized language processing or fonts<\/strong>.<\/p>\n<p><span style=\"font-weight: 400;\">By understanding these common encodings&#8217; characteristics and usage scenarios, we can choose the most appropriate one for our specific needs.<\/span><\/p>\n<h2>Convert by Casting Individual Characters to Bytes<\/h2>\n<p>Casting characters to bytes offers a direct approach for string-to-byte-array conversion, <strong>but\u00a0it&#8217;s crucial to note its limitations<\/strong>.<\/p>\n<p>It effectively translates ASCII characters (0-127) to their byte representations based on the character&#8217;s position in the ASCII table. Still, it struggles with non-ASCII characters like Cyrillic, emojis, or special symbols, potentially leading to incorrect or incomplete conversions.<\/p>\n<p>Let&#8217;s see how to use this technique for conversion:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">static byte[] ConvertStringToByteArrayUsingCasting(string message)\r\n{\r\n    var byteArray = new byte[message.Length];\r\n\r\n    for (int i = 0; i &lt; message.Length; i++)\r\n    {\r\n        byteArray[i] = (byte)message[i];\r\n    }\r\n\r\n    return byteArray;\r\n}<\/pre>\n<p>We initialize a byte array with a size equal to the length of the string. This creates a byte array with enough space to hold each string character as a byte value. Then we iterate through each character in the string, allowing us to process each character individually.<\/p>\n<p data-sourcepos=\"7:1-7:91\">Within the loop, we assign each character to its corresponding index in the byte array. This effectively converts each character to its underlying byte value and stores it in the byte array.<\/p>\n<p data-sourcepos=\"7:1-7:91\">Finally, we return the populated byte array. This array now contains the byte representation of the original string, where each character&#8217;s Unicode code point is stored as a single byte.<\/p>\n<h2>Convert To Byte Array Using Convert.ToByte() Method<\/h2>\n<p>This method is part of the <code class=\"\">Convert<\/code> class and it offers various data type conversion utilities. Similar to the previous method, it provides a direct way to convert a single character to its byte value. <strong>It is however not designed for converting entire strings to byte arrays<\/strong>.<\/p>\n<p>Conversion using this technique is straightforward:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">static byte[] ConvertStringToByteArrayUsingConvertToByte(string message)\r\n{\r\n    var byteArray = new byte[message.Length];\r\n\r\n    for (int i = 0; i &lt; message.Length; i++)\r\n    {\r\n        byteArray[i] = Convert.ToByte(message[i]);\r\n    }\r\n\r\n    return byteArray;\r\n}<\/pre>\n<p>While this code shares a similar structure with the previous example, it employs the <code>Convert.ToByte()<\/code> method to perform the character-to-byte conversion. This approach offers a more explicit and potentially more readable way to convert characters to their corresponding byte values.<\/p>\n<h2>Convert String to Byte Array Using Encoding.GetEncoding() Method<\/h2>\n<p>This method obtains an <code>Encoding<\/code> object for a specific encoding, which can then be used for various encoding-related operations, including converting strings to byte arrays using <code>GetBytes()<\/code>.<\/p>\n<p>Let&#8217;s explore using this method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">static byte[] ConvertStringToByteArrayUsingEncoding(string message)\r\n{\r\n    var encoding = Encoding.GetEncoding(\"ISO-8859-1\");\r\n    var byteCount = encoding.GetByteCount(message);\r\n    var byteArray = new byte[byteCount];\r\n\r\n    encoding.GetBytes(message, byteArray);\r\n\r\n    return byteArray;\r\n}<\/pre>\n<p>Our method employs encoding to transform a string into its corresponding byte array\u00a0by obtaining a specific instance of the <code>ISO-8859-1<\/code> encoding using the <code class=\"\">Encoding.GetEncoding()<\/code> method. This encoding is crucial for determining how characters within the string are mapped to their byte values.<\/p>\n<p>Next, it meticulously calculates the exact number of bytes required to accommodate the encoded string with <code class=\"\">encoding.GetByteCount()<\/code>and constructs a byte array with the calculated byte count via <code class=\"\">new byte[]<\/code>.\u00a0<\/p>\n<p>It translates the string characters into their corresponding byte values while storing the encoded bytes within the provided array. In the end, it returns the now-populated byte array.<\/p>\n<h2>Benchmarks<\/h2>\n<p>Now that we&#8217;ve explored various methods for converting a string into a byte array, let&#8217;s put them to the test. We&#8217;ll analyze their performance using <a href=\"https:\/\/code-maze.com\/benchmarking-csharp-and-asp-net-core-projects\/\" target=\"_blank\" rel=\"noopener\">benchmarks<\/a>, starting with a short string and then tackling a longer one.<\/p>\n<p>For the short string, we&#8217;ll use our initial <code class=\"\">message<\/code> value and add some extra text for the long one:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Welcome to CodeMaze, your one-stop destination for mastering all things .NET and C#! Explore a comprehensive learning experience tailored to your programming journey.<\/code><\/p>\n<p>Let&#8217;s look at the <strong>short string<\/strong> benchmarks:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">|                    Method |      Mean |     Error |    StdDev |    Median |    Gen0 | Allocated |\r\n|-------------------------- |----------:|----------:|----------:|----------:|--------:|----------:|\r\n|       ConvertUsingCasting |  21.03 ns |  0.396 ns |  0.371 ns |  20.93 ns |  0.0115 |  48   B   |\r\n| ConvertUsingConvertToByte |  26.52 ns |  0.515 ns |  0.787 ns |  26.42 ns |  0.0114 |  48   B   |\r\n|        ConvertToUTF8Bytes |  29.37 ns |  0.779 ns |  2.146 ns |  28.57 ns |  0.0114 |  48   B   |\r\n|   ConvertUsingGetEncoding |  73.79 ns |  1.565 ns |  1.922 ns |  72.96 ns |  0.0114 |  48   B   |<\/pre>\n<p>Next, we have the <strong>long string<\/strong> benchmarks:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">|                     Method |      Mean |     Error |    StdDev |    Median |    Gen0 | Allocated |\r\n|--------------------------- |----------:|----------:|----------:|----------:|--------:|----------:|\r\n|         ConvertToUTF8Bytes |  68.59 ns |  2.295 ns |  6.474 ns |  65.99 ns |  0.0458 |  192  B   |\r\n|    ConvertUsingGetEncoding | 101.85 ns |  2.088 ns |  2.144 ns | 101.54 ns |  0.0459 |  192  B   |\r\n|        ConvertUsingCasting | 108.66 ns |  2.245 ns |  2.672 ns | 108.25 ns |  0.0458 |  192  B   |\r\n|  ConvertUsingConvertToByte | 159.17 ns |  2.415 ns |  2.966 ns | 159.84 ns |  0.0458 |  192  B   |<\/pre>\n<p>From the benchmark results, we can deduce that for converting short strings, casting individual characters is the fastest method, closely followed by the <code>Convert.ToByte()<\/code> method. While for converting long strings, the <code>Encoding.GetBytes()<\/code> method emerges as the fastest, and the <code>Encoding.GetEncoding()<\/code> method closely follows.<\/p>\n<h2>Choosing the Most Suitable Method for Specific Scenarios<\/h2>\n<p>For most string-to-byte-array conversions in C#, the\u00a0<code>Encoding.GetBytes()<\/code> method is <strong>generally the most efficient and optimized choice<\/strong> because its internal optimizations and compiler assistance offer better performance.<\/p>\n<p>But in <strong>rare cases where complete accuracy is not critical or we are dealing with simple character sets<\/strong>, casting individual characters to bytes can suffice.<\/p>\n<p>The <code>Convert.ToByte()<\/code> method is a straightforward and readable alternative when working with small strings and single characters. <strong>It however might not handle all encodings correctly and could lead to data loss or corruption<\/strong>.<\/p>\n<p>The <code>Encoding.GetEncoding()<\/code> method shines in situations requiring specific encodings beyond the <code>ASCII<\/code> range, especially for multilingual texts, <strong>but might introduce a slight overhead when we use it for strings containing only<\/strong> <code>ASCII<\/code> <strong>characters<\/strong>.<\/p>\n<p>In summary, while the <code>Encoding.GetBytes()<\/code> method seems like the go-to choice for its efficiency and adaptability across a broad range of scenarios, <strong>understanding the strengths of alternative methods allows us to make informed decisions based on the unique characteristics of our data and the context of the application<\/strong>.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we discussed the importance of converting strings to byte arrays, explored various conversion methods, and emphasized the significance of selecting the appropriate encoding and conversion method based on the context of our application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll explore the concept of converting a string to a byte array in C#. We will also talk about why this conversion is necessary and explore the methods we can use for that. So let\u2019s begin. Why Conversion Is Necessary We\u2019ll most likely encounter the need to convert a string to a [&hellip;]<\/p>\n","protected":false},"author":92,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1057,12,1506],"tags":[1425,1811,1510,1364],"class_list":["post-103955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-array","category-csharp","category-string","tag-byte-array","tag-c","tag-c-strings","tag-encode","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Converting String to Byte Array in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we explore converting a string to a byte array in C#, why it is necessary, and the most common alternatives\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Converting String to Byte Array in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we explore converting a string to a byte array in C#, why it is necessary, and the most common alternatives\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-14T08:57:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Caleb Okechukwu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Caleb Okechukwu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/\"},\"author\":{\"name\":\"Caleb Okechukwu\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/479d6eb9d0bd7b5467c7d2e741a50355\"},\"headline\":\"Converting String to Byte Array in C#\",\"datePublished\":\"2024-01-14T08:57:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/\"},\"wordCount\":1204,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"Byte Array\",\"C#\",\"C# strings\",\"Encode\"],\"articleSection\":[\"Array\",\"C#\",\"String\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/\",\"url\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/\",\"name\":\"Converting String to Byte Array in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2024-01-14T08:57:38+00:00\",\"description\":\"In this article, we explore converting a string to a byte array in C#, why it is necessary, and the most common alternatives\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Converting String to Byte Array in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/479d6eb9d0bd7b5467c7d2e741a50355\",\"name\":\"Caleb Okechukwu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/caleb-profile-150x150.jpg\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/caleb-profile-150x150.jpg\",\"caption\":\"Caleb Okechukwu\"},\"description\":\"Caleb is a full-stack software engineer that specializes in seamlessly integerating the capabilities of .NET into the development of resilent, secure, and scalable applications. Additionally, Caleb likes to actively explore and experiment with innovative no-code tools to enhance efficiency and broaden the scope of his technical skills.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/igcaleb\"],\"url\":\"https:\/\/code-maze.com\/author\/calebokechukwy\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Converting String to Byte Array in C# - Code Maze","description":"In this article, we explore converting a string to a byte array in C#, why it is necessary, and the most common alternatives","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/","og_locale":"en_US","og_type":"article","og_title":"Converting String to Byte Array in C# - Code Maze","og_description":"In this article, we explore converting a string to a byte array in C#, why it is necessary, and the most common alternatives","og_url":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/","og_site_name":"Code Maze","article_published_time":"2024-01-14T08:57:38+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Caleb Okechukwu","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Caleb Okechukwu","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/"},"author":{"name":"Caleb Okechukwu","@id":"https:\/\/code-maze.com\/#\/schema\/person\/479d6eb9d0bd7b5467c7d2e741a50355"},"headline":"Converting String to Byte Array in C#","datePublished":"2024-01-14T08:57:38+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/"},"wordCount":1204,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["Byte Array","C#","C# strings","Encode"],"articleSection":["Array","C#","String"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/","url":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/","name":"Converting String to Byte Array in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2024-01-14T08:57:38+00:00","description":"In this article, we explore converting a string to a byte array in C#, why it is necessary, and the most common alternatives","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-converting-string-to-byte-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Converting String to Byte Array in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/479d6eb9d0bd7b5467c7d2e741a50355","name":"Caleb Okechukwu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/caleb-profile-150x150.jpg","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/caleb-profile-150x150.jpg","caption":"Caleb Okechukwu"},"description":"Caleb is a full-stack software engineer that specializes in seamlessly integerating the capabilities of .NET into the development of resilent, secure, and scalable applications. Additionally, Caleb likes to actively explore and experiment with innovative no-code tools to enhance efficiency and broaden the scope of his technical skills.","sameAs":["https:\/\/www.linkedin.com\/in\/igcaleb"],"url":"https:\/\/code-maze.com\/author\/calebokechukwy\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/103955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/92"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=103955"}],"version-history":[{"count":1,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/103955\/revisions"}],"predecessor-version":[{"id":103956,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/103955\/revisions\/103956"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=103955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=103955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=103955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}