在c ++中从十六进制转换为二进制

时间:2015-11-21 11:18:12

标签: c++

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>

using namespace std;


class hextobin
{
private:

public:
    void convh_b(string);
};

// function convh_b 
void hextobin::convh_b(string x)
{
    for(int i = 0; i<6; i++)
    {
        switch(x[i])
        {
        case '0': cout << "0000"; break;
        case '1' :cout << "0001"; break;
        case '2': cout << "0010"; break;
        case '3': cout << "0011"; break;
        case '4': cout << "0100"; break;
        case '5': cout << "0101"; break;
        case '6': cout << "0110"; break;
        case '7': cout << "0111"; break;
        case '8': cout << "1000"; break;
        case '9': cout << "1001"; break;
        case 'A': cout << "1010"; break;
        case 'B': cout << "1011"; break;
        case 'C': cout << "1100"; break;
        case 'D': cout << "1101"; break;
        case 'E': cout << "1110"; break;
        case 'F': cout << "1111"; break;
        }
    }

    cout << endl;
}

void main()
{
    hextobin p;
    string  l1;
    ifstream myfile("assembly.txt");

    if(myfile.is_open())
    {
        while(!myfile.eof())
        {
            getline(myfile, l1);
            cout << l1 << endl;
            p.convh_b(l1);
        }

        myfile.close();
    }
    else
        cout << "unable";
}

我的代码从文件中读取十六进制数,然后将它们转换为二进制,除了第一行的最后一位数?

示例:

FF01
B521

输出:

111111110000    // here 1 hexa not converted to 0001 ?? 
1011010100100001 

0 个答案:

没有答案