Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by programmrzinc · Feb 17, 2017 at 03:20 PM · arraydictionarybinarybinarywriter

'Too many bytes in what should have been a 7 bit encoded Int32' and idk what I'm doing wrong...

What am I doing wrong? everything is as it should. I'm basically reading and writing a dictionary to the filesystem, and populating some RawImages with the values of the dictionary. Can someone shed some light?

  static void Serialize(string path, Dictionary<string, byte[]> data)
         {
             using (var file = File.Create(path))
             using (var deflate = new DeflateStream(file, CompressionMode.Compress))
             using (var writer = new BinaryWriter(deflate))
             {
                 writer.Write(data.Count);
                 foreach(var pair in data)
                 {
                     writer.Write(pair.Key);
                     writer.Write(pair.Value);                    
                 }
                 writer.Close();
             }
         }
         static Dictionary<string,byte[]> Deserialize(string path)
         {
             using (var file = File.OpenRead(path))
             using (var deflate = new DeflateStream(file, CompressionMode.Decompress))
             using (var reader = new BinaryReader(deflate))
             {
                 int count = reader.ReadInt32();
                 var data = new Dictionary<string, byte[]>();
                 while(count-->0) {
                     data.Add(reader.ReadString(), reader.ReadBytes(count));
                 }
                 reader.Close();
                 return data;
             }
         }






Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by anathemort · Feb 17, 2017 at 05:33 PM

You need to record the length of the pair.Value byte[] so you know how much data to read back out. So in your Serialize method, you would change your loop to be something like this:

 foreach(var pair in data)
 {
     writer.Write(pair.Key);
     writer.Write(pair.Value.Length);
     writer.Write(pair.Value);
 }

Then in your Deserialize, when you read the data:

 int count = reader.ReadInt32();
 var dict = new Dictionary<string, byte[]>(count); //initialize capacity, too

 for(int i = 0; i < count; i++)
 {
     var key = reader.ReadString();
     var len = reader.ReadInt32();

     dict.Add(key, reader.ReadBytes(len));
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image programmrzinc · Feb 17, 2017 at 05:40 PM 0
Share

I did what you said, assigning a length before I read it. Works smoothly. $$anonymous$$uch love lol

  static void Serialize(string path, Dictionary<string, byte[]> data)
         {
             using (var file = File.Create(path))
             using (var deflate = new DeflateStream(file, Compression$$anonymous$$ode.Compress))
             using (var writer = new BinaryWriter(deflate))
             {
                 writer.Write(data.Count);
                 foreach(var pair in data)
                 {
                     writer.Write(pair.$$anonymous$$ey.Length);
                     writer.Write(Encoding.ASCII.GetBytes(pair.$$anonymous$$ey));
                     writer.Write(pair.Value.Length);
                     writer.Write(pair.Value);             
                 }
                 writer.Close();
             }
         }
         static void  Deserialize(string path, Dictionary<string, byte[]> data)
         {
             using (var file = File.OpenRead(path))
             using (var deflate = new DeflateStream(file, Compression$$anonymous$$ode.Decompress))
             using (var reader = new BinaryReader(deflate))
             {
                 int count = reader.ReadInt32();
                 string url = "";
                 byte[] image;
                 while(count-->0) {
                     int urlLength = reader.ReadInt32();
                     url = ASCIIEncoding.ASCII.GetString(reader.ReadBytes(urlLength));
                     int dataLength = reader.ReadInt32();
                     image = reader.ReadBytes(dataLength);
                     if(!data.Contains$$anonymous$$ey(url))
                         data.Add(url, image);
                 }
                 reader.Close();
             }
         }



Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Development of the shop? 1 Answer

Default Null Value for Key Not Found in Dictionary? 1 Answer

Storing level data from a texture file, and drawing at speed. 0 Answers

Is it possible to change the element name in an Array List? 1 Answer

Can Vexe.FastSave save dictionaries to files? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges