简体   繁体   English

将字符串中的十进制转换为二进制时出错

[英]Error converting decimal to binary in string

I have written a parser which reads in a text file with the text in the format of the following types: 我编写了一个解析器,该解析器以以下类型的格式读取文本文件:

a r3, r2, r1
ah r8, r5, r6, r7
ai r10, i10, r127

the number for r can change and the initial characters can change. r的数字可以更改,并且初始字符可以更改。 Basically what the parser does is based on the initial characters (a, ah, etc..), it will define an opcode value and then parse the register numbers and convert those into binary. 基本上,解析器的操作基于初始字符(a,ah等),它将定义一个操作码值,然后解析寄存器号并将其转换为二进制。 It will concatenate all these values together to create a 32-bit long binary instruction string and output this to a text file. 它将所有这些值连接在一起以创建一个32位长的二进制指令字符串,并将其输出到文本文件。

While testing it, i have ran into an error. 在测试时,我遇到了一个错误。

Here is my code: 这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <bitset>
using namespace std;

void main()
{
    string instruction, input, OpCode;
    int i = 0;
    int format = 0;
    vector<string> myString;
    ifstream inFile;
    inFile.open("myfile.txt");

    //format 1 -> input: syntax/rt/ra/rb     output -> opcode/rb/ra/rt
    //format 2 -> input: syntax/rt/ra/rb/rc  output -> opcode/rt/rb/ra/rc
    //format 3 -> input: syntax/rt/ra        output -> opcode/i7/ra/rt
    //format 4 -> input: syntax/rt/ra        output -> opcode/i10/ra/rt
    //format 5 -> input: syntax/rt           output -> opcode/i16/rt

    if (inFile.is_open()){
        cout << "test" << endl;
        //Read until no more lines in text file to read
        while (getline(inFile, input))
        {
            istringstream ss(input);
            string token;

            //Get the instruction token
            getline(ss, token, ' ');
            instruction = token;

            //ADD WORD
            if (instruction == "a"){
                OpCode = "00011000000";
                format = 1;
            }

            if (format == 1){
                // Seperate the registers
                //We have RA, RB, AND RT
                //Convert the values into binary
                string value;
                while (getline(ss, token, ',')){
                    ss.ignore();
                    token.erase(0, 1); //Remove the "r" or "i" character from each string/register              
                    myString.push_back(token);
                }

                for (int j = 0; j < myString.size(); j++)
                {
                    cout << myString[j] << endl;
                    value = bitset<8>(myString[j]).to_string();
                    cout << value << endl;
                    OpCode = OpCode + value;
                    cout << OpCode << endl;
                }
            }


        }

    }
    system("pause");
}

For the sake of simplicity, i have only put 1 if statement for the add word instruction. 为了简单起见,我仅将1 if语句用于添加字指令。 The input i am using for to test is the following: 我用来测试的输入如下:

a r3, r1, r2

What it should do is see the instruction is a , so OpCode gets set and it will proceed to parse r3, r2, r1 . 它应该做的是看到指令是a ,因此OpCode被设置,它将继续解析r3, r2, r1 It will remove the r and convert 3, 2, and 1 into its binary value and place that into a string which is concatenated with OpCode to form a 32-bit long instruction. 它将删除r并将3、2和1转换为其二进制值,然后将其放入一个字符串中,该字符串与OpCode串联在一起以形成32位长指令。 The issue i am having is with value = bitset<8>myString[j]).to_string(); 我遇到的问题是value = bitset<8>myString[j]).to_string(); . Visual studio throws the following error while debugging: Visual Studio在调试时会引发以下错误:

Unhandled exception at 0X7712C52F in Assembler_Parser.exe: Microsoft C++ exception: std::invalid_arguement at memory location 0x0039F110.

Im not sure what to make of this. 我不确定该怎么做。 It gives me the option to break or continue. 它使我可以选择中断或继续。 If i continue, it will convert 3 into 00000000. On the next iteration, it properly convert 2 to 00000010, but then again it throws an exception for 1. 如果我继续执行,它将3转换为00000000。在下一次迭代中,它将2正确转换为00000010,但是随后再次抛出1的异常。

Im not sure why this is happening. 我不确定为什么会这样。

The string constructor of bitset doesn't convert the value from its decimal (or hex) representation to binary. bitset的字符串构造函数不会将值从其十进制(或十六进制)表示形式转换为二进制形式。 If any character it encounters is not 0 or 1 it throws an std::invalid_argument exception. 如果遇到的任何字符都不为01则会引发std::invalid_argument异常。 Try std::stoi : 试试std::stoi

value = bitset<8>(std::stoi(myString[j])).to_string();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM