473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Binary Reader ReadInt32() reading value WAAAY too high?

I'm working on a project in C# (personal, I don't take programming in school, I do it for fun)
In my program you can save the project you're working on, containing information and images, I've been trying to save it to a single binary file, and up until a little while ago it worked, IDK what changed :/
Here's the save/open function:
_______________________
Expand|Select|Wrap|Line Numbers
  1.  public static void Save(string fname)
  2.  {
  3.  int length = 0;
  4.  byte[] bytes;
  5.  BinaryWriter bw = new BinaryWriter(File.Open(fname, FileMode.Create));
  6.  
  7.  bw.Write(FileVersion);
  8.  bw.Write(ProjectName);
  9.  bw.Write(count_images);
  10.  bw.Write(count_actors);
  11.  bw.Write(count_areas);
  12.  
  13.  
  14.  bw.Write(Images.Count-1);//images
  15.  
  16.  foreach (project_image pi in Images)
  17.  {
  18.  pi.bitmap.Save("tmp.jpeg",
  19.  ImageFormat.Jpeg);
  20.  bytes = File.ReadAllBytes("tmp.jpeg");
  21.  length = bytes.Length;
  22.  bw.Write(length);
  23.  bw.Write(pi.ImageName);
  24.  bw.Write(bytes);
  25.  }
  26.  
  27.  File.Delete("tmp.jpeg");
  28.  
  29.  bw.Write(Actors.Count);//actors
  30.  
  31.  foreach (project_actor pa in Actors)
  32.  {
  33.  bw.Write(pa.Name);
  34.  bw.Write(pa.Image);
  35.  bw.Write(pa.Visible);
  36.  }
  37.  
  38.  bw.Close();
  39.  }
  40.  
  41.  public static ProjectLoadReturn Open(string fname)
  42.  {
  43.  ProjectLoadReturn plr = new ProjectLoadReturn();
  44.  
  45.  int c = 0;
  46.  int l = 0;
  47.  byte[] bytes;
  48.  project_image pi;
  49.  TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitma…
  50.  
  51.  BinaryReader br = new BinaryReader(File.Open(fname, FileMode.Open));
  52.  
  53.  if (br.ReadInt32() == FileVersion)
  54.  {
  55.  ProjectName = br.ReadString();
  56.  count_images = (short)br.ReadInt32();
  57.  count_actors = (short)br.ReadInt32();
  58.  count_areas = (short)br.ReadInt32();
  59.  
  60.  c = br.ReadInt32();
  61.  for (int i = 0; i < c; i += 1)
  62.  {
  63.  pi = new project_image();
  64.  l = br.ReadInt32();
  65.  pi.ImageName = br.ReadString();
  66.  bytes = br.ReadBytes(l);
  67.  pi.bitmap = (Bitmap)tc.ConvertFrom(bytes);
  68.  Images.Add(pi);
  69.  plr.Images.Add(pi.ImageName);
  70.  pi = null;
  71.  }
  72.  }
  73.  else
  74.  plr.WrongVersion = true;
  75.  
  76.  br.Close();
  77.  
  78.  return plr;
  79.  
  80.  }
_____________________
For some reason both variables c & l in the open function are astronomically high compared to where they should be eg; 1762000896 and 1701273965 respectively ; c should have been 2 and l should have been ALLOT lower.

I'm hoping this is just a stupid mistake on my part, I'm not TOO worried as this isn't a school thing or anything so I'm not on a time frame, but I'd like to continue working on my program!

Thanks for any help :D
Nov 11 '12 #1

✓ answered by CharlesSeventy7

Sorry, figured it out, I was saving as a short and trying to read as an int :S

1 2551
Sorry, figured it out, I was saving as a short and trying to read as an int :S
Nov 11 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: huzz | last post by:
How can i access arraylist value as (int) i want access value of the index 4 of the arraylist shown below. Any suggestions? Permissions {Length=8} : {1} : {1} : "Awlad Hussain"
4
by: Mateo | last post by:
Hi! I have labels on my page (some are web forms controls, and some are plain HTML labels), and I need Mozilla compatibile way for reading label value. For example, in IE I can use this:...
4
by: Burns | last post by:
Hi I've just tried the following code FileStream objFS = new FileStream(strFileName, FileMode.Open, FileAccess.Read) BinaryReader objReader = new BinaryReader(objFS) UInt32 intFirst =...
7
by: nick.stefanov | last post by:
Ok having another issue with binary reader. My file contains values 08 8A . When I read ReadBytes(2) what gets stored in the array is = 8 and = 138. How do I convert that to 2186 integer...
1
by: Andrew Banks | last post by:
I've got the following code in a control and the reader doesn't seem to be working. The connection details are correct. Any ideas? I am reading data from this database to log the user and a lot...
6
by: james | last post by:
I will try to make this short and to the point. I am reading values from a file using a Binary Reader. At a particular address I can get a value of , Hex 10 or Hex 01 . (varies can be 30, 20, 02,03...
7
by: gene kelley | last post by:
I have an application where I need to read some header information found in 3 different types of file types. Two of the file types were fairly straight forward as the items to read in the header...
2
by: JimmyKoolPantz | last post by:
I came across a problem today and I'm not sure what my possible solutions are. The situation is I am am using a binary reader to read data from a filestream. The data source is a dbf file. ...
6
by: Melih Onvural | last post by:
I need to execute some javascript and then read the value as part of a program that I am writing. I am currently doing something like this: import htmllib, urllib, formatter class...
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.