Search Unity

How do you convert a text String to Byte Array?

Discussion in 'Scripting' started by msmooshoo, Feb 10, 2009.

  1. msmooshoo

    msmooshoo

    Joined:
    Feb 8, 2009
    Posts:
    15
    Hello. I'm trying to convert a String to a Byte Array, but the WWW.data and TextAsset.text properties are read-only. Does anyone know how to do this?
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
  3. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    Old thread I know, but these days, it complains that this is a char array, not a byte array. I would think a direct cast might work, unless 'char' is double or multibyte, and Unicode is the result of ToCharArray(). After some teeth pulling:

    var e : UnicodeEncoding = new UnicodeEncoding();
    var upload = new WWW(url, e.GetBytes(WWW.EscapeURL(s).ToCharArray()));
     
    Last edited: Dec 10, 2010
  4. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    If it helps, here's how we're converting images to base64 string on the server and from base64 string in Unity:

    Code (csharp):
    1. // php on the server to convert an image file to base64 string
    2. base64_encode ( file_get_contents("someImageFileName") );
    Code (csharp):
    1. //and in unityscript, converting base64 string to a byte array
    2. // for example, to use with Texture2D.LoadImage())
    3. static function Base64StrToByteArray (str : String) : byte[] {
    4.     return System.Convert.FromBase64String (str);
    5. }
     
  5. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    You guys all seem to do it in the most convoluted way possible, here's the simplest way:


    var bytes = System.Text.Encoding.UTF8.GetBytes(myString);


    And to get the string back:


    System.Text.Encoding.UTF8.GetString(bytes);
     
  6. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    @fholm
    Easy on the snark, there are a variety of needs reflected here, not all covered by your solution.

    That said, yours is another way :)
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You guys do realize this thread is almost 3 years old right?
     
  8. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    This doesn't work for §.

    string testStr = "Testing §";
    byte[] testByteArray = System.Text.Encoding.UTF8.GetBytes(testStr);
    testStr = System.Text.Encoding.UTF8.GetString(testByteArray);
    Debug.Log("result: " + testStr);


    running it yields:
    result: Testing ??


    Does anyone know how I can make that character properly convert into a byte array? I have to send it as part of the postData in a WWW object.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Running that code here yields "result: Testing §". Use UTF-8 encoding for your script, not ASCII.

    It's not any less relevant today.

    --Eric
     
  10. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    Ok, but I thought that's what I was doing. I am using Encoding.UTF8 not Encoding.ASCII.
    unless you mean the
    string testStr = "Testing §";
    is in ascii.
    I have also tried:
    string testStr = "Testing " + System.Convert.ToChar(167);
    and
    string testStr = "Testing \u00A7";

    both yield the same result. it converts it to ??
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I said use UTF-8 encoding for your script, I'm not talking about the code. However, using string testStr = "Testing \u00A7"; and an ASCII script also works here, so I guess it's something else.

    --Eric
     
  12. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    Are you talking about encoding the entire file that this happens in UTF-8? How do I do that in monodevelop.


    ... im confused.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I expect it's an option in the preferences somewhere (I don't have it running at the moment). But anyway, as I said, that doesn't seem to actually be the issue, since using "\u00A7" in an ASCII script works here. (Using "§" in an ASCII script does not work though.)

    --Eric
     
  14. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    618
    Make sure the Code/Text Editor you're using is saving the source file itself in UTF-8 or other unicode friendly script. A number of Windows editors default to ANSI.

    *edit*
    Missed the lower two posts; yea, same deal though :)
     
  15. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    I see that too.

    Unfortunately my server is still getting ?? on the other end.

    Debug.Log("BEFORE THE CONVERSION:\n\n" + envelope);
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(envelope);
    string tempStr = System.Text.Encoding.UTF8.GetString(bytes);
    Debug.Log("AFTER THE CONVERSION:\n\n" + tempStr); //the delimiting character still looks like §
    return new WWW(SoapUrl, System.Text.Encoding.UTF8.GetBytes(envelope), headers);


    The game I am working on is a port of a game on a different platform. If I can't get this character to work, I'll have to go and change the other version of the game, the server, and the existing entries in the database. If anyone has any further insight it would be greatly appreciated.
     
  16. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    I checked and indeed the files were in Latin ISO... I changed the related files to UTF-8, though since Im using \u00A7 it d/n really matter, and indeed I am having the same problem. :-/
     
  17. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    618
    You may need to backspace over the characters and type them again in your source file.
     
  18. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    I found the solution.

    After verifying that it wasn't the server's fault (it was getting ?? at its entry point), I almost threw my hands up and rebuilt the server for a hacktastic special case of changing any ?? it finds to §, I went back to look at the actual web request and unity's WWW class documentation. It turns out I needed a little more information in one of the headers.

    Here is what I did:

    Hashtable headers = new Hashtable();

    //headers["Content-Type"] = "text/xml;"; //<--------------------- what I had originally
    headers["Content-Type"] = "text/xml; charset=utf-8"; //< ---- what fixed it
    return new WWW(SoapUrl, System.Text.Encoding.UTF8.GetBytes(envelope), headers);