2

I want to concatenate a uint64_t to unsigned char*. I can not use sprintf(), strcpy(), and strcat() because they only accept char* as argument.

#include <cstdint>

// I want to concatenate these two:

unsigned char data[8] = "tJQVfvcj";
uint64_t num = 1732462110473334785;

The output should be equivalent to:

unsigned char concat[28] = "tJQVfvcj1732462110473334785";

The efficiency is also important, although it is the second priority. I highly appreciate any suggestions.

Edit: I used sprintf() like this:

  unsigned char data[9] = "tJQVfvcj";
  uint64_t num = 1732462110473334785;

  unsigned char concat[28];
  sprintf(concat, "%s%" PRId64 "", data, num);

However, it leads to this error:

error: invalid conversion from ‘unsigned char*’ to ‘char*’ [-fpermissive]

Ali
  • 1,023
  • 1
  • 9
  • 31
  • 5
    C or C++? Those are different languages. – Yksisarvinen Aug 12 '20 at 15:25
  • Concatenate "with". There is also `std::to_string()` – sweenish Aug 12 '20 at 15:27
  • 1
    Use std::format – eerorika Aug 12 '20 at 15:28
  • `sprintf()` takes a format string, wherein you can specify formats for converting numbers. In C++, `std::ostringstream` is yet another option. – Tony Delroy Aug 12 '20 at 15:28
  • @Yksisarvinen You are right. I am using C++ but I am flexible in changing to C. Do you know any solution for C++? – Ali Aug 12 '20 at 15:35
  • Is the question so trivial that deserves so many negative feedbacks? I tried various ways before post the question. – Ali Aug 12 '20 at 15:38
  • @ggorlen I tried ```sprintf(concat, "%s%" PRId64 "", data, num)``` and it leads to this error: invalid conversion from ‘unsigned char*’ to ‘char*’ [-fpermissive]. I updated the question. – Ali Aug 12 '20 at 15:41
  • 1
    The error message is very clear, use `char concat[28]` instead of `unsigned char[]` – phuclv Aug 12 '20 at 15:45
  • @phuclv using ```char``` fixes the problem, but I need ```unsigned char``` later as an input to another function. – Ali Aug 12 '20 at 15:46
  • then change the other function to receive `char*` instead of `unsigned char*`. In the worst case just cast to the desired type – phuclv Aug 12 '20 at 15:48
  • Unless you can guarantee that the data is always less than 10 characters, declaring a buffer of length of only 28 will lead to a buffer overrun. – PaulMcKenzie Aug 12 '20 at 15:48
  • @phuclv Unfortunately I can not change the other functions. It is in a library that I am using. So the question is how can I convert ```char*``` to ```unsigned char*``` safely? – Ali Aug 12 '20 at 15:50
  • 1
    @Ali -- Consider the reason why the type is `char *`. A `char` can either be signed or unsigned, depending on the platform, compiler settings, etc. Maybe if you changed your compiler settings to treat `char` as unsigned, then that may address the issue. – PaulMcKenzie Aug 12 '20 at 15:53
  • This may be a [duplicate](https://stackoverflow.com/questions/20518527/gcc-force-compiler-to-use-unsigned-char-by-default)? – PaulMcKenzie Aug 12 '20 at 15:57
  • @PaulMcKenzie I used the ```-funsigned-char``` flag. However, I still get the error: invalid conversion from ‘char*’ to ‘const unsigned char*’. So I have to do another conversion. This is not efficient. – Ali Aug 12 '20 at 16:11
  • 2
    Why not simply do `static_cast(concat)`? – PaulMcKenzie Aug 12 '20 at 16:15
  • 1
    @PaulMcKenzie `char`, `signed char` and `unsigned char` are 3 distinct types so a function receiving `unsigned char` can't accept `char` even if char is unsigned. A cast is the only solution in this case – phuclv Aug 13 '20 at 01:32

0 Answers0