简体   繁体   English

如何在C中将十六进制连接到字符串

[英]How to concatenate an hex to a string in C

I'm trying to create a string that's composed with multiple parts. 我正在尝试创建由多个部分组成的字符串。 It starts as a normal string and at some point a function is called that fills a pointer with an hex number. 它以普通字符串开头,并在某些时候调用一个函数,该函数用十六进制数字填充指针。 Like shown below. 如下图所示。

PVOID hexval = NULL;
PCTSTR mystring = TEXT("BLA");

function(&hexval);

mystring += hexval;  // just an idea of what I want, not actual code

As shown above I want mystring to have the hexvalue appended to it. 如上所示,我希望mystring可以附加hexvalue Assume my hexvalue is 0x424C41 . 假设我的hexvalue值为0x424C41 I want to end with mystring being "BLABLA". 我想以mystring作为“ BLABLA”结束。

What's the best way to do this? 最好的方法是什么?

(Assuming I understand you correctly, and that UNICODE is not define by the preprocessor) (假设我理解正确,并且预处理程序未定义UNICODE)

you should do the following: 您应该执行以下操作:

  1. Allocate space for a string which would contain both the original text and the new "text". 为将同时包含原始文本和新“文本”的字符串分配空间。
  2. Copy the original string to the new location. 将原始字符串复制到新位置。
  3. Concatenate the bytes of the hexval, from lowest to highest, after the copy of the original string. 在原始字符串的副本之后,从最低到最高连接hexval的字节。

There are two tricky points here: 这里有两个棘手的问题:

  1. You haven't explained how the "hex data" is terminated. 您尚未说明“十六进制数据”如何终止。 Is there always a zero-valued byte at the "end" of it? 在它的“末端”是否总有一个零值字节? Or - does it have a fixed size? 或者-它有固定的尺寸吗?
  2. The order of bytes of the hex value may not be the order of bytes you want in a string. 十六进制值的字节顺序可能不是您想要的字符串中的字节顺序。 Some machines store the lowest byte first ("little-endian"; in your example, it would be 0x41, 0x4C, 0x42, 0x00 - assuming it's a 4-byte value) while others store the highest byte first ("big endian"). 某些机器先存储最低字节(“ little-endian”;在您的示例中,它将是0x41、0x4C,0x42、0x00-假定为4字节),而其他机器先存储最高字节(“ big endian”) 。 If you don't know you're on a little-endian machine, you'll need to reorder the bytes and you can't just copy from hexval directly. 如果您不知道自己使用的是hexval字节序计算机,则需要重新排序字节,而不能直接从hexval复制。

HOWEVER! 然而! I advise you against doing any of this. 我建议您不要这样做。 It is much better to avoid getting into a situation in which you need to perform these brute-force conversions. 最好避免陷入需要执行这些蛮力转换的情况。 I would bet you could probably solve whatever problem you're facing differently. 我敢打赌,您可能会以不同的方式解决任何问题。

#include <stddef.h>
#include <stdlib.h>

#include <windows.h>
#include <tchar.h>

int main(void)
{
    ULONG_PTR value  = 0x424C41;
    LPVOID hexval    = &value;
    PCTSTR mystring  = _T("BLA");

    size_t length    = _tcslen(mystring);
    size_t new_size  = length + sizeof(ULONG_PTR) + 1;

    LPTSTR new_string = calloc(new_size, sizeof(*new_string));
    _tcscpy(new_string, mystring);

    size_t offset;
    for (offset = sizeof(ULONG_PTR); offset && !((char*)hexval)[offset - 1]; --offset);

    for (size_t i = length ? length : 0, k = offset; k; ++i, --k)
        new_string[i] = ((char*)hexval)[k - 1];

    _tprintf(_T("\"%s\"\n"), new_string);
    free(new_string);
}

Maintaining ANSI-support in 2019 is masochism, though. 不过,在2019年保持ANSI支持是受虐狂。

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

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