S
Silk.NET•4y ago
Eagle

Couldn't find shorter than this. For some reason, C# really doesn't like converting char* to byte* d

Couldn't find shorter than this. For some reason, C# really doesn't like converting char* to byte* directly.
var text = "dziqjldji";
fixed(char* characters = text.AsSpan()){
var bytes = (byte*)characters;
}
var text = "dziqjldji";
fixed(char* characters = text.AsSpan()){
var bytes = (byte*)characters;
}
9 Replies
Eagle
EagleOP•4y ago
I'm not sure it will work. It does compile, but I'm worried it might only use the element at the given address rather than the collection. Ah crap, I forgot about the conversion to UTF8. Might implode. Wait does GetBytes not work? Seems like it is just the thing you want.
Opcode_
Opcode_•4y ago
Doesn't work
No description
Opcode_
Opcode_•4y ago
the def of ApplicationName
No description
Eagle
EagleOP•4y ago
Encoding.UTF8.GetBytes() can take a string and return a byte array. If you still need that as a pointer, you can probably use fixed after that.
var text = "dqzildj";
var bytes = Encoding.UTF8.GetBytes(text);
fixed(byte* pointer = bytes){}
var text = "dqzildj";
var bytes = Encoding.UTF8.GetBytes(text);
fixed(byte* pointer = bytes){}
Maybe this?
fixed(byte* bytes = Encoding.UTF8.GetBytes("AppName")){
ApplicationName = bytes;
}
fixed(byte* bytes = Encoding.UTF8.GetBytes("AppName")){
ApplicationName = bytes;
}
It looks like the compiler prevents you to assign ApplicationName outside of a fixed block. You'll probably have to play with that. If the byte pointer doesn't work, you can probably make a dummy fixed block and do ApplicationName = Encoding.UTF8.GetBytes("AppName"); directly inside.
Opcode_
Opcode_•4y ago
It doesn't even recognize this as a variable?
No description
Eagle
EagleOP•4y ago
🤔
Opcode_
Opcode_•4y ago
GitHub
OpenXR-SDK-Source/openxr_program.cpp at f680f8d1fd51c0524d6742c4c36...
Sources for OpenXR loader, basic API layers, and example code. - OpenXR-SDK-Source/openxr_program.cpp at f680f8d1fd51c0524d6742c4c3657038cc4a860a · KhronosGroup/OpenXR-SDK-Source
Eagle
EagleOP•4y ago
Yeah, that's a C standard function for copying characters of a string from an address to another. You'd just assign a value to a string in C#, but the issue here is the fixed keyword. Looking up the issue right now.
vdvman1
vdvman1•4y ago
I think this is because ApplicationName is actually directly part of the containing struct, it isn't a pointer that can be assigned Such an assignment would require a full copy

Did you find this page helpful?