{"id":46,"date":"2017-10-01T23:49:56","date_gmt":"2017-10-01T14:49:56","guid":{"rendered":"http:\/\/www.sciement.com\/tech-blog\/?p=46"},"modified":"2017-10-01T23:54:42","modified_gmt":"2017-10-01T14:54:42","slug":"signed16bitintbinaryreadwrite","status":"publish","type":"post","link":"https:\/\/www.sciement.com\/tech-blog\/python\/signed16bitintbinaryreadwrite\/","title":{"rendered":"[Python][C++]\u7b26\u53f7\u4ed816\u30d3\u30c3\u30c8\u6574\u6570\u306e\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u66f8\u304d\u3059\u308b"},"content":{"rendered":"<p>DICOM\u5f62\u5f0f\u306e\u533b\u7528\u753b\u50cf\u3092\u7528\u3044\u3066\u753b\u50cf\u51e6\u7406\u306a\u3069\u3092\u884c\u3044\u305f\u3044\u5834\u5408\u3001\u5f53\u305f\u308a\u524d\u3067\u3059\u304cDICOM\u5f62\u5f0f\u306e\u533b\u7528\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307e\u306a\u3051\u308c\u3070\u3044\u3051\u307e\u305b\u3093\u3002<br \/>\n\u4e16\u306e\u4e2d\u306b\u306fDICOM\u5f62\u5f0f\u306e\u533b\u7528\u753b\u50cf\u306e\u8aad\u307f\u66f8\u304d\u304c\u51fa\u6765\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u8af8\u3005\u3042\u308a\u307e\u3059\u304c\u3001\u4f8b\u3048\u3070ImageJ\u3067\u3061\u3087\u3063\u3068\u3060\u3051\u52a0\u5de5\u3057\u3066\u304b\u3089\u30b3\u30fc\u30c9\u3067\u8272\u3005\u8a66\u3057\u305f\u3044\u3001\u3068\u3044\u3046\u3088\u3046\u306a\u3068\u304d\u306b\u306f\u3001\u308f\u3056\u308f\u3056DICOM\u5f62\u5f0f\u306e\u753b\u50cf\u3092\u3082\u3046\u4e00\u5ea6\u4f5c\u308b\u3068\u9762\u5012\u306a\u306e\u3067\u3001\u5024\u3060\u3051\u304c\u76f4\u66f8\u304d\u3055\u308c\u305fRaw\u30d5\u30a1\u30a4\u30eb\u3067Export\u3059\u308b\u3053\u3068\u3082\u305d\u3053\u305d\u3053\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\uff08\u79c1\u3060\u3051\uff1f\uff09\u3002<br \/>\n\u305d\u3057\u3066\u4f8b\u3048\u3070CT\u753b\u50cf\u306e\u5834\u5408\u306b\u306f\u5404\u30d4\u30af\u30bb\u30eb\u306eCT\u5024\u304c\u8ca0\u306e\u6574\u6570\u306e\u3068\u304d\u3082\u3042\u308a\u307e\u3059\u306e\u3067\u3001Raw\u30d5\u30a1\u30a4\u30eb\u306f\u7b26\u53f7\u4ed816\u30d3\u30c3\u30c8\u6574\u6570\u3067\u51fa\u529b\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u308f\u3051\u3067\u3001\u305d\u3093\u306a<strong>\u7b26\u53f7\u4ed816\u30d3\u30c3\u30c8\u6574\u6570\u306e\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u66f8\u304d\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u826f\u3044\u306e\u304b<\/strong>\u3001\u3068\u3044\u3046\u304a\u8a71\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3068\u3057\u3066\u30014\u00d74\u306e\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5024\u3092\u5404\u30d4\u30af\u30bb\u30eb\u306b\u6301\u3064\u753b\u50cf\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.sciement.com\/tech-blog\/wp-content\/uploads\/2017\/10\/rawtest4_4_detail.png\" alt=\"\" width=\"512\" height=\"512\" class=\"alignnone size-full wp-image-48\" srcset=\"https:\/\/www.sciement.com\/tech-blog\/wp-content\/uploads\/2017\/10\/rawtest4_4_detail.png 512w, https:\/\/www.sciement.com\/tech-blog\/wp-content\/uploads\/2017\/10\/rawtest4_4_detail-150x150.png 150w, https:\/\/www.sciement.com\/tech-blog\/wp-content\/uploads\/2017\/10\/rawtest4_4_detail-300x300.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<p>\u753b\u50cf\u5185\u306e\u6570\u5b57\u306f\u3001\u5404\u30d4\u30af\u30bb\u30eb\u306e\u5024\u3067\u3001\u666e\u901a\u306e10\u9032\u6570\u8868\u793a\u3068\u3001\u5bfe\u5fdc\u3059\u308b16\u9032\u6570\u8868\u793a\u3067\u3059\u3002<br \/>\n\u3053\u308c\u3092ImageJ\u3067Raw\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u51fa\u529b\u3057\u3066\u30d0\u30a4\u30ca\u30ea\u30a8\u30c7\u30a3\u30bf\u3067\u898b\u3066\u307f\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote><p>ff c8 ff ed 01 01 07 06 00 e8 07 3f 0d 6a 10 cf 0c d3 11 20 10 91 0d a3 10 ed 0d dc 0c 4b 0b f6<\/p><\/blockquote>\n<p>16\u9032\u6570\u8868\u793a\u3067\u9806\u756a\u306b\u4e26\u3093\u3067\u3044\u308b\u3060\u3051\u306e\u5358\u7d14\u306a\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u52b9\u7387\u3088\u304f\u8aad\u307f\u8fbc\u3093\u3067\u3001\u914d\u5217\u306b\u4ee3\u5165\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u307e\u3059\u3002\u307e\u305a\u306fPython\u7248\u3002<\/p>\n<pre class=\"lang:python decode:true \" >import os\r\nimport sys\r\nimport array\r\n\r\n\r\ndef read_signed_16bit_int_binary_sequence(file_full_path):\r\n    file_size = os.stat(file_full_path).st_size  # returns byte size\r\n    number_of_pixels = file_size \/ 2  # 16 bit is 2 bytes.\r\n\r\n    file = open(file_full_path, 'rb')\r\n    image_value = array.array('h')  # 'h': signed short (2 byte = 16 bit)\r\n    image_value.fromfile(file, number_of_pixels)\r\n    file.close()\r\n\r\n    if sys.byteorder == 'little':\r\n        image_value.byteswap()\r\n\r\n    return image_value\r\n\r\n\r\ndef write_signed_16bit_int_binary_sequence(file_full_path, value_array):\r\n    file = open(file_full_path, 'wb')\r\n\r\n    if sys.byteorder == 'little':\r\n        value_array.byteswap()\r\n\r\n    value_array.tofile(file)\r\n    file.close()\r\n\r\n    if sys.byteorder == 'little':\r\n        value_array.byteswap()\r\n\r\n\r\n# read\r\ninput_file_path = 'test.raw'\r\npixel_values = read_signed_16bit_int_binary_sequence(input_file_path)\r\n\r\n# write\r\noutput_file_path = 'test_output.raw'\r\nwrite_signed_16bit_int_binary_sequence(output_file_path, pixel_values)<\/pre>\n<p>\u552f\u4e00\u306e\u6b63\u89e3\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u79c1\u304c\u6700\u3082\u52b9\u7387\u7684\u3060\u3068\u601d\u3063\u305f\u65b9\u6cd5\u306fPython\u6a19\u6e96\u3067\u3042\u308b<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" >array.array(&#8216;h&#8217;)<\/span><br \/>\n\u3092\u7528\u3044\u308b\u65b9\u6cd5\u3067\u3059\u3002\u3053\u308c\u3092\u7528\u3044\u308b\u3068\u3001<\/p>\n<pre class=\"start-line:11 lang:python decode:true \" >    image_value = array.array('h')  # 'h': signed short (2 byte = 16 bit)\r\n    image_value.fromfile(file, number_of_pixels)\r\n<\/pre>\n<p>\u306e2\u884c\u3060\u3051\u3067\u3001\u7b26\u53f7\u4ed816\u30d3\u30c3\u30c8\u6574\u6570\u304c\u4e26\u3093\u3060\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u3092\u4e00\u6c17\u306b\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<br \/>\n\u2026\u3060\u304c\u3057\u304b\u3057\uff01\u305d\u306e\u5f8c\u306e<\/p>\n<pre class=\"start-line:15 lang:python decode:true \" >    if sys.byteorder == 'little':\r\n        image_value.byteswap()<\/pre>\n<p>\u3053\u306e2\u884c\u3092\u5fd8\u308c\u308b\u3068\u3001\u30de\u30b7\u30f3\u306b\u3088\u3063\u3066\u306f\u5024\u304c\u30e1\u30c1\u30e3\u30af\u30c1\u30e3\u306b\u306a\u308a\u307e\u3059\u3002Windows\u30de\u30b7\u30f3\u3067\u306f\uff08\u203b\u4f8b\u5916\u306f\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\uff09\u5177\u4f53\u7684\u306b\u306f\u3053\u3046\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.sciement.com\/tech-blog\/wp-content\/uploads\/2017\/10\/rawtest4_4_detail_incorrect.png\" alt=\"\" width=\"512\" height=\"512\" class=\"alignnone size-full wp-image-49\" srcset=\"https:\/\/www.sciement.com\/tech-blog\/wp-content\/uploads\/2017\/10\/rawtest4_4_detail_incorrect.png 512w, https:\/\/www.sciement.com\/tech-blog\/wp-content\/uploads\/2017\/10\/rawtest4_4_detail_incorrect-150x150.png 150w, https:\/\/www.sciement.com\/tech-blog\/wp-content\/uploads\/2017\/10\/rawtest4_4_detail_incorrect-300x300.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<p>\u6700\u521d\u306e\u56f3\u306816\u9032\u6570\u8868\u793a\u306f\u540c\u3058\u306a\u306e\u306b\u3001\u5bfe\u5fdc\u3059\u308b10\u9032\u6570\u8868\u793a\u304c\u30e1\u30c1\u30e3\u30af\u30c1\u30e3\u3067\u3059\u3002<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" >array.array<\/span><br \/>\n\u3092\u7528\u3044\u3066\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3068\u3001\u305d\u306e\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304b\u308c\u3066\u3044\u308b\u305d\u3063\u304f\u308a\u305d\u306e\u307e\u307e\u306e\u72b6\u614b\u304c\u3053\u306earray.array\u306b\u66f8\u304d\u8fbc\u307e\u308c\u307e\u3059\u3002<br \/>\n\u3044\u307e\u3001<\/p>\n<pre class=\"start-line:11 lang:python decode:true \" >    image_value = array.array('h')  # 'h': signed short (2 byte = 16 bit)<\/pre>\n<p>\u3068\u7b26\u53f7\u4ed8short\uff08Python\u3067\u306f\u3053\u308c\u304c\u7b26\u53f7\u4ed816\u30d3\u30c3\u30c8\u6574\u6570\u306b\u5bfe\u5fdc\uff09\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u308c\u306f2\u30d0\u30a4\u30c8\u306a\u306e\u3067\u300116\u9032\u65702\u3064\u305a\u3064\u304c1\u3064\u306e\u6574\u6570\u3068\u3057\u3066\u89e3\u91c8\u3055\u308c\u307e\u3059\u3002<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" >image_value[0]<\/span><br \/>\n\u306f\u3001\u6700\u521d\u306e16\u9032\u65702\u3064\u3001\u3064\u307e\u308a<\/p>\n<blockquote><p>ff c8<\/p><\/blockquote>\n<p>\u304b\u3089\u5024\u304c\u4f5c\u3089\u308c\u307e\u3059\u304c\u3001\u30e1\u30e2\u30ea\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u901a\u308a\u306b\u89e3\u91c8\u3055\u308c\u307e\u3059\u3002<br \/>\n\u3053\u3053\u3067\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u306e\u554f\u984c\u304c\u767a\u751f\u3057\u307e\u3059\u3002Windows\u306f\u30ea\u30c8\u30eb\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u306a\u306e\u3067\u3001\u30e1\u30e2\u30ea\u4e0a\u306b<\/p>\n<blockquote><p>ff c8<\/p><\/blockquote>\n<p>\u3068\u66f8\u304b\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u89e3\u91c8\u3068\u3057\u3066\u306f<\/p>\n<blockquote><p>ffc8<\/p><\/blockquote>\n<p>\u3067\u306f\u306a\u304f\u3001\u9806\u756a\u304c\u9006\u306b\u306a\u3063\u3066<\/p>\n<blockquote><p>c8ff<\/p><\/blockquote>\n<p>\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u306a\u306e\u3067\u3001\u30e1\u30e2\u30ea\u4e0a\u306b\u914d\u7f6e\u3059\u308b\u969b\u306b\u306f\u5404\u7b26\u53f7\u4ed816\u30d3\u30c3\u30c8\u6574\u6570\u30671\u30d0\u30a4\u30c8\u76ee\u30682\u30d0\u30a4\u30c8\u76ee\u3068\u3092\u5165\u308c\u66ff\u3048\u306a\u3051\u308c\u3070\u3044\u3051\u307e\u305b\u3093\u3002<br \/>\n\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u8d70\u3089\u305b\u3066\u3044\u308b\u30de\u30b7\u30f3\u304c\u30ea\u30c8\u30eb\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u306a\u306e\u304b\u30d3\u30c3\u30b0\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u306a\u306e\u304b\u306f\u3001Python\u3067\u306f<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" >sys.byteorder<\/span><br \/>\n\u3068\u3044\u3046\u4fbf\u5229\u306a\u6a5f\u80fd\u3067\u5224\u5b9a\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u3001\u30d0\u30a4\u30c8\u306e\u5165\u308c\u66ff\u3048\u3082array\u306b\u5bfe\u3057\u3066<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" >byteswap()<\/span><br \/>\n\u3092\u884c\u3046\u3068\u52dd\u624b\u306b\u3084\u3063\u3066\u304f\u308c\u308b\u3068\u3044\u3046\u4fbf\u5229\u306a\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306b\u66f8\u304d\u8fbc\u307f\u3067\u3059\u304c\u3001<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" >array.array<\/span><br \/>\n\u306e\u5185\u5bb9\u3092\u5168\u90e8\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u51fa\u529b\u3059\u308b\u305f\u3081\u306e\u4fbf\u5229\u306a\u6a5f\u80fd\u304c\u3053\u308c\u307e\u305f\u6700\u521d\u304b\u3089\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001<\/p>\n<pre class=\"start-line:27 lang:python decode:true \" >    value_array.tofile(file)<\/pre>\n<p>\u306e1\u884c\u3060\u3051\u3067\u3059\u3002\u7c21\u5358\u3067\u3059\u306d\u3002<br \/>\n\u305f\u3060\u3057\u3001\u3053\u3053\u3067\u3082\u30e1\u30e2\u30ea\u306e\u914d\u7f6e\u901a\u308a\u306b\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u8fbc\u307e\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u306e\u3067\u3001\u3082\u3068\u3082\u3068\u306e\u30d5\u30a1\u30a4\u30eb\u3068\u540c\u3058\u306b\u3059\u308b\u305f\u3081\u306b\u306f\u3001<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" >byteswap()<\/span><br \/>\n\u3092\u7528\u3044\u3066\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u3092\u30d3\u30c3\u30b0\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u8868\u8a18\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u3092\u66f8\u304d\u8fbc\u3093\u3060\u5f8c\u306b\u518d\u3073<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" >byteswap()<\/span><br \/>\n\u3092\u884c\u3063\u3066\u3001\u5143\u306b\u623b\u3059\u306e\u3092\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>Python\u3067\u306f\u3053\u306e\u3088\u3046\u306b\u3001\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u306e\u5224\u5b9a\u3084byteswap\u3092\u7c21\u5358\u306b\u884c\u3046\u3053\u3068\u304c\u51fa\u6765\u3066\u4fbf\u5229\u3067\u3059\u3002<br \/>\n\u304c\u3001<strong>Python\u306ffor\u6587\u304c\u3068\u3066\u3082\u9045\u3044<\/strong>\u305f\u3081\u3001\u753b\u50cf\u5168\u4f53\u306b\u753b\u50cf\u30d5\u30a3\u30eb\u30bf\u3092\u304b\u3051\u305f\u308a\u3059\u308b\u3088\u3046\u306a\u5834\u5408\u306b\u306f\u99ac\u9e7f\u306b\u306a\u3089\u306a\u3044\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002<br \/>\n256\u00d7256\u306e\u753b\u50cf\u3067\u3082\u3001\u30d4\u30af\u30bb\u30eb\u6570\u306f256\u00d7256 = 65,536\u3082\u3042\u308a\u307e\u3059\u306e\u3067\u3001for\u6587\u3092\u56de\u3059\u3068\u7d50\u69cb\u30a4\u30e9\u30a4\u30e9\u3057\u3066\u304d\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/numba.pydata.org\/\" rel=\"noopener\" target=\"_blank\"><strong>Numba<\/strong><\/a>\u306a\u3069\u3092\u4e0a\u624b\u304f\u7528\u3044\u308c\u3070\u9ad8\u901f\u5316\u51fa\u6765\u308b\u306e\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u2026\u3002<\/p>\n<p>\u3068\u8a00\u3046\u308f\u3051\u3067\u3001C++\u3067\u3082\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:c++ decode:true \" >#include&lt;iostream&gt;\r\n#include&lt;fstream&gt;\r\n#include&lt;vector&gt;\r\n#include&lt;string&gt;\r\n\r\nbool check_is_little_endian() {\r\n\tconst int16_t value = 1;\r\n\treturn (*(char*)&amp;value) ? true : false;\r\n}\r\n\r\nconst size_t get_file_byte_size(std::ifstream&amp; file) {\r\n\tfile.seekg(0, std::ios::end);\r\n\tconst size_t file_size = (size_t)file.tellg();\r\n\tfile.seekg(0, std::ios::beg);\r\n\t\r\n\treturn file_size;\r\n}\r\n\r\nvoid convert_signed_16bit_int_endian(std::vector&lt;int16_t&gt;* target_vector) {\r\n\tfor (auto&amp; value : *target_vector) {\r\n\t\tvalue = (value &lt;&lt; 8) | ((value &gt;&gt; 8) &amp; 0xFF); \/\/ The most left value does not change even if bit-shifted, so the mask by 0xFF is necessary.\r\n\t}\r\n}\r\n\r\nbool read_signed_16bit_int_binary_sequence(const std::string&amp; file_full_path, std::vector&lt;int16_t&gt;* target_vector) {\r\n\tstd::ifstream file(file_full_path, std::ios::in | std::ios::binary);\r\n\tif (!file) {\r\n\t\tstd::cout &lt;&lt; \"Error: The file path is incorrect. There is no file.\" &lt;&lt; std::endl;\r\n\t\treturn false;\r\n\t}\r\n\tconst size_t file_size = get_file_byte_size(file);\r\n\ttarget_vector-&gt;clear();\r\n\ttarget_vector-&gt;resize(file_size \/ 2); \/\/ 16 bit is 2 bytes.\r\n\r\n\tfile.read((char*)target_vector-&gt;data(), file_size);\r\n\tif (check_is_little_endian()) {\r\n\t\tconvert_signed_16bit_int_endian(target_vector);\r\n\t}\r\n\tfile.close();\r\n\treturn true;\r\n}\r\n\r\nbool write_signed_16bit_int_binary_sequence(const std::string&amp; file_full_path, const std::vector&lt;int16_t&gt;&amp; target_vector) {\r\n\tstd::ofstream file(file_full_path, std::ios::out | std::ios::binary);\r\n\tif (!file) {\r\n\t\tstd::cout &lt;&lt; \"Error: The file to write could not be opend.\" &lt;&lt; std::endl;\r\n\t\treturn false;\r\n\t}\r\n\tconst size_t file_size = target_vector.size() * 2; \/\/ int16_t (16 bit) is 2 byte.\r\n\tif (check_is_little_endian()) {\r\n\t\tstd::vector&lt;int16_t&gt; temp_vector = target_vector; \/\/ deep copy.\r\n\t\tconvert_signed_16bit_int_endian(&amp;temp_vector);\r\n\t\tfile.write((char*)temp_vector.data(), file_size);\r\n\t}\r\n\telse {\r\n\t\tfile.write((char*)target_vector.data(), file_size);\r\n\t}\r\n\r\n\tfile.close();\r\n\treturn true;\r\n}\r\n\r\nint main()\r\n{\r\n\tconst std::string input_file_path = \"test.raw\";\r\n\tstd::vector&lt;int16_t&gt; pixel_values;\r\n\r\n\tread_signed_16bit_int_binary_sequence(input_file_path, &amp;pixel_values);\r\n\r\n\tconst std::string output_file_path = \"test_output.raw\";\r\n\twrite_signed_16bit_int_binary_sequence(output_file_path, pixel_values);\r\n\r\n\treturn 0;\r\n}<\/pre>\n<p>Python\u3068\u6bd4\u3079\u308b\u3068\u884c\u6570\u304c2\u500d\u5f31\u3082\u5897\u3048\u3066\u3057\u307e\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u3001Python\u3067\u306f\u6a19\u6e96\u3067\u5099\u308f\u3063\u3066\u3044\u305f\u6a5f\u80fd\u306e\u3044\u304f\u3064\u304b\u3092\u81ea\u5206\u3067\u5b9f\u88c5\u3057\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u3044\u3053\u3068\u3082\u5f71\u97ff\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3068\u306f\u8a00\u3048\u305d\u3053\u307e\u3067\u5927\u5909\u306a\u3053\u3068\u306b\u306f\u306a\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002<br \/>\nint16_t\u578b\u306e\u914d\u5217\u3067\u3042\u308bstd::vector&lt;int16_t&gt;\u306b\u4e00\u6c17\u306b\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u3092\u7a81\u3063\u8fbc\u3080\u305f\u3081\u306b\u306f<\/p>\n<pre class=\"start-line:35 lang:c++ decode:true \" >\tfile.read((char*)target_vector-&gt;data(), file_size);<\/pre>\n<p>\u306e1\u884c\u3067\u4e8b\u8db3\u308a\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u3053\u3053\u3067\u3082\u3084\u306f\u308a\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u554f\u984c\u304c\u767a\u751f\u3057\u307e\u3059\u306e\u3067\u3001\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u3092\u5224\u5b9a\u3057\u3066\u3001\u30ea\u30c8\u30eb\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u3067\u3042\u308c\u3070byteswap\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3067\u3001\u3053\u306e\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u5224\u5b9a\u3067\u3059\u304c\u3001\u73fe\u5728\u306eC++\u306e\u6a19\u6e96\u6a5f\u80fd\u306b\u306f\u7121\u3044\u3088\u3046\u3067\u3059\u3002\u3057\u304b\u3057\u5224\u5b9a\u304c\u5fc5\u8981\u306a\u3068\u304d\u306f\u5c11\u306a\u304b\u3089\u305a\u3042\u308a\u307e\u3059\u306e\u3067\u3001\u8ab0\u304b\u304c\u3046\u307e\u3044\u3084\u308a\u65b9\u3092\u898b\u3064\u3051\u3066\u3044\u308b\u306f\u305a\u3067\u3059\u3002<br \/>\n\u691c\u7d22\u3059\u308b\u3068\u7c21\u5358\u306b\u898b\u3064\u304b\u308a\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"http:\/\/torasukenote.blog120.fc2.com\/blog-entry-105.html\" rel=\"noopener\" target=\"_blank\"><strong>\u521d\u5fc3\u8005\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u4f53\u9a13\u8a18\u3000\u30d3\u30c3\u30b0\uff1f or \u30ea\u30c8\u30eb\uff1f\uff08\u30a8\u30f3\u30c7\u30a3\u30a2\u30f3\u306e\u5224\u5b9a\uff09\u3010C\u8a00\u8a9e\u3011<\/strong><\/a><\/p>\n<p>\u95a2\u6570\u3068\u3057\u3066\u5b9f\u88c5\u3059\u308c\u3070\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"start-line:6 lang:c++ decode:true \" >bool check_is_little_endian() {\r\n\tconst int16_t value = 1;\r\n\treturn (*(char*)&amp;value) ? true : false;\r\n}<\/pre>\n<p>\u3057\u304b\u3057byteswap\u306f\u3061\u3087\u3063\u3068\u30cf\u30de\u308a\u307e\u3057\u305f\u2026\u3002<br \/>\n\u4e0a\u4f4d8\u30d3\u30c3\u30c8\u3068\u4e0b\u4f4d8\u30d3\u30c3\u30c8\u3068\u3092\u5165\u308c\u66ff\u3048\u308c\u3070\u826f\u3044\u306e\u3067\u3001<\/p>\n<pre class=\"start-line:21 lang:c++ decode:true \" >\t\tvalue = (value &lt;&lt; 8) | (value &gt;&gt; 8);<\/pre>\n<p>\u3067OK!\u2026\u304b\u3068\u601d\u3044\u304d\u3084\u3001\u3053\u308c\u3067\u306f\u30c0\u30e1\u3067\u3057\u305f\u3002\u6b63\u3057\u304f\u306f<\/p>\n<pre class=\"start-line:21 lang:c++ decode:true \" >\t\tvalue = (value &lt;&lt; 8) | ((value &gt;&gt; 8) &amp; 0xFF); \/\/ The most left value does not change even if bit-shifted, so the mask by 0xFF is necessary.\r\n<\/pre>\n<p>\u3067\u3059\u3002<a href=\"https:\/\/stackoverflow.com\/questions\/2182002\/convert-big-endian-to-little-endian-in-c-without-using-provided-func\" rel=\"noopener\" target=\"_blank\"><strong>Stack Overflow<\/strong><\/a>\u306b\u3042\u308a\u307e\u3057\u305f\u3002<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" > &amp; 0xFF<\/span><br \/>\n\u3068\u306f\u4f55\u305e\u3084\uff1f\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001<br \/>\n<a href=\"http:\/\/www.cc.kyoto-su.ac.jp\/~yamada\/pB\/bit.html\" rel=\"noopener\" target=\"_blank\"><strong>\u6574\u6570\u578b\u3068\u30d3\u30c3\u30c8\u64cd\u4f5c<\/strong><\/a><br \/>\n\u3092\u8aad\u3093\u3067\u89e3\u6c7a\u3057\u307e\u3057\u305f\u3002\u7b26\u53f7\u4ed8\u6574\u6570\u306e\u30d3\u30c3\u30c8\u6f14\u7b97\u3067\u306f\u3001\u30d3\u30c3\u30c8\u3092\u305a\u3089\u3057\u3066\u3082\u7b26\u53f7\u306e\u90e8\u5206\u306e\u5024\u306f\u305d\u306e\u307e\u307e\u6b8b\u308b\u306e\u3067\u3001\u4e0a\u4f4d8\u30d3\u30c3\u30c8\u30928\u30d3\u30c3\u30c8\u53f3\u306b\u305a\u3089\u3057\u305f\u5834\u5408\u306f\u3001\u305d\u306e\u7d50\u679c\u306e16\u30d3\u30c3\u30c8\u306e\u3046\u3061\u306e\u4e00\u756a\u5de6\u5074\u306e\u30d3\u30c3\u30c8\u3092\u5e38\u306b0\u306b\u3057\u306a\u3051\u308c\u3070\u3044\u3051\u307e\u305b\u3093\u3002<br \/>\n\u305d\u306e\u305f\u3081\u306e<br \/>\n<span class=\"lang:python decode:true  crayon-inline \" > &amp; 0xFF<\/span><br \/>\n\u3067\u3059\u3002<br \/>\n\u305d\u3053\u3055\u3048\u30af\u30ea\u30a2\u3059\u308c\u3070\u3001\u3042\u3068\u306f\u66f8\u304d\u8fbc\u307f\u306e\u5834\u5408\u3082\u542b\u3081\u3066Python\u3068\u57fa\u672c\u7684\u306b\u306f\u4e00\u7dd2\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3067Python\u3067\u3082C++\u3067\u3082\u7b26\u53f7\u4ed816\u30d3\u30c3\u30c8\u6574\u6570\u306e\u30d0\u30a4\u30ca\u30ea\u30d5\u30a1\u30a4\u30eb\u3092\u81ea\u7531\u306b\u64cd\u4f5c\u3067\u304d\u307e\u3059\u306d\u3002<br \/>\n\u3082\u3063\u3068\u7c21\u5358\u306a\u65b9\u6cd5\u304c\u3042\u308c\u3070\u305c\u3072\u77e5\u308a\u305f\u3044\u3067\u3059\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DICOM\u5f62\u5f0f\u306e\u533b\u7528\u753b\u50cf\u3092\u7528\u3044\u3066\u753b\u50cf\u51e6\u7406\u306a\u3069\u3092\u884c\u3044\u305f\u3044\u5834\u5408\u3001\u5f53\u305f\u308a\u524d\u3067\u3059\u304cDICOM\u5f62\u5f0f\u306e\u533b\u7528\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307e\u306a\u3051\u308c\u3070\u3044\u3051\u307e\u305b\u3093\u3002 \u4e16\u306e\u4e2d\u306b\u306fDICOM\u5f62\u5f0f\u306e\u533b\u7528\u753b\u50cf\u306e\u8aad\u307f\u66f8\u304d\u304c\u51fa\u6765\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u8af8\u3005\u3042\u308a\u307e\u3059\u304c\u3001\u4f8b\u3048\u3070 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-c","category-python"],"_links":{"self":[{"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":13,"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/posts\/46\/revisions\/62"}],"wp:attachment":[{"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sciement.com\/tech-blog\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}