-2

I am just trying to print a shape filled with filled ASCII boxes into the console, but the output is just garbage text. Here is my code:

#include <stdio.h>

const char shade_block[] = {
    "▒▒██████▒▒\n\
     ▒████████▒\n\
     ██████████\n\
     ▒████████▒\n\
     ▒▒██████▒▒\n\
    "};

int main()
{
    printf(shade_block);
    return 0;
}

And here is the output:

ΓûÆΓûÆΓûêΓûêΓûêΓûêΓûêΓûêΓûÆΓûÆ
         ΓûÆΓûêΓûêΓûêΓûêΓûêΓûêΓûêΓûêΓûÆ
         ΓûêΓûêΓûêΓûêΓûêΓûêΓûêΓûêΓûêΓûê
         ΓûÆΓûêΓûêΓûêΓûêΓûêΓûêΓûêΓûêΓûÆ
         ΓûÆΓûÆΓûêΓûêΓûêΓûêΓûêΓûêΓûÆΓûÆ

Picture of the code, if it's not displaying properly.

Picture of the code, if it's not displaying properly.

I am working in C, CodeLite IDE, WIndows 10 with MinGW-32. Thanks in advance.

phuclv
  • 37,963
  • 15
  • 156
  • 475
xaresys
  • 31
  • 1
  • 3
  • 7
  • result depend from code page used by console. usual console use OEM code page, when you ANSI code page. you need use Unicode functions and text for correct print – RbMm Jul 25 '17 at 23:46
  • Note that you should not use ```\``` termination to create long strings — witness the spaces on the second and subsequent lines. Use string concatenation: `"▒▒██████▒▒\n" "▒████████▒\n" "██████████\n" "▒████████▒\n" "▒▒██████▒▒\n"` which deals with the leading blanks too. There's just white space (newlines in your code, blanks in my comment) between the string segments. The backslash-newline mechanism was necessary before C90, but not since then. – Jonathan Leffler Jul 26 '17 at 00:29
  • It probably isn't any consolation to tell you that the code works OK on a Mac running macOS Sierra. – Jonathan Leffler Jul 26 '17 at 00:32

3 Answers3

0
  1. How your text editor codes unprintable characters is unknown but I guess it is UTF-something, and yout table contains something completely different that you think

  2. See how do ascii > 127 print

    for(int i = 32; i < 256;i++)
        printf("%3d / %02x = %c\n\r", i, i, i);`
    

Then define your table using escape sequences - for example

char a[] = "\xf3\xef"

Here you have 4 columns version

for (int i = 32; i < 256; i++)
    printf("%3d / %02x = %c%s", i, i, i, ((i >> 2) << 2 == i) ? "\n\r" : "\t");

and the output should be like this:

 32 / 20 =
 33 / 21 = !     34 / 22 = "     35 / 23 = #     36 / 24 = $
 37 / 25 = %     38 / 26 = &     39 / 27 = '     40 / 28 = (
 41 / 29 = )     42 / 2a = *     43 / 2b = +     44 / 2c = ,
 45 / 2d = -     46 / 2e = .     47 / 2f = /     48 / 30 = 0
 49 / 31 = 1     50 / 32 = 2     51 / 33 = 3     52 / 34 = 4
 53 / 35 = 5     54 / 36 = 6     55 / 37 = 7     56 / 38 = 8
 57 / 39 = 9     58 / 3a = :     59 / 3b = ;     60 / 3c = <
 61 / 3d = =     62 / 3e = >     63 / 3f = ?     64 / 40 = @
 65 / 41 = A     66 / 42 = B     67 / 43 = C     68 / 44 = D
 69 / 45 = E     70 / 46 = F     71 / 47 = G     72 / 48 = H
 73 / 49 = I     74 / 4a = J     75 / 4b = K     76 / 4c = L
 77 / 4d = M     78 / 4e = N     79 / 4f = O     80 / 50 = P
 81 / 51 = Q     82 / 52 = R     83 / 53 = S     84 / 54 = T
 85 / 55 = U     86 / 56 = V     87 / 57 = W     88 / 58 = X
 89 / 59 = Y     90 / 5a = Z     91 / 5b = [     92 / 5c = \
 93 / 5d = ]     94 / 5e = ^     95 / 5f = _     96 / 60 = `
 97 / 61 = a     98 / 62 = b     99 / 63 = c    100 / 64 = d
101 / 65 = e    102 / 66 = f    103 / 67 = g    104 / 68 = h
105 / 69 = i    106 / 6a = j    107 / 6b = k    108 / 6c = l
109 / 6d = m    110 / 6e = n    111 / 6f = o    112 / 70 = p
113 / 71 = q    114 / 72 = r    115 / 73 = s    116 / 74 = t
117 / 75 = u    118 / 76 = v    119 / 77 = w    120 / 78 = x
121 / 79 = y    122 / 7a = z    123 / 7b = {    124 / 7c = |
125 / 7d = }    126 / 7e = ~    127 / 7f =     128 / 80 = Ç
129 / 81 = ü    130 / 82 = é    131 / 83 = â    132 / 84 = ä
133 / 85 = à    134 / 86 = å    135 / 87 = ç    136 / 88 = ê
137 / 89 = ë    138 / 8a = è    139 / 8b = ï    140 / 8c = î
141 / 8d = ì    142 / 8e = Ä    143 / 8f = Å    144 / 90 = É
145 / 91 = æ    146 / 92 = Æ    147 / 93 = ô    148 / 94 = ö
149 / 95 = ò    150 / 96 = û    151 / 97 = ù    152 / 98 = ÿ
153 / 99 = Ö    154 / 9a = Ü    155 / 9b = ø    156 / 9c = £
157 / 9d = Ø    158 / 9e = ×    159 / 9f = ƒ    160 / a0 = á
161 / a1 = í    162 / a2 = ó    163 / a3 = ú    164 / a4 = ñ
165 / a5 = Ñ    166 / a6 = ª    167 / a7 = º    168 / a8 = ¿
169 / a9 = ®    170 / aa = ¬    171 / ab = ½    172 / ac = ¼
173 / ad = ¡    174 / ae = «    175 / af = »    176 / b0 = ░
177 / b1 = ▒    178 / b2 = ▓    179 / b3 = │    180 / b4 = ┤
181 / b5 = Á    182 / b6 = Â    183 / b7 = À    184 / b8 = ©
185 / b9 = ╣    186 / ba = ║    187 / bb = ╗    188 / bc = ╝
189 / bd = ¢    190 / be = ¥    191 / bf = ┐    192 / c0 = └
193 / c1 = ┴    194 / c2 = ┬    195 / c3 = ├    196 / c4 = ─
197 / c5 = ┼    198 / c6 = ã    199 / c7 = Ã    200 / c8 = ╚
201 / c9 = ╔    202 / ca = ╩    203 / cb = ╦    204 / cc = ╠
205 / cd = ═    206 / ce = ╬    207 / cf = ¤    208 / d0 = ð
209 / d1 = Ð    210 / d2 = Ê    211 / d3 = Ë    212 / d4 = È
213 / d5 = ı    214 / d6 = Í    215 / d7 = Î    216 / d8 = Ï
217 / d9 = ┘    218 / da = ┌    219 / db = █    220 / dc = ▄
221 / dd = ¦    222 / de = Ì    223 / df = ▀    224 / e0 = Ó
225 / e1 = ß    226 / e2 = Ô    227 / e3 = Ò    228 / e4 = õ
229 / e5 = Õ    230 / e6 = µ    231 / e7 = þ    232 / e8 = Þ
233 / e9 = Ú    234 / ea = Û    235 / eb = Ù    236 / ec = ý
237 / ed = Ý    238 / ee = ¯    239 / ef = ´    240 / f0 = ­
241 / f1 = ±    242 / f2 = ‗    243 / f3 = ¾    244 / f4 = ¶
245 / f5 = §    246 / f6 = ÷    247 / f7 = ¸    248 / f8 = °
249 / f9 = ¨    250 / fa = ·    251 / fb = ¹    252 / fc = ³
253 / fd = ²    254 / fe = ■    255 / ff =  
phuclv
  • 37,963
  • 15
  • 156
  • 475
0___________
  • 60,014
  • 4
  • 34
  • 74
0

windows console used only Unicode (UTF-16 encoding) for display text in console. if you pass text to console in Unicode, say via WriteConsoleW - it will be displayed as is. if you use ansi version WriteConsoleA - your text initially will be converted to Unicode by call MultiByteToWideChar and as first parameter CodePage will be used value returned from GetConsoleOutputCP. by default console used OEM code page GetOEMCP. however you use ansi encoding (GetACP). you can call SetConsoleOutputCP before print in ansi. and you will be need use specific, hardcoded code page value - exactly the same, which your compiler use when compiled you source string "▒▒██████▒▒". when you run on the same windows, where you compile code (and if compiler not give you warning C4566: character represented by universal-character-name '\uxxxx' cannot be represented in the current code page (N)) - the SetConsoleOutputCP(GetACP()) - will be worked, but the ANSI code pages can be different on different computers - so not work on another.

so good solution here - use Unicode L"▒▒██████▒▒" strings with WriteConsoleW or another W functions. if you got string in runtime as multibyte strings - you need before print it or call SetConsoleOutputCP(cp) where cp - is code page used in your string and then use A function for output. or (better) first yourself convert string to Unicode via MultiByteToWideChar(cp, *) and then use W functions for output

RbMm
  • 31,280
  • 3
  • 35
  • 56
  • I changed `shade_block`'s type to `wchar_t`, added the L before it, and used wprintf, and now it prints nothing. Is this a problem with my compiler? – xaresys Jul 26 '17 at 01:38
  • No. See my answer. Forget unicode & w_char for the moment. – 0___________ Jul 26 '17 at 07:40
  • @xaresys I use `WriteConsoleW` and it really print all this charasters – RbMm Jul 26 '17 at 07:48
  • @PeterJ - why I need Forget unicode ? Unicode is native for windows and with Unicode all perfect working – RbMm Jul 26 '17 at 07:50
  • @RbMm - calm down and give the guy a chance to go through his first "hello world" applications. I know that you know how to do it, but it will make a complete mess in his brain. Give him a chance to understand what the string, pointer etc etc are. I hope that you know what I mean – 0___________ Jul 26 '17 at 07:50
  • @xaresys - `wprintf` not solution here, because look like crt translate Unicode to ansi first (with own default code page convertions) and then use ansi `printf` - problem with crt itself. use windows api `WriteConsoleW` instead. with this all is worked. – RbMm Jul 26 '17 at 08:03
0

Though RbMm and PeterJ's answers worked, I used tips from this StackOVerflow question and eventually figured out how to print the block as-is without any character-escaping or WriteConsoleW:

#include <stdio.h>
#include <wchar.h>              //For wprintf and wide chars
#include <io.h>                 //For _setmode
#define U_16 0x20000            //U-16 text mode, for the text blocks

const wchar_t shade_block[] = {
    L"▒▒██████▒▒\n"
    "▒████████▒\n"
    "██████████\n"
    "▒████████▒\n"
    "▒▒██████▒▒\n"
    };

int main()
{
    _setmode(_fileno(stdout), U_16); //set the output mode to U_16
    wprintf(L"%s\n", shade_block);

    return 0;

}
xaresys
  • 31
  • 1
  • 3
  • 7