0
\$\begingroup\$

sorry for the unhelpful title. I'm trying to understand how to convert information to bytes so that I can save it using Google's Saved Games feature that comes with Play Services, as explained here.

https://codelabs.developers.google.com/codelabs/playservices_unity/index.html?index=..%2F..%2Findex#9

I've mostly copy-pasted the code but it's not really working out for me.

However, my initial question is about bytes. So it looks like Google stores game information in bytes[] . The example only shows a single int being converted to byte and then being saved. What if I wanted to save multiple integers in Google's Saved Game services? I'm not exactly sure what this byte array is in the first place and how everything can be converted to bytes. If anybody has any good resources or examples, it would be much appreciated! Thank you

\$\endgroup\$
1
  • \$\begingroup\$ Byte array is literally array of bytes. Any information is stored in bytes on your PC. Array of byte, in terms of programming, is representation of some sort of data in most simple form. For instance, you have some custom data type. And you want to store its data somewhere. But for some reason, you can't simply save data in format you've created. Then you simply convert this data to byte array, which is simple data format, and can be saved mostly anywhere. Then you just get this data back, when you need it, and convert it back to actual data type. \$\endgroup\$ Commented Sep 8, 2016 at 7:20

1 Answer 1

1
\$\begingroup\$

You can convert pretty much anything to a byte[] but the built-in methods are not all the same.

For numeric, non-string values you can do:

byte[] _intValue = BitConverter.GetBytes(_someInt);

For string values, you can do:

byte[] _stringValue = Encoding.ASCII.GetBytes(_someString);

If this is something you plan to do a lot of, you may want to create your own overloaded classes so you have a single call you can make to convert any data you need, probably with methods to covert to and from byte[] for each.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ When there is a possibility that your string might contain international characters, you should of course use Encoding.UTF8 instead of Encoding.ASCII. \$\endgroup\$ Commented Sep 8, 2016 at 12:22
  • \$\begingroup\$ @Philipp yes, for sure. These are two examples of many possible methods for converting to byte array. \$\endgroup\$ Commented Sep 8, 2016 at 12:31
  • \$\begingroup\$ What if I wanted to save multiple things in a byte array? For example , say I wanted to save an int and a string. Now I have two byte arrays - but Google play saved games only takes one byte array. Can I combine the two byte arrays and mark the start and end so the information doesn't get mixed up? \$\endgroup\$ Commented Sep 8, 2016 at 22:19
  • \$\begingroup\$ Yes, if they are always a fixed length, or you can pad them to a fixed length, you can just dump them in. If they aren't, I'd recommend using a particular series of unlikely characters to mark a separation between elements. Of course, they should always be packed in the same order. If they may be in a different order, you could also use specifiers. \$\endgroup\$ Commented Sep 8, 2016 at 22:44
  • 1
    \$\begingroup\$ Great idea, thank you so much. Sorry for the delayed comment \$\endgroup\$ Commented Sep 14, 2016 at 23:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.