ubyte[] to string with @nogc - different behaviors
Steven Schveighoffer
schveiguy at gmail.com
Mon Aug 10 13:42:40 UTC 2020
On 8/9/20 11:56 PM, Andy Balba wrote:
> On Wednesday, 5 August 2020 at 21:15:10 UTC, Luhrel wrote:
>> On Wednesday, 5 August 2020 at 20:28:22 UTC, Steven Schveighoffer wrote:
>>>
>>> ...
>>>
>>> But still, you are mallocing a block and throwing it away.
>>>
>>> And I realize, this wouldn't have worked anyway, as str is a string,
>>> which means you can't overwrite the data.
>>>
>>> I think there is likely a better way to do what you are trying to do.
>>>
>>
>> Maybe with a ref parameter, but I think this will only move the
>> problem outside the function.
>>
>>>
>>> ...
>>>
>>> What is the source of the incoming data? Will it be around by the
>>> time you need to use this string? Can you just slice it without
>>> copying? I'm not familiar with this code, so I don't know the
>>> requirements.
>>>
>>> I would think:
>>>
>>> return cast(const(char)[])buffer[0 .. strnlen(buffer.ptr,
>>> buffer.length))];
>>>
>>> or something like that.
>>>
>>
>> Will try that.
>
>
> What am I missing here ??
> Prior to reading this post, I would have tried
>
> ubyte[4] ss= [0x23, 0x24, 0x25, 0x26] ; writeln ("ss ", ss);
> ... which outputs (as expected) ss [35, 36, 37, 38]
>
> And to convert ss to string, I would have tried :
> string sss = cast(string) ss; writeln ("sss ", sss);
> ... which outputs (as expected) sss #$%&
>
>
> So, unless I'm missing something, isn't converting ubyte[] to string
> super simple ?
There were other problems in this code example, and once you fix the
underlying problems, it's no longer a conversion of ubyte[] to string.
But yes, conversion between arrays is pretty easy in D.
-Steve
More information about the Digitalmars-d
mailing list