Sign in to follow this  

Implicit conversion??

This topic is 4401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In c#, this is what im doing:
            byte[] temp = new byte[2];
            pChunk.bytesRead = m_FilePointer.Read(temp, 0, 2);
            pChunk.ID = ushort.Parse(temp.ToString());
Why doesnt this work, i get number was not in correct format. Thanks for any help

Share this post


Link to post
Share on other sites
I'm no expert, but are you certain that temp.ToString() is what you expect?

Most times with file reading I've seen some Formatting call used with bytes from files.

[edit: how are you opening the file? binary file, text?]

Share this post


Link to post
Share on other sites
I haven't gotten to file i/o in C# yet, but the output of the following code seems like a good sign of what's wrong:

byte[] temp = new byte[2];
temp[0] = 1;
temp[1] = 2;
System.Console.WriteLine(temp.ToString());

CM

Share this post


Link to post
Share on other sites

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

class Program {
static void Main(string[] args) {
/*if (args.Length < 1) {
return;
}*/

//FileInfo fi=new FileInfo(args[0]);
FileInfo fi = new FileInfo(@"test.txt");
StreamReader fr = fi.OpenText();
ushort us =new ushort();
string s=fr.ReadLine();

Console.WriteLine("s: {0}", s);
us=ushort.Parse(s);
Console.WriteLine("us: {0}", us);

}
}




To parse out multiple shorts on a line, use regex's to split the line string to smaller strings.

[edit: I've known C# for about 3 days, so it's highly plausible that there's a better way]

Share this post


Link to post
Share on other sites

// *snip* above code.
FileStream fs = new FileStream(@"test.bin", FileMode.OpenOrCreate);
BinaryWriter fwb = new BinaryWriter(fs);
fwb.Write((UInt16)us);
fwb.Close();
fs.Close();
Console.WriteLine("Bin-write: {0}", us);
fs = new FileStream(@"test.bin", FileMode.Open);
BinaryReader frb = new BinaryReader(fs);
us=frb.ReadUInt16();
Console.WriteLine("Bin-read: {0}", us);
frb.Close();
fs.Close();





Doing a simple open with a FileInfo does seem to yield improper results, probably because the filestream isn't close()'d nicely before the second call opens it or some buffer isn't flushed or somesuch.

Share this post


Link to post
Share on other sites
I think you guys are making things a little too complicated. The correct solution is relatively easy and painless.

Quote:
Original post by rgirard413
In c#, this is what im doing:

byte[] temp = new byte[2];
pChunk.bytesRead = m_FilePointer.Read(temp, 0, 2);
pChunk.ID = ushort.Parse(temp.ToString());


Why doesnt this work, i get number was not in correct format.

Thanks for any help

Parse is for transforming strings to integers. Try printing the result of temp.ToString() and you'll find that it's probably not what you expected.

Instead, use the BitConverter class ("Converts base data types to an array of bytes, and an array of bytes to base data types.") and its corresponding ToUInt16(byte[]) method.

Remember, UInt16 isn't CLS-compliant, so if you're looking for portability here, stick with Int32.

Hope that helps,
-- k2

Share this post


Link to post
Share on other sites
If you know the endianness, you can do it manually:

pChunk.ID = (temp[0] << 8) | temp[1];

Depending on C#'s semantics for signedness of types and so on, you may need to do something with the second byte in order to have it treated as unsigned. (In Java, you can do it for example as (temp[0] << 8) | (temp[1] & 0xff).) That's for big-endian of course; for little-endian, swap the indices around.

Share this post


Link to post
Share on other sites

This topic is 4401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this