-1

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. Assume my hexvalue is 0x424C41. I want to end with mystring being "BLABLA".

What's the best way to do this?

Swordfish
  • 12,971
  • 3
  • 21
  • 43
Diego
  • 17
  • 3

2 Answers2

1

(Assuming I understand you correctly, and that UNICODE is not define by the preprocessor)

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.

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"). 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.

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.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • And is sprintf the best way to do it? The hex data has always the same fixed size. – Diego May 11 '19 at 16:54
  • 1
    @Diego: No, I didn't say `sprintf()` was the best way to do "it", because I don't know what "it" is. The three steps I suggested, on a little-endian machine would involve `malloc()` for allocation and `strcpy()` / `strncpy()` for copying the the two strings. – einpoklum May 11 '19 at 17:05
0
#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.

Swordfish
  • 12,971
  • 3
  • 21
  • 43