January 14, 2019 at 1:29 am #15897
I need to convert strings to arrays of bytes, but I haven’t been able to figure it out yet. I haven’t found any Chr or Ascii functions anywhere, and I’m ready to rip my hair out over the CString madness. Any tips?January 14, 2019 at 8:40 am #15899
Firt thing to know: CString is not a type you are supposed to use inside Monkey2. It’s a convenience type that will automaticaly translate a String to CString for a C/Cpp function. When you declare CString in your externals, the C func will be receiving a null terminated char* (ie a CString). Arrays in Monkey2 are Not C Arrays and thus if you want a real C type array in Monkey 2
you’ll have to malloc it yourself and use a “Ubyte Ptr”. Dereferencing it will then look like a “classic” array but you won’t have the “.Length” so you’ll have to store the size by yourself or check for null termination. You can malloc using databuffer too as i’ve seen you did.You’ll have to use libc.char_t type wich is the same as C’s char. In an Array or in a DataBuffer. And use the .Data To Get the first element.
Secondly, Monkey2 String is 16 bit so you’ll have to ensure that you don’t meet any value exceeding the 8bit capability of your Ubytes. Or use UShorts if you can? I know there is some Char vs UTF8 thing. For me that’s where you get a problem in your quest.
The probable easyest way to do what you want (for me) is to find a C function that do the stuff and import it in your project, there you’ll have to use of CString.
String an Array are actulay defined in C++ and not in Monkey2 (bbTypes.h/bbTypes.cpp).
If you post some C/C++ code I can write a wrapper for you if you’re not comfortable with externals.
For Ascii funcs. If you reach a String element by index it will give you it’s int value. To transform that int value back to a string Use String.FromChar()January 14, 2019 at 9:01 am #15901
Thanks for the answer. I guess I can do it in C/C++, but I was hoping to learn to do it in native Mx2 as a learning exercise.
I do a lot of work with various REST APIs and having a working HMAC-SHA256 implementation is super important to me. Could you take a peek at the very simple code in my other post? http://monkeycoder.co.nz/forums/topic/help-porting-hmac-sha256-code/#post-15900
Thanks!January 14, 2019 at 9:51 am #15902
Geeting a char by string index actually returns a signed int! So that is a bit strange as the language reference states that Strings have 16 bit elements.Monkey123456789101112131415Namespace myapp#Import "<std>"#Import "<reflection>"Using std..Using reflection..Function Main()Local s:="yo"Local c:=sPrint Typeof(c)End
I’ll have a look at your code but I must say I’m not really into bit shifting and all that stuff so I’m not sure I’ll be able to help.January 18, 2019 at 11:08 pm #15951
Strings in monkey2 are converted to UTF8 when read/written to memory/streams, and when converted to the type ‘CString’ (aka ‘const char *’) for use with extern APIS.
The first 128 characters of UTF8 match up with the (only) 128 chars of ascii, but bytes in UTF8 strings with their high bit set indicate the start of a ‘multibyte character sequence’, so the number of bytes in a UTF8 ‘string’ may not equal the number of characters in the string.
More info on UTF8 can be found here:
Google for lots more!
You must be logged in to reply to this topic.