<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>char and string problem</title>
		<link>http://www.allegro.cc/forums/view/606272</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 03 Feb 2011 02:41:41 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So I was declaring and initializing a char like this: </p><p><span class="source-code"><span class="k1">char</span> example<span class="k3">=</span><span class="n">164</span><span class="k2">;</span></span></p><p>so.. when I do:</p><p><span class="source-code"> std::cout<span class="k3">&lt;</span><span class="k3">&lt;</span>example<span class="k3">&lt;</span><span class="k3">&lt;</span>std::endl<span class="k2">;</span></span> it shows me a &quot;ñ&quot; letter...</p><p>Becuase this: <span class="source-code"><span class="k1">char</span> ene<span class="k3">=</span><span class="s">"ñ"</span><span class="k2">;</span></span>give me an error     <br />and this <span class="source-code"><span class="k1">char</span> ene<span class="k3">=</span><span class="s">'ñ'</span><span class="k2">;</span></span> give me a: ▓</p><p>but since al_draw_text only accepts a pointer, I have to do:</p><p><span class="source-code"><span class="k1">char</span> <span class="k3">*</span>example<span class="k2">;</span></span></p><p>and if I do:</p><p><span class="source-code">example<span class="k3">=</span><span class="n">164</span><span class="k2">;</span></span></p><p>I get an error... (Obviously)</p><p>If I do:</p><p><span class="source-code">example<span class="k3">=</span><span class="s">"Ñ"</span><span class="k2">;</span></span></p><p>the console shows me this: ├æ, two symbols...</p><p>Then if I do:
</p><div class="source-code snippet"><div class="inner"><pre>    <span class="k1">char</span> example<span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k2">;</span>

    example<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k3">=</span><span class="s">'ñ'</span><span class="k2">;</span>
</pre></div></div><p>

I get again this symbol: ▓</p><p>And if I do:</p><p><span class="source-code"> <span class="k1">char</span> example<span class="k2">[</span><span class="k2">]</span><span class="k3">=</span><span class="s">"ñ"</span><span class="k2">;</span></span></p><p>I get this: ├ ... another symbol...</p><p>if I do: <span class="source-code"><span class="k1">char</span> <span class="k3">*</span>example<span class="k3">=</span><span class="s">"ñ"</span><span class="k2">;</span></span><br />I get: ├▒</p><p>So... I don&#39;t know what else to do...</p><p>I just want one <span class="source-code"><span class="k1">char</span></span>, with just one letter...</p><p>The estrange thing is, that if do...</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>
<span class="number">  2</span><span class="p">#include &lt;iostream&gt;</span>
<span class="number">  3</span>
<span class="number">  4</span><span class="p">#include "allegro5/allegro.h"</span>
<span class="number">  5</span><span class="p">#include "allegro5/allegro_font.h"</span>
<span class="number">  6</span><span class="p">#include "allegro5/allegro_ttf.h"</span>
<span class="number">  7</span>
<span class="number">  8</span>
<span class="number">  9</span><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 10</span><span class="k2">{</span>
<span class="number"> 11</span>   <a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">*</span>display <span class="k2">;</span>
<span class="number"> 12</span>   <a href="http://www.allegro.cc/manual/ALLEGRO_FONT"><span class="a">ALLEGRO_FONT</span></a>    <span class="k3">*</span><a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a>    <span class="k2">;</span>
<span class="number"> 13</span>
<span class="number"> 14</span>   <a href="http://www.allegro.cc/manual/al_init"><span class="a">al_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>   <a href="http://www.allegro.cc/manual/al_init_font_addon"><span class="a">al_init_font_addon</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>   <a href="http://www.allegro.cc/manual/al_init_ttf_addon"><span class="a">al_init_ttf_addon</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>
<span class="number"> 18</span>   display <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_display"><span class="a">al_create_display</span></a><span class="k2">(</span><span class="n">300</span>, <span class="n">300</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 19</span>   <a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a> <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_ttf_font"><span class="a">al_load_ttf_font</span></a><span class="k2">(</span><span class="s">"consola.ttf"</span>, <span class="n">40</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>
<span class="number"> 21</span>
<span class="number"> 22</span>    std::string example<span class="k2">;</span>
<span class="number"> 23</span>    <span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span>temp<span class="k2">;</span>
<span class="number"> 24</span>
<span class="number"> 25</span>    example<span class="k3">=</span><span class="s">"ñÑñÑ"</span><span class="k2">;</span>
<span class="number"> 26</span>    temp<span class="k3">=</span>example.data<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 27</span>
<span class="number"> 28</span>    std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> example <span class="k3">&lt;</span><span class="k3">&lt;</span> std::endl<span class="k2">;</span>
<span class="number"> 29</span>
<span class="number"> 30</span>    <span class="k1">while</span><span class="k2">(</span><span class="k1">true</span><span class="k2">)</span><span class="k2">{</span>
<span class="number"> 31</span>
<span class="number"> 32</span>    <a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a>,<a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">255</span>, <span class="n">255</span>, <span class="n">255</span><span class="k2">)</span>,<span class="n">0</span> ,<span class="n">0</span> , <span class="n">0</span>, temp<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 33</span>    <a href="http://www.allegro.cc/manual/al_flip_display"><span class="a">al_flip_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 34</span>    <span class="k2">}</span>
<span class="number"> 35</span>
<span class="number"> 36</span>
<span class="number"> 37</span>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 38</span><span class="k2">}</span>
</div></div><p>

<span class="remote-thumbnail"><span class="json">{"name":"ene.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/d\/9d6cdd902b557784e8ed5d39b9701b4b.jpg","w":423,"h":401,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/d\/9d6cdd902b557784e8ed5d39b9701b4b"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/9/d/9d6cdd902b557784e8ed5d39b9701b4b-240.jpg" alt="ene.jpg" width="240" height="227" /></span></p><p>al_draw_text shows correctly the &quot;ñ&quot; letter... So what can I do?</p><p>I don&#39;t get it what is happening? <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /></p><h3>Conclusion:</h3><p>
What is happening is that the console is using Extended ASCII, and std::string was not made to use UTF-8, that was the whole problem... it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implications E.g. you can&#39;t count the amount of characters by length() | size(). Instead one has to iterate through the string, parse all UTF-8 multibytes and count each multibyte as one character.</p><p>For that reason is better to use the Allegro UTF-8 API...</p><p>If I do <span class="source-code"><span class="k1">char</span> temp<span class="k2">[</span><span class="k2">]</span><span class="k3">=</span><span class="k2">{</span><span class="n">0xC3</span> ,<span class="n">0xB1</span>,<span class="s">'\0'</span><span class="k2">}</span><span class="k2">;</span></span> Allegro shows me perfectly the &quot;ñ&quot; letter since those are the bits (represented that way in Hex) that correspond to the &quot;ñ&quot; in UTF-8.</p><p>The console still don&#39;t knowing what is happening so it shows me &quot;├▒&quot; but that is fine, I won&#39;t use the console...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AMCerasoli)</author>
		<pubDate>Wed, 02 Feb 2011 01:30:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Did you try<br /><span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a>,<a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">255</span>, <span class="n">255</span>, <span class="n">255</span><span class="k2">)</span>,<span class="n">0</span> ,<span class="n">0</span> , <span class="n">0</span>, example.c_str<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Wed, 02 Feb 2011 01:35:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your console and your code are using different character encodings.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Wed, 02 Feb 2011 01:38:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://">Mark Oates</a> said:</div><div class="quote"><p>Did you try<br />al_draw_text(font,al_map_rgb(255, 255, 255),0 ,0 , 0, example.c_str());</p></div></div><p>

Thanks man, I didn&#39;t know that...</p><div class="quote_container"><div class="title"><a href="http://">gnolam</a> said:</div><div class="quote"><p>Your console and your code are using different character encodings.</p></div></div><p>

Well yes... Code::Blocks is using UTF-8... and the console I don&#39;t know... </p><p>The problem is that, when I do <span class="source-code">example.erase<span class="k2">(</span>temp.length<span class="k2">(</span><span class="k2">)</span><span class="k3">-</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></span> to delete one charter from the string works fine when using normal charters, but if I use the &quot;ñ&quot; charter, since it add two charters ( ex:├▒ ) the erase function thinks that there should be just one charter (and it erase just one charter) when actually there are two... So remains one charter there... where? I dunno... And when I type the next charter it breaks (al_draw_text).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AMCerasoli)</author>
		<pubDate>Wed, 02 Feb 2011 02:07:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Long story short, your code editor and output device (terminal or command prompt window) are using different character sets or encodings so the data comes out wrong. You need to sync them up so that they&#39;re using the same formats. To do it in a proper way that should work on all machine configurations, you&#39;ll need to use an API that knows how to convert to and from the internal and external formats. This often requires determining (or guessing) the external format, as well as choosing your internal format.</p><p>It can be very complicated and I still haven&#39;t figured it out yet so I can&#39;t really give you any code advice. However, if you get everything speaking the same language then you shouldn&#39;t notice that your program is character-encoding-stupid, at least not on your own machine(s). If you actually intend to release the program for others to use then it&#39;s best to do it right though.</p><p>Generally, Unicode is the preferred character set these days because it can basically cover every human language (and then some) that you&#39;d ever want to support. There are some exceptions still I think, but it&#39;s still a growing standard. There are many ways that Unicode can be represented in a computer however. Among the most popular are UTF-8 and UTF-16. UTF-8 is good where space or bandwidth are more limited than processing power, and UTF-16 (or UTF-32) are more preferred where space is less of an issue than processing power is. There are also some languages that are only represented in 4 or more bytes, so using something like UTF-8 for them won&#39;t save you anything (and will actually cost you space). In any case, UTF-8 is a great choice for a character encoding because it&#39;s ASCII compatible, which means most editors and compilers will already understand the important characters. The characters above the ASCII range often don&#39;t matter to something like a compiler, so it doesn&#39;t hurt to use UTF-8 anyway. It might be easier to use UTF-16 (two-byte wide characters) in memory though to represent the characters to make processing strings easier, but only if you actually need to process characters. That&#39;s basically where your current program is going wrong. You&#39;re trying to process characters, assuming a character is a single-byte (or <span class="source-code"><span class="k1">char</span></span>), when in reality a character can be 1 byte or it can be 6 bytes. You don&#39;t know until you check the string, byte-by-byte. You basically need to use an API that is aware of the character encoding.</p><p>Long story short, if you can, get everything speaking UTF-8: your text editor and your terminal or command prompt window. If you have the time, look for tutorials on how to write character set and character encoding aware programs (and report back to us with the results <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />). <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /> I think that parts of Allegro 5 are Unicode aware so maybe you can just use the Allegro 5 API to do it right, but I haven&#39;t learned those APIs yet so I&#39;m no help with that either. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Wed, 02 Feb 2011 02:22:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m assuming you&#39;re saving your source as utf-8.  That&#39;s probably the best way to do it when using Allegro.</p><p>Try setting your console to utf-8, the command is &#39;chcp 65001&#39;.</p><p>And if you have trouble with &#39;extended&#39; characters in strings, you can use hexadecimal escape codes in strings, like:<br /><span class="source-code"><span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span> <span class="k3">=</span> <span class="s">"A\x42C"</span><span class="k2">;</span>  <span class="c">// "ABC"</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 02 Feb 2011 02:33:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606272/901513#target">torhu</a> said:</div><div class="quote"><p>
Try setting your console to utf-8, the command is &#39;chcp 65001&#39;.
</p></div></div><p>
For some reason, this completely foobars my console in my XP VM. Vim won&#39;t run when I do that, and it even insta-crashed the entire VM once. <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Wed, 02 Feb 2011 02:35:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I could change the CHCP but just temporally... And now I realize that on Windows 7 what my program does is just show &quot;I&quot; charters and things like that, and the CHCP of my windows 7 is the same in this PC (Win Vista)... both says &quot;Active code page: 850&quot;... What might be happening? can I change the CHCP at run time?</p><p>In my XP machine also runs fine, and the code page is 435, or something like that...</p><p>I&#39;m going to check the UTF-8 API of Allegro, but I think the problem is other...</p><p>PS: I Attached an example, if someone want to test it to see what happens... is an .exe</p><p><b>EDIT:</b> Well I think I&#39;m gonna say god bay to std::string, I&#39;m using the al_draw_ustr instead... and I think is pretty much the same compared with std::string... the problem is that I&#39;m going to need to save some text to disk... So... Lets see</p><p><b>EDIT Again:</b> It hasn&#39;t a function like std::string.data()? or std::string.c_str()?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AMCerasoli)</author>
		<pubDate>Wed, 02 Feb 2011 06:32:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Joel Spolsky (a rather well known developer and blogger) wrote a <a href="http://www.joelonsoftware.com/articles/Unicode.html">pretty good article on the use of Unicode</a> that I think somewhat tries to explain how to do things &quot;right&quot;. It would have been nice if he&#39;d have shown some programming code, preferably C, to back up the article, but it&#39;s a good read anyway.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Wed, 02 Feb 2011 06:53:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606272/901573#target">AMCERASOLI</a> said:</div><div class="quote"><p>
EDIT Again: It hasn&#39;t a function like std::string.data()? or std::string.c_str()?
</p></div></div><p>
<span class="source-code"><a href="http://www.allegro.cc/manual/al_cstr"><span class="a">al_cstr</span></a></span> and <span class="source-code"><a href="http://www.allegro.cc/manual/al_cstr_dup"><span class="a">al_cstr_dup</span></a></span> respectively.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Wed, 02 Feb 2011 07:54:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/606272/901489#target">AMCERASOLI</a> said:</div><div class="quote"><p>
I just want one char, with just one letter...
</p></div></div><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span> array<span class="k2">[</span><span class="n">2</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span><span class="n">164</span> , <span class="s">'\0'</span><span class="k2">}</span>
<a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a><span class="k2">(</span>al_font , <a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">255</span>,<span class="n">255</span><span class="k2">)</span> , x , y , ALLEGRO_ALIGN_LEFT , array<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 02 Feb 2011 10:18:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://">bamccaig</a> said:</div><div class="quote"><p>Joel Spolsky (a rather well known developer and blogger) wrote a pretty good article on the use of Unicode [www.joelonsoftware.com] that I think somewhat tries to explain how to do things &quot;right&quot;. It would have been nice if he&#39;d have shown some programming code, preferably C, to back up the article, but it&#39;s a good read anyway.</p></div></div><p>

Really really thank you... You opened my mind. Everyone should read that article.</p><div class="quote_container"><div class="title"><a href="http://">Edgar Reynaldo</a> said:</div><div class="quote"><p>char array[2] = {164 , &#39;\0&#39;}<br />al_draw_text(al_font , al_map_rgb(255,255,255) , x , y , ALLEGRO_ALIGN_LEFT , array);</p></div></div><p>

That doesn&#39;t work because the &quot;164&quot; Keystroke belongs to the Extended ASCII and I&#39;m using UTF-8... I don&#39;t know if I&#39;m wrong but since the &quot;ñ&quot; letter uses 2 bytes I can&#39;t store it like other letters... </p><p>What is happening is that the console is using Extended ASCII, and std::string was not made to use UTF-8, that was the whole problem... it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implications E.g. you can&#39;t count the amount of characters by length() | size(). Instead one has to iterate through the string, parse all UTF-8 multibytes and count each multibyte as one character.</p><p>For that reason is better to use the Allegro UTF-8 API...</p><p>If I do <span class="source-code"><span class="k1">char</span> temp<span class="k2">[</span><span class="k2">]</span><span class="k3">=</span><span class="k2">{</span><span class="n">0xC3</span> ,<span class="n">0xB1</span>,<span class="s">'\0'</span><span class="k2">}</span><span class="k2">;</span></span> Allegro shows me perfectly the &quot;ñ&quot; letter since those are the bits (represented that way in Hex) that correspond to the &quot;ñ&quot; in UTF-8.</p><p>The console still don&#39;t knowing what is happening so it shows me &quot;├▒&quot; but that is fine, I won&#39;t use the console...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AMCerasoli)</author>
		<pubDate>Thu, 03 Feb 2011 02:41:41 +0000</pubDate>
	</item>
</rss>
