Распечатайте цветной текст к консоли в C++

Разобрался. Заменен sslStream.AuthenticateAsClient ("gateway.sandbox.push.apple.com"); с sslStream.AuthenticateAsClient ("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false); И зарегистрировал сертификаты на ПК.

Редактировать: вот код для создания полезной нагрузки в соответствии с запросом:

    private static byte[] GeneratePayload(byte [] deviceToken, string message, string sound)
    {
        MemoryStream memoryStream = new MemoryStream();

        // Command
        memoryStream.WriteByte(0);

        byte[] tokenLength = BitConverter.GetBytes((Int16)32);
        Array.Reverse(tokenLength);
        // device token length
        memoryStream.Write(tokenLength, 0, 2);

        // Token
        memoryStream.Write(deviceToken, 0, 32);

        // String length
        string apnMessage = string.Format ( "{{\"aps\":{{\"alert\":{{\"body\":\"{0}\",\"action-loc-key\":null}},\"sound\":\"{1}\"}}}}",
            message,
            sound);

        byte [] apnMessageLength = BitConverter.GetBytes((Int16)apnMessage.Length);
        Array.Reverse ( apnMessageLength );
        // message length
        memoryStream.Write(apnMessageLength, 0, 2);

        // Write the message
        memoryStream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length);

        return memoryStream.ToArray();
    } // End of GeneratePayload
7
задан Brock Woolf 22 May 2009 в 18:36
поделиться

2 ответа

Ознакомьтесь с этим руководством . Я бы сделал собственный манипулятор, чтобы сделать что-нибудь вроде:

std::cout << "standard text" << setcolour(red) << "red text" << std::endl;

Вот небольшое руководство по реализации собственного манипулятора.

Быстрый пример кода:

#include <iostream>
#include <windows.h>
#include <iomanip>

using namespace std;

enum colour { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };

struct setcolour
{
   colour _c;
   HANDLE _console_handle;


       setcolour(colour c, HANDLE console_handle)
           : _c(c), _console_handle(0)
       { 
           _console_handle = console_handle;
       }
};

// We could use a template here, making it more generic. Wide streams won't
// work with this version.
basic_ostream<char> &operator<<(basic_ostream<char> &s, const setcolour &ref)
{
    SetConsoleTextAttribute(ref._console_handle, ref._c);
    return s;
}

int main(int argc, char *argv[])
{
    HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);
    cout << "standard text" << setcolour(RED, chandle) << " red text" << endl;

    cin.get();
}
10
ответ дан 6 December 2019 в 23:13
поделиться

I did a search for "c++ console write colored text" and came up with this page at about 4 or 5. As the site has a copy & paste section I thought I'd post it here (another question on link rot also prompted this):

#include <stdlib.h>
#include <windows.h>
#include <iostream>

using namespace std;

enum Color { DBLUE=1,GREEN,GREY,DRED,DPURP,BROWN,LGREY,DGREY,BLUE,LIMEG,TEAL,
    RED,PURPLE,YELLOW,WHITE,B_B };
/* These are the first 16 colors anyways. You test the other hundreds yourself.
   After 15 they are all combos of different color text/backgrounds. */

bool quit;

void col(unsigned short color)
{
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

istream &operator>> ( istream &in, Color &c )
{
    int tint;
    cin >> tint;
    if (tint==-1) quit=true;
    c=(Color)tint;
}

int main()
{
    do {
        col(7); // Defaults color for each round.
        cout << "Enter a color code, or -1 to quit... ";
        Color y;
        cin >> y; // Notice that >> is defined above for Color types.
        col(y); // Sets output color to y.
        if (!quit) cout << "Color: " << (int)y << endl;
    } while (!quit);
    return 0;
}

For C# there's this page

1
ответ дан 6 December 2019 в 23:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: