FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Error: 4 invalid UTF-8 sequence [Weird]

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
Flippeh



Joined: 06 Jun 2007
Posts: 3

PostPosted: Wed Jun 06, 2007 2:19 pm    Post subject: Error: 4 invalid UTF-8 sequence [Weird] Reply with quote

Heya =)
I am building up a small IRC Client, but something goes horribly wrong.
I know WHATS wrong, but i don't know how to fix.

Code first:
Code:
private import std.stdio;
private import std.socket;
private import std.socketstream;
private import std.string;

const int PORT = 6667;

void main(char[][] args){
    char[] host = "irc.german-freakz.net";

    // open a socket:
    InternetHost ih = new InternetHost;
    ih.getHostByName(host);
    InternetAddress ia = new InternetAddress(ih.addrList[0], PORT);
    TcpSocket sock = new TcpSocket();
    sock.connect(ia);


    char[] line;
    SocketStream stream = new SocketStream(sock);

    sock.send("USER DBot flippeh.de stockholm.se.quakenet.org :i iz a bawt\r\n");
    sock.send("NICK DBot3\r\n");

    while ((line = stream.readLine()) != null) {
        if (!stream.eof()) {
            writef("%s\n", line);
        }
    }
}


Okay, its connection and outputting really nice.
But then there is the problem. If the line to read contains german characters (ae oe ue), it crashes. How do i fix that? =/
Back to top
View user's profile Send private message
cracki



Joined: 07 May 2007
Posts: 10
Location: Germany

PostPosted: Wed Jun 06, 2007 3:11 pm    Post subject: Reply with quote

D's standard output can only handle utf8.
you try to output something other than utf8.
you need to transform your strings. simplest solution: just escape everything that's outside of the range of ascii (0-127).
_________________
...meh...
Back to top
View user's profile Send private message
Flippeh



Joined: 06 Jun 2007
Posts: 3

PostPosted: Wed Jun 06, 2007 3:14 pm    Post subject: Reply with quote

Erm well, i cannot escape them...
When i connect the server sends me a dozen of MOTD messages etc.. i can't modify them.. as soon as they arrive, the application dies.

Edit:
Code:
while ((line = stream.readLine()) != null) {
        try {
            if (!stream.eof()) {
                if (!line < 127) {
                    writef("%s\n", line);
                }
            }
        } catch {

        }
    }


Seems to work, as any message containing the stuff gets simply ignored. How can i escape it, tho?
Back to top
View user's profile Send private message
cracki



Joined: 07 May 2007
Posts: 10
Location: Germany

PostPosted: Wed Jun 06, 2007 7:35 pm    Post subject: Reply with quote

you change the string before outputting it.
walk it, change chars that are >127.
_________________
...meh...
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Wed Jun 06, 2007 8:16 pm    Post subject: Re: Error: 4 invalid UTF-8 sequence [Weird] Reply with quote

Dirclib (IRC library for D) handles this nicely, by converting invalid UTF-8 from latin-1 to UTF-8. Many clients do similar.
Back to top
View user's profile Send private message
Flippeh



Joined: 06 Jun 2007
Posts: 3

PostPosted: Wed Jun 06, 2007 11:41 pm    Post subject: Reply with quote

I can't find ANY substring method..
How can i check single signs in a char?

... god, D is so hard to start for someone who doesn't know everything about it.

Edit: Got it.
But its not working...

Code:
   for (int i = 0; i < szLine.length; i++) {
                if (cast(int)szLine[i] > 127) {
                    writef("Invalid: %d", szLine[i]);
                }
            }


It doesnt output anything
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group