Home > Blockchain >  Visual Studio cmd doesn't display Unicode characters
Visual Studio cmd doesn't display Unicode characters

Time:08-03

I am trying to make an ASCII converter in C that outputs an image using ▀ ▄ ░ █ Unicode characters.

It works, except for the output part. Instead of the actual characters, it just displays ?.

When I add system("chcp 65001"); or system("chcp 65001"); (to use UTF-8/UTF-16) at the beginning of the file, it doesn't do anything, and still displays ?

image

When I try add /source-charset:utf-8 /execution-charset:utf-8 to the debug properties, it writes some weird LoLs:

image

Here is the whole code:

#include <iostream>
#define STBI_ONLY_BMP
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

int main(int argc, char* argv[]) {
    system("chcp 65001");
    int width, height, channels;
    unsigned char* img = stbi_load("C:/Users/user/source/repos/OCBitmapConvert/grayscale.bmp", &width, &height, &channels, 1);
    if (img == NULL) {
        printf("Error in loading the image\n");
        exit(1);
    }
    char imgarray[80][50];
    printf("Loaded image with a width of %dpx, a height of %dpx and %d channels\n", width, height, channels);
    //Convert the image from 1 dimensional array to 2 dimensional array
    for(int y = 0; y < 50; y  ){
        for (int x = 0; x < 79; x  ){
            int pixell = (y*80) x;
            if(img[pixell] == 0){
                imgarray[x][y] = 0;
            }
            else imgarray[x][y] = 1;
        };
    };
    //Print the 2dim array (just for debugging that the 2d array conversion works correctly)
    for(int y = 0; y < 50; y  ){
        for(int x = 0; x < 79; x  ){
            printf("%d",imgarray[x][y]);
        }
        printf("\n");
    }
    //Convert the image to ascii characters (using ▀ ▄ ░ █). 2 pixels above each other is 1 unicode character
    printf("\n\n\n");
    for(int y = 0; y < 48; y= y 2){
        for(int x = 0; x < 79; x  ){
            if(imgarray[x][y] == 0){
                if(imgarray[x][y   1] == 0){
                    printf("░");
                }
                else printf("▄");
            }
            else{
                if(imgarray[x][y   1] == 0){
                    printf("▀");
                }
                else printf("█");
            }
        }
        printf("\n");
    }
}

CodePudding user response:

After testing, the following code can output ▀ ▄ ░ █ , you could refer to my code to modify your code.

#include <stdio.h>
#include <io.h>
#include <fcntl.h>

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    wprintf(L"░▒▓▄▀█▀ ▄ ░ █\n");
    return 0; // CYRILLIC CAPITAL LETTER YAE U 0518
}

Result:

enter image description here

Update:

I modified the code with reference to your sample, the code can still run, I suggest you refer to this code and modify it.

#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <iostream>
#define STBI_ONLY_BMP
#define STB_IMAGE_IMPLEMENTATION
int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    char imgarray[80][50];
    for (int y = 0; y < 50; y  ) {
        for (int x = 0; x < 79; x  ) {
            
            
             imgarray[x][y] = 1;
        }
    }
    for (int y = 0; y < 48; y = y   2) {
        for (int x = 0; x < 79; x  ) {
            if (imgarray[x][y] == 1)
                wprintf(L"░▒▓▄▀█▀ ▄ ░ █\n");
        }
    }
    return 0; 
}

Result:

enter image description here

In this case you need to find the place that caused the exception. The debugger cannot predict the future, but you can tell it to break on all exceptions, (ctrl atl e and tick all the boxes under "Thrown"). When the assert happens walk down the call stack to your code it will tell you which line is causing the problem.

  • Related