1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 How to Convert Bytes to String? viceversa?

Discussion in 'Programming' started by lord_berto, Jan 10, 2017.

  1. lord_berto

    lord_berto Member

    Joined:
    Aug 6, 2016
    Posts:
    112
    Hello, I wanted to know how i could take a string, for example
    "Hello" and convert it to bytes "33749023" ?
    I saw this one example posted by someone on reddit

    seed = 0;
    s = "my string seed"; for(var i=0; i<string_byte_length(s); i++)
    { random_set_seed(seed); seed = seed+string_byte_at(s,i); }


    this will supposedly convert any text into bytes, but how could i undo this?
    Bytes to readable text?

    Thanks..
     
  2. MishMash

    MishMash Member

    Joined:
    Jun 20, 2016
    Posts:
    379
    well, addition is not an in-vertible operation, as there is no way of knowing what numbers could have contributed to the initial value.

    This bit of code is simply summing up all the byte values of the characters in a string into one number. If it were concatenating them, so say "255" + "323", so that it returned a numeric string, it would be partially possible.
    To give you an example of why this doesn't work, if you just have the final value "100", that could have been made from:

    10 + 90 or 20 + 30 + 50 or 100 + 0 or 10 + 10 + 10... you get the idea :p

    The best thing is to instead change the way you are thinking about the problem and keep track of both the seed variable as a string AND the numeric reduction (A reduction is just a process that reduces a list of multiple values into a single value).

    So you would have say:
    global.GAME_SEED_STRING = "0mgEpicL3v3l_xxx";
    global.GAME_SEED_VALUE = get_seed_value( global.GAME_SEED_STRING );

    (Where get_seed_value is the code you posted above).
     
  3. lord_berto

    lord_berto Member

    Joined:
    Aug 6, 2016
    Posts:
    112


    Thanks alot for informing me about that,
    So would you know a way of instead converting text into an unreadable text?
    for example encode "Hello" to say "BKLKDD"
     
  4. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
  5. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,473
    Use buffers.
    String to bytes:
    Write the string to a buffer as buffer_text.
    Seek to start.
    Then read u8 values from buffer

    Bytes to string:
    Write u8 values to buffer.
    Write a final null character (u8 0)
    Seek to start.
    Then read a string from buffer.
     
    Phil Strahl likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice