A few words about context
I'm working on a PHP library which uses the PHP FFI to make tensorflow usable in PHP. One part of that library has to pass strings to tensorflow's c library. To be able to do that, there are these definitions in the c header file:
extern void TF_StringInit(TF_TString *t);
extern void TF_StringCopy(TF_TString *dst, unsigned const char *src,
size_t size);
After lot's of debugging, I arrived at this code which does pass the string into the TF_TString:
// Initialize the TF_TString
$tstr = TensorFlow::$ffi->new('TF_TString[1]');
TensorFlow::$ffi->TF_StringInit(FFI::addr($tstr[0]));
// Unpack the input string
$unpacked = unpack('C*', $str);
$input = FFI::new('uint8_t[' . count($unpacked) . ']');
foreach($unpacked as $i=>$part) {
$input[$i - 1] = $part;
}
// Copy the unpacked string into the TF_TString
TensorFlow::$ffi->TF_StringCopy(FFI::addr($tstr[0]), $input, strlen($str) + 1);
The issue
If $str is Hello World, I would expect that the TF_TString now contains Hello World.
Instead, it is 0Hello World.
So the string is always prefixed with a 0.
Further thoughts
It would theoretically be possible that this issue happens because of my logic which converts the string from tensorflow back to php. However, tensorflow has an internal operation called StringJoin which expects two strings as input an joins them. If I use that operation for the strings Hello and World, I get 4HelloR World. As there is a the new char R inbetween the joined words, I expect that the issue lies with my encoding and not somewhere else.
I'm looking forward to any kind of input
This is kind of a longshot question, because I don't expect many people working with PHP FFI yet, but maybe (hopefully!) there is someone with more knowledge about strings in c which is able to help.