How do you read a ANSI encoded files and use it with wxString? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
david_mtl
Earned a small fee
Earned a small fee
Posts: 20
Joined: Fri Jan 10, 2020 4:13 pm

How do you read a ANSI encoded files and use it with wxString?

Post by david_mtl »

So, I've been searching for the way to do it and I just can't find it. I'm sure I am missing something trivial.

I'm reading a binary a file where characters are encoded in ANSI (8 bit characters). I want to show their content in a wxString.

For exemple, I read the letter "é":
- fread gives me a char, the letter "é" value is -63 because it is an signed type.
- I cannot use this character directly inside a wxString, it is not valid because it is a character encoded on 8 bit. wxString is empty.

Is there a simple way to do it? Do I need to convert all the time to an of wchar_t first than use that instead?

I'd like to know how you people manage this.

Thx again, sorry if I'm missing something trivial.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How do you read a ANSI encoded files and use it with wxString?

Post by doublemax »

First of all: Unless it's UTF-8 there is no easy way to reliably detect an 8 bit text encoding. Assuming you know the file is ISO8859-1 encoded:

Code: Select all

wxFile f_in( "d:\\some_file.txt" );
wxString s;
f_in.ReadAll( &s, wxConvISO8859_1);
f_in.Close();
wxLogMessage("%s", s);
wxConvISO8859_1 is one of the stock conversion objects, there are also a few others:
https://docs.wxwidgets.org/trunk/overvi ... nv_classes

You can use them also when using the respective wxString contructor:
https://docs.wxwidgets.org/trunk/classw ... 4651d98123
Use the source, Luke!
david_mtl
Earned a small fee
Earned a small fee
Posts: 20
Joined: Fri Jan 10, 2020 4:13 pm

Re: How do you read a ANSI encoded files and use it with wxString?

Post by david_mtl »

Code: Select all

wxString name = wxString(n.c_str(), wxConvISO8859_1);
worked like a charm, thanks a lot for your help!
Post Reply