C++ cli 将字符串格式化为十六进制并验证

C++ cli 将字符串格式化为十六进制并验证,c++-cli,C++ Cli,我有一个从C++/CLIrichtextbox接受十六进制值的应用程序 该字符串来自用户输入 示例输入和预期输出 01 02 03 04 05 06 07 08 09 0A //good input 0102030405060708090A //bad input but can automatically be converted to good by adding spaces. XX ZZ DD AA OO PP II UU HH SS //bad input

我有一个从C++/CLI
richtextbox
接受十六进制值的应用程序
该字符串来自用户输入

示例输入和预期输出

01 02 03 04 05 06 07 08 09 0A  //good input

0102030405060708090A           //bad input but can automatically be converted to good by adding spaces.

XX ZZ DD AA OO PP II UU HH SS  //bad input this is not hex

01 000 00 00 00 00 00 01 001 0 //bad input hex is only 2 chars
如何编写函数:
1.检测输入是好输入还是坏输入。
2.如果输入错误,请检查错误输入的类型:无空格,非十六进制,必须拆分为2个字符。
3.如果没有空格输入错误,只需自动添加空格即可

到目前为止,我通过搜索以下空间制作了一个空间检查器:

for ( int i = 2; i < input.size(); i++ ) 
{
    if(inputpkt[i] == ' ')
    {
        cout << "good input" << endl;
        i = i+2;
    }
    else
    {
        cout << "bad input. I will format for you" << endl;
    }
}

更新

1检查输入是否为十六进制:

bool ishex(std::string const& s)
{
    return s.find_first_not_of("0123456789abcdefABCDEF ", 0) == std::string::npos;
}

<>你是用C++还是CLI操作,还是用C++?您已经将其标记为C++/CLI,但您使用的是std::string,而不是.Net

我建议将此作为一个总体计划:首先,根据任何空格将大字符串拆分为小字符串。对于每个单独的字符串,请确保它只包含[0-9a-fA-F],并且长度为两个字符的倍数

实现可以是这样的:

array<Byte>^ ConvertString(String^ input)
{
    List<System::Byte>^ output = gcnew List<System::Byte>();

    // Splitting on a null string array makes it split on all whitespace.
    array<String^>^ words = input->Split(
        (array<String^>)nullptr, 
        StringSplitOptions::RemoveEmptyEntries);

    for each(String^ word in words)
    {
        if(word->Length % 2 == 1) throw gcnew Exception("Invalid input string");

        for(int i = 0; i < word->Length; i += 2)
        {
            output->Add((Byte)(GetHexValue(word[i]) << 4 + GetHexValue(word[i+1])));
        }
    }

    return output->ToArray();
}

int GetHexValue(Char c) // Note: Upper case 'C' makes it System::Char
{
    // If not [0-9a-fA-F], throw gcnew Exception("Invalid input string");
    // If is [0-9a-fA-F], return integer between 0 and 15.
}
数组^ConvertString(字符串^input)
{
List^output=gcnewlist();
//对空字符串数组进行拆分将使其在所有空格上进行拆分。
数组^words=input->Split(
(数组)nullptr,
StringSplitOptions::RemoveEmptyEntries);
对于每个(单词中的字符串^word)
{
如果(字->长度%2==1)抛出新异常(“无效输入字符串”);
对于(int i=0;iLength;i+=2)
{
输出->将((字节)(GetHexValue(字[i])添加到数组();
}
int GetHexValue(Char c)//注意:大写字母“c”表示系统::Char
{
//如果不是[0-9a-fA-F],抛出新异常(“无效输入字符串”);
//如果为[0-9a-fA-F],则返回0到15之间的整数。
}
array<Byte>^ ConvertString(String^ input)
{
    List<System::Byte>^ output = gcnew List<System::Byte>();

    // Splitting on a null string array makes it split on all whitespace.
    array<String^>^ words = input->Split(
        (array<String^>)nullptr, 
        StringSplitOptions::RemoveEmptyEntries);

    for each(String^ word in words)
    {
        if(word->Length % 2 == 1) throw gcnew Exception("Invalid input string");

        for(int i = 0; i < word->Length; i += 2)
        {
            output->Add((Byte)(GetHexValue(word[i]) << 4 + GetHexValue(word[i+1])));
        }
    }

    return output->ToArray();
}

int GetHexValue(Char c) // Note: Upper case 'C' makes it System::Char
{
    // If not [0-9a-fA-F], throw gcnew Exception("Invalid input string");
    // If is [0-9a-fA-F], return integer between 0 and 15.
}