簡體   English   中英

C ++:包含十六進制值的格式字符串

[英]C++: Format String Containing Hex Value

在Visual Studio 2010上使用C ++。

嘗試提出一個健壯的函數,它將十六進制值作為字符串,大小為整數,然后輸出格式化的十六進制值。

例如,

如果輸入字符串為“A2”且大小為1,則輸出為“0xA2”

如果輸入字符串為“800”且大小為2,則輸出為“0x0800”

如果輸入字符串為“DEF”且大小為4,則輸出為“0x00000DEF”

如果輸入字符串為“00775”且大小為4,則輸出為“0x00000775”

如果輸入字符串為“FB600”且大小為3,則輸出為“0x0FB600”

基本思想是,將size乘以2,然后如果字符串長度小於該值,則將前導零添加到十六進制值,然后將其附加為“0x”。

無論是否添加前導零,都附加“0x”。

正如您在第一個示例中所看到的,由於字符串已包含2個字符,因此無需添加零。

我提出了以下功能,但它有內存損壞。 此外,當我嘗試通過調用此函數幾個時間來處理大量數據時,它崩潰了。 似乎我的邏輯中有記憶漏洞。

所以我希望有人能為這個功能提出一個強大的智能代碼。

我嘗試了什么:

void formatHexString(char* inputHex, int size, char* outputFormattedHex)
{
    int len = size * 2;
    int diff = len - strlen(inputHex);
    char * tempHex = new char [diff + 2]; //"2" is for holding "0x"
    tempHex[0] = '0';
    tempHex[1] = 'x';

    if (len > strlen(inputHex))
    {

        for (int i = 2; i < ((len - strlen(inputHex)) + 2); i++)
        {
            tempHex[i] = '0';

        }

    }

    strcat(tempHex, inputHex);
    sprintf(outputFormattedHex, "%s", tempHex);

    delete [] tempHex;

    cout <<outputFormattedHex <<endl;
}


int main 
{
    char bbb1[24];
    formatHexString("23", 1, bbb1);
    char bbb2[24];
    formatHexString("A3", 2, bbb2);
    char bbb3[24];
    formatHexString("0AA23", 4, bbb3);
    char bbb4[24];
    formatHexString("7723", 4, bbb4);
    char bbb5[24];
    formatHexString("AA023", 4, bbb5);
    return 0;
}

更新:

我無法修改原始函數的參數,因為此函數是從其他應用程序調用的。 所以我用你的代碼修改了我原來的功能,但這不起作用。 有任何想法嗎?

void formatHexString(char* inputHex, int size, char* outputFormattedHex)
{
    string input(inputHex);
    std::size_t const input_len(input.length());

    if (!size || (size * 2 < input_len))
        size = input_len / 2 + input_len % 2;

    std::stringstream ss;
    ss << "0x" << std::setw(2 * size) << std::setfill('0') << input;
    sprintf(outputFormattedHex, "%s", ss.str());
}
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <cstddef>

std::string formatHexString(std::string const & input, std::size_t size = 0)
{
    std::size_t const input_len(input.length());

    // always round up to an even count of digits if no size is specified
    // or size would cause the output to be truncated
    if (!size || (size * 2 < input_len))
        size = input_len / 2 + input_len % 2;

    std::stringstream ss;
    ss << "0x" << std::setw(2 * size) << std::setfill('0') << input;
    return ss.str();
}

int main()
{
    std::cout << formatHexString(   "23") << '\n'
              << formatHexString(   "A3", 2) << '\n'
              << formatHexString( "AA23", 4) << '\n'
              << formatHexString( "7723", 4) << '\n'
              << formatHexString("AA023", 4) << '\n';
}

沒有std::stringstream解決方案:

#include <string>
#include <cstddef>

std::string formatHexString(std::string const & input, std::size_t size = 0)
{
    std::size_t const input_len(input.length());

    // always round up to an even count of digits if no size is specified
    // or size would cause the output to be truncated
    if (!size || (size * 2 < input_len))
        size = input_len / 2 + input_len % 2;

    std::string result("0x");

    for (std::size_t i = 0, leading_zeros = size * 2 - input_len; i < leading_zeros; ++i)
        result += '0';

    result += input;
    return result;
}

更新:

#define  _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cstddef>
#include <cstdio>

void formatHexString(char const * inputHex, int size, char * outputFormattedHex)
{
    int const input_len(std::strlen(inputHex));

    if (!size || (size * 2 < input_len))
        size = input_len / 2 + input_len % 2;

    std::stringstream ss;
    ss << "0x" << std::setw(2 * size) << std::setfill('0') << inputHex;
    std::strcpy(outputFormattedHex, ss.str().c_str());
}

int main()
{
    char output[24];
    formatHexString("23", 1, output);
    std::cout << output << '\n';

    formatHexString("A3", 2, output);
    std::cout << output << '\n';

    formatHexString("0AA23", 4, output);
    std::cout << output << '\n';

    formatHexString("7723", 4, output);
    std::cout << output << '\n';

    formatHexString("AA023", 4, output);
    std::cout << output << '\n';
}

從您的問題中不清楚,您對輸入的前導零的期望是什么:即輸入“00000000EA”大小2變為“00EA”,或者保持其所有前導零。

這個簡單的解決方案適用於兩種情況(bTrim = true,對於第一種情況):

#include <string>

void formatHexString(std::string & strHex, unsigned int nSize, bool bTrim = true) 
{
    if (bTrim) // Trim leading-zeros:
        strHex = strHex.substr(strHex.find_first_not_of('0'));

    if (nSize > strHex.length()) // Pad with leading-zeros to fit nSize:
        strHex.insert(0, std::string(nSize - strHex.length(), '0').c_str());

    strHex.insert(0, "0x"); // Insert prefix
}

-

如果保留原始簽名很重要,請將以上formatHexString包裝為:

void formatHexString(char* inputHex, int size, char* outputFormattedHex)
{
    std::string strHex(inputHex);
    formatHexString(strHex, size * 2);
    strcpy_s(outputFormattedHex, strHex.length()+1, strHex.c_str()); // Visual Studio
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM