View previous topic :: View next topic |
Author |
Message |
Flippeh
Joined: 06 Jun 2007 Posts: 3
|
Posted: Wed Jun 06, 2007 2:19 pm Post subject: Error: 4 invalid UTF-8 sequence [Weird] |
|
|
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 |
|
|
cracki
Joined: 07 May 2007 Posts: 10 Location: Germany
|
Posted: Wed Jun 06, 2007 3:11 pm Post subject: |
|
|
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 |
|
|
Flippeh
Joined: 06 Jun 2007 Posts: 3
|
Posted: Wed Jun 06, 2007 3:14 pm Post subject: |
|
|
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 |
|
|
cracki
Joined: 07 May 2007 Posts: 10 Location: Germany
|
Posted: Wed Jun 06, 2007 7:35 pm Post subject: |
|
|
you change the string before outputting it.
walk it, change chars that are >127. _________________ ...meh... |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Wed Jun 06, 2007 8:16 pm Post subject: Re: Error: 4 invalid UTF-8 sequence [Weird] |
|
|
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 |
|
|
Flippeh
Joined: 06 Jun 2007 Posts: 3
|
Posted: Wed Jun 06, 2007 11:41 pm Post subject: |
|
|
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 |
|
|
|