String to array of bytes?

About Monkey 2 Forums Monkey 2 Programming Help String to array of bytes?

This topic contains 4 replies, has 3 voices, and was last updated by  Mark Sibly 3 months, 1 week ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #15897

    jondecker76
    Participant

    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?

    #15899

    abakobo
    Participant

    Hi,

    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()

    #15901

    jondecker76
    Participant

    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!

    #15902

    abakobo
    Participant

    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.

    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.

    #15951

    Mark Sibly
    Keymaster

    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:

    http://kunststube.net/encoding/
    http://utf8everywhere.org/

    Google for lots more!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.