{"id":326,"date":"2010-11-08T23:42:00","date_gmt":"2010-11-08T23:42:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/java-best-practices-char-to-byte-and-byte-to-char-conversions.html"},"modified":"2012-10-21T19:22:14","modified_gmt":"2012-10-21T19:22:14","slug":"java-best-practices-char-to-byte-and","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html","title":{"rendered":"Java Best Practices \u2013 Char to Byte and Byte to Char conversions"},"content":{"rendered":"<p>Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> performance tunning. Especially we will focus on how to handle character to byte and byte to character conversions efficiently when the default encoding is used. This article concludes with a performance comparison between two proposed custom approaches and two classic ones (the &#8220;<i>String.getBytes()<\/i>&#8221; and the <i>NIO<\/i> <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/nio\/ByteBuffer.html\">ByteBuffer<\/a>) for converting characters to bytes and vice \u2013 versa.<\/p>\n<p>All discussed topics are based on use cases derived from the development of mission critical, ultra high performance production systems for the telecommunication industry.<\/p>\n<p>Prior reading each section of this article it is highly recommended that you consult the relevant Java API documentation for detailed information and code samples.<\/p>\n<p>All tests are performed against a Sony Vaio with the following characteristics :<\/p>\n<ul>\n<li>System : openSUSE 11.1 (x86_64)<\/li>\n<li>Processor (CPU) : Intel(R) Core(TM)2 Duo CPU T6670 @ 2.20GHz<\/li>\n<li>Processor Speed : 1,200.00 MHz<\/li>\n<li>Total memory (RAM) : 2.8 GB<\/li>\n<li>Java : OpenJDK 1.6.0_0 64-Bit<\/li>\n<\/ul>\n<p>The following test configuration is applied :<\/p>\n<ul>\n<li>Concurrent worker Threads : 1<\/li>\n<li>Test repeats per worker Thread : 1000000<\/li>\n<li>Overall test runs : 100<\/li>\n<\/ul>\n<p><span class=\"Apple-style-span\" style=\"font-size: x-large\"><strong>Char to Byte and Byte to Char conversions<\/strong><\/span><br \/>\nCharacter to byte and byte to character conversions are considered common tasks among Java developers who are programming against a networking environment, manipulate streams of byte data, serialize <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> objects, implementing communication protocols etc. For that reason Java provides a handful of utilities that enable a developer to convert a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> (or a character array) to its byte array equivalent and vice versa.<\/p>\n<p>The \u201c<i>getBytes(charsetName)<\/i>\u201d operation of the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> class is probably the most commonly used &nbsp;method for converting a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> into its byte array equivalent. Since every character can be represented differently according to the encoding scheme used, its of no surprise that the aforementioned operation requires a \u201c<i>charsetName<\/i>\u201d in order to correctly convert the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> characters. If no \u201c<i>charsetName<\/i>\u201d is provided, the operation encodes the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> into a sequence of bytes using the platform&#8217;s default character set.<\/p>\n<p>Another \u201cclassic\u201d approach for converting a character array to its byte array equivalent is by using the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/nio\/ByteBuffer.html\">ByteBuffer<\/a> class of the NIO package. An example code snippet for the specific approach will be provided later on.<\/p>\n<p>Both the aforementioned approaches although very popular and indisputably easy to use and straightforward greatly lack in performance compared to more fine grained methods. Keep in mind that <strong><i>we are not converting between character encodings<\/i><\/strong>. For converting between character encodings you should stick with the \u201cclassic\u201d approaches using either the \u201c<i>String.getBytes(charsetName)<\/i>\u201d or the <i>NIO<\/i> framework methods and utilities.<\/p>\n<p>When all characters to be converted are ASCII characters, a proposed conversion method is the one shown below :<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush: java\">public static byte[] stringToBytesASCII(String str) {\r\n char[] buffer = str.toCharArray();\r\n byte[] b = new byte[buffer.length];\r\n for (int i = 0; i &lt; b.length; i++) {\r\n  b[i] = (byte) buffer[i];\r\n }\r\n return b;\r\n}\r\n<\/pre>\n<p>The resulted byte array is constructed by casting every character value to its byte equivalent since we know that all characters are in the ASCII range (0 \u2013 127) thus can occupy just one <span class=\"Apple-tab-span\"> <\/span>byte in size.<\/p>\n<p>Using the resulted byte array we can convert back to the original <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a>, by utilizing the \u201cclassic\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> constructor \u201c<i>new String(byte[])<\/i>\u201d<\/p>\n<p>For the default character encoding we can use the methods shown below to convert a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> to a byte array and vice \u2013 versa :<\/p>\n<pre class=\"brush: java\">public static byte[] stringToBytesUTFCustom(String str) {\r\n char[] buffer = str.toCharArray();\r\n byte[] b = new byte[buffer.length &lt;&lt; 1];\r\n for(int i = 0; i &lt; buffer.length; i++) {\r\n  int bpos = i &lt;&lt; 1;\r\n  b[bpos] = (byte) ((buffer[i]&amp;0xFF00)&gt;&gt;8);\r\n  b[bpos + 1] = (byte) (buffer[i]&amp;0x00FF);\r\n }\r\n return b;\r\n}\r\n<\/pre>\n<p>Every character type in Java occupies 2 bytes in size. For converting a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> to its byte array equivalent we convert every character of the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> to its 2 byte representation.<\/p>\n<p>Using the resulted byte array we can convert back to the original <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a>, by utilizing the method provided below :<\/p>\n<pre class=\"brush: java\">public static String bytesToStringUTFCustom(byte[] bytes) {\r\n char[] buffer = new char[bytes.length &gt;&gt; 1];\r\n for(int i = 0; i &lt; buffer.length; i++) {\r\n  int bpos = i &lt;&lt; 1;\r\n  char c = (char)(((bytes[bpos]&amp;0x00FF)&lt;&lt;8) + (bytes[bpos+1]&amp;0x00FF));\r\n  buffer[i] = c;\r\n }\r\n return new String(buffer);\r\n}\r\n<\/pre>\n<p>We construct every <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> character from its 2 byte representation. Using the resulted character &nbsp;array we can convert back to the original <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a>, by utilizing the \u201cclassic\u201d <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> constructor \u201c<i>new String(char[])<\/i>\u201d<\/p>\n<p>Last but not least we provide two example methods using the NIO package in order to convert a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> to its byte array equivalent and vice \u2013 versa :<\/p>\n<pre class=\"brush: java\">public static byte[] stringToBytesUTFNIO(String str) {\r\n char[] buffer = str.toCharArray();\r\n byte[] b = new byte[buffer.length &lt;&lt; 1];\r\n CharBuffer cBuffer = ByteBuffer.wrap(b).asCharBuffer();\r\n for(int i = 0; i &lt; buffer.length; i++)\r\n  cBuffer.put(buffer[i]);\r\n return b;\r\n}\r\n<\/pre>\n<pre class=\"brush: java\">public static String bytesToStringUTFNIO(byte[] bytes) {\r\n CharBuffer cBuffer = ByteBuffer.wrap(bytes).asCharBuffer();\r\n return cBuffer.toString();\r\n}\r\n<\/pre>\n<p>For the final part of this article we provide the performance comparison charts for the aforementioned <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> to byte array and byte array to <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> conversion approaches. We have tested all methods using the input string \u201c<strong><i>a test string<\/i><\/strong>\u201d.<\/p>\n<p>First the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> to byte array conversion performance comparison chart :<\/p>\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a href=\"http:\/\/3.bp.blogspot.com\/_tWwHCKnIbjs\/TNhnfKRmzuI\/AAAAAAAAACg\/oSTyA4hMOqA\/s1600\/chart1.png\"><img decoding=\"async\" border=\"0\" height=\"165\" src=\"http:\/\/3.bp.blogspot.com\/_tWwHCKnIbjs\/TNhnfKRmzuI\/AAAAAAAAACg\/oSTyA4hMOqA\/s400\/chart1.png\" width=\"400\" \/><\/a><\/div>\n<p>The horizontal axis represents the number of test runs and the vertical axis the average transactions per second (TPS) for each test run. Thus higher values are better. As expected, both \u201c<i>String.getBytes()<\/i>\u201d and \u201c<i>stringToBytesUTFNIO(String)<\/i>\u201d approaches performed poorly compared to the \u201c<i>stringToBytesASCII(String)<\/i>\u201d and \u201c<i>stringToBytesUTFCustom(String)<\/i>\u201d suggested approaches. As you can see, our proposed methods achieve almost 30% increase in TPS compared to the \u201cclassic\u201d methods.<\/p>\n<p>Lastly the byte array to <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> performance comparison chart :<\/p>\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a href=\"http:\/\/1.bp.blogspot.com\/_tWwHCKnIbjs\/TNhnm-9iV1I\/AAAAAAAAACk\/55bj7PI8rwA\/s1600\/chart2.png\"><img decoding=\"async\" border=\"0\" height=\"166\" src=\"http:\/\/1.bp.blogspot.com\/_tWwHCKnIbjs\/TNhnm-9iV1I\/AAAAAAAAACk\/55bj7PI8rwA\/s400\/chart2.png\" width=\"400\" \/><\/a><\/div>\n<p>The horizontal axis represents the number of test runs and the vertical axis the average transactions per second (TPS) for each test run. Thus higher values are better. As expected, both \u201c<i>new String(byte[])<\/i>\u201d and \u201c<i>bytesToStringUTFNIO(byte[])<\/i>\u201d approaches performed poorly compared to the \u201c<i>bytesToStringUTFCustom(byte[])<\/i>\u201d suggested approach. As you can see, our proposed method achieved almost 15% increase in TPS compared to the \u201c<i>new String(byte[])<\/i>\u201d method, and almost 30% increase in TPS compared to the \u201c<i>bytesToStringUTFNIO(byte[])<\/i>\u201d method.<\/p>\n<p>In conclusion, when you are dealing with character to byte or byte to character conversions and you do not intent to change the encoding used, you can achieve superior performance by utilizing custom \u2013 fine grained \u2013 methods rather than using the \u201cclassic\u201d ones provided by the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> class and the <i>NIO<\/i> package. Our proposed approach achieved an overall of 45% increase in performance compared to the \u201cclassic\u201d approaches when converting the test&nbsp;<a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> to its byte array equivalent and vice \u2013 versa.<\/p>\n<p>Happy coding<\/p>\n<p>Justin<\/p>\n<p>P.S.<\/p>\n<p>After taking into consideration the proposition from several of our readers to utilize the &#8220;<i>String.charAt(int)<\/i>&#8221; operation instead of using the &#8220;<i>String.toCharArray()<\/i>&#8221; so as to convert the&nbsp;<a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a>&nbsp;characters into bytes, I altered our proposed methods and re-executed the tests. As expected, further performance gains where achieved. In particular, an <strong><i>extra<\/i><\/strong> 13% average increase in TPS was recorded for the&nbsp;\u201c<i>stringToBytesASCII(String)<\/i>\u201d method and an <strong><i>extra<\/i><\/strong> 2% average increase in TPS was recorded for the \u201c<i>stringToBytesUTFCustom(String)<\/i>\u201d. So you should use the altered methods as they perform even better than the original ones. The updated methods are shown below :<\/p>\n<pre class=\"brush: java\">public static byte[] stringToBytesASCII(String str) {\r\n byte[] b = new byte[str.length()];\r\n for (int i = 0; i &lt; b.length; i++) {\r\n  b[i] = (byte) str.charAt(i);\r\n }\r\n return b;\r\n}\r\n<\/pre>\n<pre class=\"brush: java\">public static byte[] stringToBytesUTFCustom(String str) {\r\n byte[] b = new byte[str.length() &lt;&lt; 1];\r\n for(int i = 0; i &lt; str.length(); i++) {\r\n  char strChar = str.charAt(i);\r\n  int bpos = i &lt;&lt; 1;\r\n  b[bpos] = (byte) ((strChar&amp;0xFF00)&gt;&gt;8);\r\n  b[bpos + 1] = (byte) (strChar&amp;0x00FF); \r\n }\r\n return b;\r\n}\r\n<\/pre>\n<div style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px\"><strong><i>Related Articles :<\/i><\/strong><\/div>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/07\/java-best-practices-dateformat-in.html\">Java Best Practices \u2013 DateFormat in a Multithreading Environment<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/07\/java-best-practices-high-performance.html\">Java Best Practices \u2013 High performance Serialization<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html\">Java Best Practices \u2013 Vector vs ArrayList vs HashSet<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html\">Java Best Practices \u2013 String performance and Exact String Matching<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/09\/java-best-practices-queue-battle-and.html\">Java Best Practices \u2013 Queue battle and the Linked ConcurrentHashMap<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String performance tunning. Especially we will focus on how to handle character to byte and byte to character conversions efficiently when the default encoding is used. This article concludes with a performance comparison between &hellip;<\/p>\n","protected":false},"author":4,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[110,109,66,99],"class_list":["post-326","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-byte-array","tag-character","tag-java-best-practices","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Best Practices \u2013 Char to Byte and Byte to Char conversions - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Best Practices \u2013 Char to Byte and Byte to Char conversions - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2010-11-08T23:42:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T19:22:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Byron Kiourtzoglou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Byron Kiourtzoglou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html\"},\"author\":{\"name\":\"Byron Kiourtzoglou\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9c0c8d27141b068173953202dd9aebeb\"},\"headline\":\"Java Best Practices \u2013 Char to Byte and Byte to Char conversions\",\"datePublished\":\"2010-11-08T23:42:00+00:00\",\"dateModified\":\"2012-10-21T19:22:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html\"},\"wordCount\":1070,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Byte array\",\"Character\",\"Java Best Practices\",\"String\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html\",\"name\":\"Java Best Practices \u2013 Char to Byte and Byte to Char conversions - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2010-11-08T23:42:00+00:00\",\"dateModified\":\"2012-10-21T19:22:14+00:00\",\"description\":\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/11\\\/java-best-practices-char-to-byte-and.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Java Best Practices \u2013 Char to Byte and Byte to Char conversions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9c0c8d27141b068173953202dd9aebeb\",\"name\":\"Byron Kiourtzoglou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"caption\":\"Byron Kiourtzoglou\"},\"description\":\"Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications\\\/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.\",\"sameAs\":[\"https:\\\/\\\/www.pivotalgamers.com\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/byron-kiourtzoglou-530ab222\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/byron-kiourtzoglou\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Best Practices \u2013 Char to Byte and Byte to Char conversions - Java Code Geeks","description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String","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:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html","og_locale":"en_US","og_type":"article","og_title":"Java Best Practices \u2013 Char to Byte and Byte to Char conversions - Java Code Geeks","og_description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String","og_url":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2010-11-08T23:42:00+00:00","article_modified_time":"2012-10-21T19:22:14+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Byron Kiourtzoglou","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Byron Kiourtzoglou","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html"},"author":{"name":"Byron Kiourtzoglou","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9c0c8d27141b068173953202dd9aebeb"},"headline":"Java Best Practices \u2013 Char to Byte and Byte to Char conversions","datePublished":"2010-11-08T23:42:00+00:00","dateModified":"2012-10-21T19:22:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html"},"wordCount":1070,"commentCount":5,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Byte array","Character","Java Best Practices","String"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html","url":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html","name":"Java Best Practices \u2013 Char to Byte and Byte to Char conversions - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2010-11-08T23:42:00+00:00","dateModified":"2012-10-21T19:22:14+00:00","description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Java Best Practices \u2013 Char to Byte and Byte to Char conversions"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9c0c8d27141b068173953202dd9aebeb","name":"Byron Kiourtzoglou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","caption":"Byron Kiourtzoglou"},"description":"Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications\/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.","sameAs":["https:\/\/www.pivotalgamers.com\/","https:\/\/www.linkedin.com\/in\/byron-kiourtzoglou-530ab222"],"url":"https:\/\/www.javacodegeeks.com\/author\/byron-kiourtzoglou"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=326"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/326\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}