14 Replies - 9561 Views - Last Post: 01 September 2012 - 02:44 AM Rate Topic: -----

#1 nesa24casa  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 25-August 12

C# BinaryReader/Writer Strings with delimiter

Posted 28 August 2012 - 06:37 AM

Hello,
how can i use BinaryReader/Writer to read/write delimited (splited) strings?

Example:
File has value E.n.e.m.y.
How to read it as Enemy in Binaryreader (to textbox) from file ?
and
How to write Noone as N.o.o.n.e. with BinaryWriter ( from textbox) to file ?

Thanks in advance
Best regards
Is This A Good Question/Topic? 0
  • +

Replies To: C# BinaryReader/Writer Strings with delimiter

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# BinaryReader/Writer Strings with delimiter

Posted 28 August 2012 - 07:13 AM

BinaryReader:

http://msdn.microsof...(v=vs.100).aspx

Binary Writer:

http://msdn.microsof...(v=vs.100).aspx

Give it a try yourself. Those pages have literally all the information you need to do the task.

Also, "noone" is an incorrect spelling of "no one".
Was This Post Helpful? 0
  • +
  • -

#3 nesa24casa  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 25-August 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 28 August 2012 - 07:28 AM

Enemy and Noone are just examples ( random typed )
it looks like string.join/split will do a trick

string[] test = { "N", "o", "o", "n", "e" };
string.Join(",", test);


also Split and Insert
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 28 August 2012 - 10:19 AM

Your call to Join() will fail to put a trailing delimiter like you have in your original post:

View Postnesa24casa, on 28 August 2012 - 06:37 AM, said:

Example:
File has value E.n.e.m.y.
How to read it as Enemy in Binaryreader (to textbox) from file ?
and
How to write Noone as N.o.o.n.e. with BinaryWriter ( from textbox) to file ?


And I think you'll be confused by the results of Split() for the string "Wil.i.am": W.i.l...i...a.m.

This post has been edited by Skydiver: 28 August 2012 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 28 August 2012 - 10:24 AM

Also, if it's a text file, why do you need to use BinaryReader/Writer?

If it's not a text file, but actually binary file and you are trying to modify (aka "hack") the resources embedded within the file, then you should take a closer look at the hex dump that you are basing your code on. Chances are that it is a Unicode string and what you think are '.' on the hex dump are actually bytes with the value of zero.
Was This Post Helpful? 2
  • +
  • -

#6 nesa24casa  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 25-August 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 01:41 AM

its binary file not text.
i am using hexedit tool method and its . not empty (00).
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 02:01 AM

Okay, if you say so...
Attached Image
Was This Post Helpful? 1
  • +
  • -

#8 nesa24casa  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 25-August 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 03:52 AM

View PostSkydiver, on 29 August 2012 - 02:01 AM, said:

Okay, if you say so...
Attachment Capture.PNG


I don't understand this post as i said its . not empty(00)
E.n.e.m.y.
452E6E2E652E6D2E792E

i know about hexing i just need some help. this is forum. its purpose is to help and not to troll someone.
Was This Post Helpful? -1
  • +
  • -

#9 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 06:26 AM

Nobody's trolling you. He's suggesting that you might have been wrong. If you're sure you're not, then you're not. It's not a bad thing for someone to question you, is it? People questioning you make you a better programmer.
Was This Post Helpful? 1
  • +
  • -

#10 nesa24casa  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 25-August 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 06:44 AM

View PostCurtis Rutland, on 29 August 2012 - 06:26 AM, said:

Nobody's trolling you. He's suggesting that you might have been wrong. If you're sure you're not, then you're not. It's not a bad thing for someone to question you, is it? People questioning you make you a better programmer.


I know that it makes me better but i have posted before that its binary not txt
and that its . not empty (00)

I just need little assitance over this issue.
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 07:01 AM

Well, if you still need assistance, you need to ask a new question. Because the last post you made said you had basically solved the issue. If you're still in need of help, let us know what's holding you up.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 07:36 AM

Perhaps if you provided an example of the file, this could be solved quickly. It doesn't seem to make much sense to have a file in the format you've described. Is there any possibility that perhaps someone viewed the file in a hex editor, then copied and pasted the text into another file yielding the .-separated characters?
Was This Post Helpful? 0
  • +
  • -

#13 nesa24casa  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 25-August 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 08:05 AM

View PostJackOfAllTrades, on 29 August 2012 - 07:36 AM, said:

Perhaps if you provided an example of the file, this could be solved quickly. It doesn't seem to make much sense to have a file in the format you've described. Is there any possibility that perhaps someone viewed the file in a hex editor, then copied and pasted the text into another file yielding the .-separated characters?

Its not case here. .dat file can with software in that way. 2E(.) as char space and not 00(.).
Well i have solved issue with split/join/insert thing just needed to see maybe someone know a better way to do it.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 29 August 2012 - 12:17 PM

A simple for loop to extract or write will be faster and safer. You don't have to worry about the weird case of embedded periods in the incoming string, or the need to append an extra period at the end of the outgoing string. Additionally, you don't have to pay the price of overhead of having multiple strings and managing those strings which using Split() and Join().

Since you already have code that works, I won't feel guilty about presenting this code. It assumes that the string formed without the dots in the .DAT file is UTF8 encoded, but shouldn't be too difficult to refactor to let the class have an encoding property exposed.

class DotEncoded
{
    const byte Delimiter = 0x2E;

    public string ReadString(BinaryReader reader, int count)
    {
        byte[] raw = reader.ReadBytes(count);
        byte[] trimmed = new byte[raw.Length / 2];
        for (int i = 0; i < trimmed.Length; i++)
        {
            trimmed[i] = raw[i * 2];
            Debug.Assert(raw[i * 2 + 1] == Delimiter, "Expected to see delimiter");
        }
        return Encoding.UTF8.GetString(trimmed);
    }

    public int WriteString(BinaryWriter writer, string data)
    {
        byte[] encoded = Encoding.UTF8.GetBytes(data);
        byte[] raw = new byte[encoded.Length * 2];

        for (int i = 0; i < encoded.Length; i++)
        {
            raw[i * 2] = encoded[i];
            raw[i * 2 + 1] = Delimiter;
        }
        writer.Write(raw);
        return raw.Length;
    }
}


This post has been edited by Skydiver: 29 August 2012 - 12:18 PM

Was This Post Helpful? 1
  • +
  • -

#15 nesa24casa  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 25-August 12

Re: C# BinaryReader/Writer Strings with delimiter

Posted 01 September 2012 - 02:44 AM

Thanks for this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1