Legacy GM How to Convert Bytes to String? viceversa?

lord_berto

Member
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..
 

MishMash

Member
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).
 

lord_berto

Member
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).


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"
 

GMWolf

aka fel666
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.
 
Top