<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>http://mochiuwiki.e2.valueserver.jp/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%E3%81%AE%E5%BF%9C%E7%94%A8_-_%E9%80%B2%E6%95%B0%E3%81%AE%E5%A4%89%E6%8F%9B</id>
	<title>C++の応用 - 進数の変換 - 版の履歴</title>
	<link rel="self" type="application/atom+xml" href="http://mochiuwiki.e2.valueserver.jp/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%E3%81%AE%E5%BF%9C%E7%94%A8_-_%E9%80%B2%E6%95%B0%E3%81%AE%E5%A4%89%E6%8F%9B"/>
	<link rel="alternate" type="text/html" href="http://mochiuwiki.e2.valueserver.jp/index.php?title=C%2B%2B%E3%81%AE%E5%BF%9C%E7%94%A8_-_%E9%80%B2%E6%95%B0%E3%81%AE%E5%A4%89%E6%8F%9B&amp;action=history"/>
	<updated>2026-09-01T03:49:47Z</updated>
	<subtitle>このウィキのこのページに関する変更履歴</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>http://mochiuwiki.e2.valueserver.jp/index.php?title=C%2B%2B%E3%81%AE%E5%BF%9C%E7%94%A8_-_%E9%80%B2%E6%95%B0%E3%81%AE%E5%A4%89%E6%8F%9B&amp;diff=5487&amp;oldid=prev</id>
		<title>Wiki: /* 2進数から16進数への変換 */</title>
		<link rel="alternate" type="text/html" href="http://mochiuwiki.e2.valueserver.jp/index.php?title=C%2B%2B%E3%81%AE%E5%BF%9C%E7%94%A8_-_%E9%80%B2%E6%95%B0%E3%81%AE%E5%A4%89%E6%8F%9B&amp;diff=5487&amp;oldid=prev"/>
		<updated>2022-05-09T07:07:56Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;2進数から16進数への変換&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ja&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← 古い版&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2022年5月9日 (月) 16:07時点における版&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l35&quot;&gt;35行目:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;35行目:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 2進数から16進数への変換 ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 2進数から16進数への変換 ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  std::string &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;btoh&lt;/del&gt;(const std::string &amp;amp;bin)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  std::string &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;BinToHex&lt;/ins&gt;(const std::string &amp;amp;bin)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  {&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     std::string hex;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     std::string hex;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key mochiuwiki:diff:1.41:old-5486:rev-5487:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Wiki</name></author>
	</entry>
	<entry>
		<id>http://mochiuwiki.e2.valueserver.jp/index.php?title=C%2B%2B%E3%81%AE%E5%BF%9C%E7%94%A8_-_%E9%80%B2%E6%95%B0%E3%81%AE%E5%A4%89%E6%8F%9B&amp;diff=5486&amp;oldid=prev</id>
		<title>Wiki: ページの作成:「== 概要 == C++において、進数の変換を行う手順を記載する。&lt;br&gt; &lt;br&gt;&lt;br&gt;  == 16進数から2進数への変換 ==  &lt;syntaxhighlight lang=&quot;c++&quot;&gt;  std::string HexToBin(const std::string &amp;hex)  {     std::string bin;     for(int i = 0; i &lt; hex.length(); i++)     {        if (hex[i] == &#039;0&#039;) bin += &quot;0000&quot;;        else if(hex[i] == &#039;1&#039;) bin += &quot;0001&quot;;        else if(hex[i] == &#039;2&#039;) bin += &quot;0010&quot;;        else if(hex[i] == &#039;3&#039;) bin += &quot;0011&quot;;        else…」</title>
		<link rel="alternate" type="text/html" href="http://mochiuwiki.e2.valueserver.jp/index.php?title=C%2B%2B%E3%81%AE%E5%BF%9C%E7%94%A8_-_%E9%80%B2%E6%95%B0%E3%81%AE%E5%A4%89%E6%8F%9B&amp;diff=5486&amp;oldid=prev"/>
		<updated>2022-05-09T07:06:27Z</updated>

		<summary type="html">&lt;p&gt;ページの作成:「== 概要 == C++において、進数の変換を行う手順を記載する。&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;  == 16進数から2進数への変換 ==  &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;  std::string HexToBin(const std::string &amp;amp;hex)  {     std::string bin;     for(int i = 0; i &amp;lt; hex.length(); i++)     {        if (hex[i] == &amp;#039;0&amp;#039;) bin += &amp;quot;0000&amp;quot;;        else if(hex[i] == &amp;#039;1&amp;#039;) bin += &amp;quot;0001&amp;quot;;        else if(hex[i] == &amp;#039;2&amp;#039;) bin += &amp;quot;0010&amp;quot;;        else if(hex[i] == &amp;#039;3&amp;#039;) bin += &amp;quot;0011&amp;quot;;        else…」&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== 概要 ==&lt;br /&gt;
C++において、進数の変換を行う手順を記載する。&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 16進数から2進数への変換 ==&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 std::string HexToBin(const std::string &amp;amp;hex)&lt;br /&gt;
 {&lt;br /&gt;
    std::string bin;&lt;br /&gt;
    for(int i = 0; i &amp;lt; hex.length(); i++)&lt;br /&gt;
    {&lt;br /&gt;
       if (hex[i] == &amp;#039;0&amp;#039;) bin += &amp;quot;0000&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;1&amp;#039;) bin += &amp;quot;0001&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;2&amp;#039;) bin += &amp;quot;0010&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;3&amp;#039;) bin += &amp;quot;0011&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;4&amp;#039;) bin += &amp;quot;0100&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;5&amp;#039;) bin += &amp;quot;0101&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;6&amp;#039;) bin += &amp;quot;0110&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;7&amp;#039;) bin += &amp;quot;0111&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;8&amp;#039;) bin += &amp;quot;1000&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;9&amp;#039;) bin += &amp;quot;1001&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;A&amp;#039; || hex[i] == &amp;#039;a&amp;#039;) bin += &amp;quot;1010&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;B&amp;#039; || hex[i] == &amp;#039;b&amp;#039;) bin += &amp;quot;1011&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;C&amp;#039; || hex[i] == &amp;#039;c&amp;#039;) bin += &amp;quot;1100&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;D&amp;#039; || hex[i] == &amp;#039;d&amp;#039;) bin += &amp;quot;1101&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;E&amp;#039; || hex[i] == &amp;#039;e&amp;#039;) bin += &amp;quot;1110&amp;quot;;&lt;br /&gt;
       else if(hex[i] == &amp;#039;F&amp;#039; || hex[i] == &amp;#039;f&amp;#039;) bin += &amp;quot;1111&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    return bin;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 2進数から16進数への変換 ==&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 std::string btoh(const std::string &amp;amp;bin)&lt;br /&gt;
 {&lt;br /&gt;
    std::string hex;&lt;br /&gt;
    std::string bin4;&lt;br /&gt;
    int len = bin.length();&lt;br /&gt;
 &lt;br /&gt;
    // 2進数の文字数が4で割り切れるように先頭に0を足す&lt;br /&gt;
    std::string tmp;&lt;br /&gt;
    if(len % 4 != 0)&lt;br /&gt;
    {&lt;br /&gt;
       for (int i = 0; i &amp;lt; 4 - (len % 4); i++)&lt;br /&gt;
       {&lt;br /&gt;
          tmp += &amp;quot;0&amp;quot;;&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    std::string bin_tmp = tmp + bin;&lt;br /&gt;
    for(int i = 0; i &amp;lt; bin_tmp.length(); i += 4)&lt;br /&gt;
    {&lt;br /&gt;
       bin4 = bin_tmp.substr(i, 4);&lt;br /&gt;
 &lt;br /&gt;
       if (bin4 == &amp;quot;0000&amp;quot;) hex += &amp;quot;0&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;0001&amp;quot;) hex += &amp;quot;1&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;0010&amp;quot;) hex += &amp;quot;2&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;0011&amp;quot;) hex += &amp;quot;3&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;0100&amp;quot;) hex += &amp;quot;4&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;0101&amp;quot;) hex += &amp;quot;5&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;0110&amp;quot;) hex += &amp;quot;6&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;0111&amp;quot;) hex += &amp;quot;7&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;1000&amp;quot;) hex += &amp;quot;8&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;1001&amp;quot;) hex += &amp;quot;9&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;1010&amp;quot;) hex += &amp;quot;A&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;1011&amp;quot;) hex += &amp;quot;B&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;1100&amp;quot;) hex += &amp;quot;C&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;1101&amp;quot;) hex += &amp;quot;D&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;1110&amp;quot;) hex += &amp;quot;E&amp;quot;;&lt;br /&gt;
       else if(bin4 == &amp;quot;1111&amp;quot;) hex += &amp;quot;F&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    return hex;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 10進数から16進数への変換 ==&lt;br /&gt;
文字の長さを揃えて、&amp;lt;code&amp;gt;sprintf&amp;lt;/code&amp;gt;関数を使用して変換する。&amp;lt;br&amp;gt;&lt;br /&gt;
サイズを指定しない場合、そのままの長さで返す。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 std::string IntToHex(int x, int size = 0)&lt;br /&gt;
 {&lt;br /&gt;
    char hex_buf[16] = {};&lt;br /&gt;
    std::sprintf(hex_buf, &amp;quot;%X&amp;quot;, x);&lt;br /&gt;
    std::string hex_str = hex_buf;&lt;br /&gt;
 &lt;br /&gt;
    if(size == 0)&lt;br /&gt;
    {&lt;br /&gt;
       return hex_str;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    std::string front;&lt;br /&gt;
    if(x &amp;gt;= 0)&lt;br /&gt;
    {&lt;br /&gt;
       front = &amp;quot;0&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    else&lt;br /&gt;
    {&lt;br /&gt;
       front = &amp;quot;F&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // 指定のサイズになるまで文字を増やす&lt;br /&gt;
    int diff = size - hex_str.length();&lt;br /&gt;
    for(int i = 0; i &amp;lt; diff; i++)&lt;br /&gt;
    {&lt;br /&gt;
       hex_str = front + hex_str;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // 指定のサイズになるまで文字を減らす&lt;br /&gt;
    if(diff &amp;lt; 0)&lt;br /&gt;
    {&lt;br /&gt;
       hex_str.erase(0, -diff);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    return hex_str;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 16進数から符号なし10進数への変換 ==&lt;br /&gt;
標準の&amp;lt;code&amp;gt;strtoul&amp;lt;/code&amp;gt;関数を使用する。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 unsigned int HexToUInt(const std::string &amp;amp;hex)&lt;br /&gt;
 {&lt;br /&gt;
    return std::strtoul(hex.c_str(), NULL, 16);&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 16進数から符号あり10進数への変換 ==&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 int HexToInt(const std::string &amp;amp;hex)&lt;br /&gt;
 {&lt;br /&gt;
    int result;&lt;br /&gt;
    if(std::strtoul(hex.substr(0,1).c_str(), NULL, 16) &amp;gt; 7)&lt;br /&gt;
    {&lt;br /&gt;
       result = std::strtoul(hex.c_str(), NULL, 16) - 2 * std::pow(2.0, (int)(hex.size() - 1));&lt;br /&gt;
    }&lt;br /&gt;
    else&lt;br /&gt;
    {&lt;br /&gt;
       result = std::strtoul(hex.c_str(), NULL, 16);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    return result;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 10進数から2進数への変換 ==&lt;br /&gt;
16進数に変換した後、2進数へ変換する。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 std::string IntToBin(int x, int size = 0)&lt;br /&gt;
 {&lt;br /&gt;
    std::string bin_str;&lt;br /&gt;
    bin_str = HexToBin(itoh(x, ceil(size / 4.0)));&lt;br /&gt;
 &lt;br /&gt;
    if(size == 0)&lt;br /&gt;
    {&lt;br /&gt;
       return bin_str;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // サイズ調整&lt;br /&gt;
    int diff = size - bin_str.length();&lt;br /&gt;
    if(diff &amp;lt; 0)&lt;br /&gt;
    {&lt;br /&gt;
       bin_str.erase(0, -diff);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    return bin_str;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 2進数から符号なし10進数への変換 ==&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 unsigned int BinToUInt(const std::string&amp;amp; bin)&lt;br /&gt;
 {&lt;br /&gt;
    return std::strtoul(bin.c_str(), NULL, 2);&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 2進数から符号あり10進数への変換 ==&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 int BinToInt(const std::string &amp;amp;bin)&lt;br /&gt;
 {&lt;br /&gt;
    int result = 0;&lt;br /&gt;
 &lt;br /&gt;
    if(bin[0] == &amp;#039;1&amp;#039;)&lt;br /&gt;
    {&lt;br /&gt;
       result = std::strtoul(bin.c_str(), NULL, 2) - 2 * std::pow(2.0, (int)(bin.size() - 1));&lt;br /&gt;
    }&lt;br /&gt;
    else&lt;br /&gt;
    {&lt;br /&gt;
       result = std::strtoul(bin.c_str(), NULL, 2);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    return result;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== float型と16進数の変換 ==&lt;br /&gt;
==== はじめに ====&lt;br /&gt;
float型と16進数の変換では、以下に示すint型とfloat型の共用体を定義する。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 union intfloat&lt;br /&gt;
 {&lt;br /&gt;
    int   ival;&lt;br /&gt;
    float fval;&lt;br /&gt;
 };&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== float型から16進数への変換 ====&lt;br /&gt;
まず、上記の共用体にfloat型の値を代入して、int型で取り出す。&amp;lt;br&amp;gt;&lt;br /&gt;
次に、int型の10進数から16進数に変換する。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 std::string ftoh(float finput)&lt;br /&gt;
 {&lt;br /&gt;
    IntFloat value = {};&lt;br /&gt;
    value.fval = finput;&lt;br /&gt;
    std::string result = IntToHex(value.ival, 8);&lt;br /&gt;
 &lt;br /&gt;
    return result;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== 16進数からfloat型への変換 ====&lt;br /&gt;
まず、16進数から10進数に変換する。&amp;lt;br&amp;gt;&lt;br /&gt;
次に、上記の共用体にint型の値を代入して、float型で取り出す。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 float htof(const std::string &amp;amp;hex)&lt;br /&gt;
 {&lt;br /&gt;
    IntFloat value;&lt;br /&gt;
    value.ival = HexToInt(hex);&lt;br /&gt;
 &lt;br /&gt;
    return value.fval;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__FORCETOC__&lt;br /&gt;
[[カテゴリ:C++]]&lt;/div&gt;</summary>
		<author><name>Wiki</name></author>
	</entry>
</feed>