Rui Moraes

Hello Guys.

I'm only searching ab out file conversion wth .net. To get a file .txt and transform into a .bin file. Is there a sample with other formats

Thanks by attention.



Re: Visual C# General Is there a way to convert a txt file into a bin file?

timts

Do you mean to read the file as txt then write a new file in binary mode

you can read the file with StreamReader and write the new file with BinaryWriter.






Re: Visual C# General Is there a way to convert a txt file into a bin file?

Rui Moraes

Well,

In truly, I'd like to only convert the file (without to read). Because another specific application would read the information about this file content.

Thanks by attention.

Rui





Re: Visual C# General Is there a way to convert a txt file into a bin file?

IsshouFuuraibou

Do you mean just changing the extension There would be no way to change the inherent structure of the file without first knowing what's in the file (what the read is for).

You can just do a File.Move or a File.Copy to change the extension programatically. Aside from that you'll have to tell us more about what you're intending to do. The biggest problem will likely be if this other application is expecting a specific file format. This is often the case for special file extensions, being that bin is a broad file extension (could be a disc image, linux executable, binary file, etc) we can't say if it would have the same structure as just a renamed .txt file or if it would have a specific bit layout it needs to adhere to.





Re: Visual C# General Is there a way to convert a txt file into a bin file?

Peter Ritchie

What do you mean by "convert" Will the data in the "bin" file be the same, or does it need to be translated or converted in any way to be "binary"






Re: Visual C# General Is there a way to convert a txt file into a bin file?

Rui Moraes

Hello guys,

Sorry to answer too late.

Weel, I'm trying to explain.

I'm sorry about my english Smile.

For example.

I have a file called "helloworld.txt", which has a line with some information, like a bar code.

And there's a application from a data collector (I don't if there's a model XTM in US) that only read a file in a .BIN format. Because the device only accept applications created by a modified language of BASIC (BASIC not Visual Basic rs), called XPBASIC.

If the .NET offers a option only to convert the content file from .txt to .bin, I'd would find some way, or, find another way (who knows in C++).

Thanks by Attention and sorry about my english.





Re: Visual C# General Is there a way to convert a txt file into a bin file?

H. Tony

Could you show us what format this 'helloworld.txt' is in and what is the requirement for .BIN format If the content is the same, you can just use File.Move to rename the file, otherwise it's impossible NOT to read the file, convert it to the desired format, then write the new file out.

Rui Moraes wrote:

Hello guys,

Sorry to answer too late.

Weel, I'm trying to explain.

I'm sorry about my english Smile.

For example.

I have a file called "helloworld.txt", which has a line with some information, like a bar code.

And there's a application from a data collector (I don't if there's a model XTM in US) that only read a file in a .BIN format. Because the device only accept applications created by a modified language of BASIC (BASIC not Visual Basic rs), called XPBASIC.

If the .NET offers a option only to convert the content file from .txt to .bin, I'd would find some way, or, find another way (who knows in C++).

Thanks by Attention and sorry about my english.






Re: Visual C# General Is there a way to convert a txt file into a bin file?

Rui Moraes

Of Course!!

For example.

This file would have a ID of some Product (only numbers).

Example:

Code Snippet

12545687

And this device only prints this information.

Thanks by attention.





Re: Visual C# General Is there a way to convert a txt file into a bin file?

Rui Moraes

Thanks Tony,

When I arrive at home, I'll make some tests with your suggestion.

Thanks. Big Smile





Re: Visual C# General Is there a way to convert a txt file into a bin file?

H. Tony

So the input .txt file contains the ID of some product in TEXT format, what about the format for .BIN file is it a binary number for the same product ID

Rui Moraes wrote:

Of Course!!

For example.

This file would have a ID of some Product (only numbers).

Example:

Code Snippet

12545687

And this device only prints this information.

Thanks by attention.






Re: Visual C# General Is there a way to convert a txt file into a bin file?

Rui Moraes

Yes,

It's the .bin file will have the same ID Product content.

The conversion is necessary because the device only reads .bin files Smile





Re: Visual C# General Is there a way to convert a txt file into a bin file?

Peter Ritchie

Do you mean you need to convert the text "1234567" to an integer and store the integer (i.e. 4 bytes) in the binary file






Re: Visual C# General Is there a way to convert a txt file into a bin file?

Rui Moraes

Yes, this way I'm studying.





Re: Visual C# General Is there a way to convert a txt file into a bin file?

ChunSheng Tang - MSFT

Hi, Rui

Are you seeking for BitConverter.GetBytes

Code Snippet

int i = 1234567;

//Convert to 4 bytes
byte[] data = BitConverter.GetBytes(i);

//Convert it back.
int j = BitConverter.ToInt32(data, 0);

How to write the binary data to the bin file should depend on its format.

Best Regards

Chunsheng Tang