如何将一串十六进制代码转换为十六进制代码?

时间:2019-03-30 19:00:01

标签: c++

我正在尝试将6个十六进制数字的字符串转换为0xffffff类型的格式。 C ++中十六进制的数据类型是什么?例如,我目前有一个用于ffffff的字符串,但是我需要将其放入函数调用的0xffffff中。基本上,我的代码会生成一个十六进制的随机数字作为一个整数,然后将这些整数转换为十六进制代码。然后,我将这些字符串附加在一起以创建一个十六进制字符串,但是现在我仍然停留在如何将其转换为light_matrix.wr_pix()函数调用的0xffffff格式的问题上。这是我的代码:

#include "de10_baseline.h"
#include "ws2812_core.h"
#include "stdlib.h"
#include "time.h"
#include <string>

std::string getHex(int digit){

    std::string hex_digit;
    switch(digit){
        case 0:
                    hex_digit = "0";
        case 1:
                    hex_digit = "1";
        case 2:
                    hex_digit = "2";
        case 3:
                    hex_digit = "3";
        case 4:
                    hex_digit = "4";
        case 5:
                    hex_digit = "5";
        case 6:
                    hex_digit = "6";
        case 7:
                    hex_digit = "7";
        case 8:
                    hex_digit = "8";
        case 9:
                    hex_digit = "9";
        case 10:
                    hex_digit = "a";
        case 11:
                    hex_digit = "b";
        case 12:
                    hex_digit = "c";
        case 13:
                    hex_digit = "d";
        case 14:
                    hex_digit = "e";
        case 15:
                    hex_digit = "f";
        default:
                hex_digit="g";
    }
    return hex_digit;

}
int main() {


    //get i and j of button press
        // i-1, j-1
        // i-1 , j
        // i-1 , j +1
        // i , j-1
        // i , j+1
        // i+1, j-1
        // i+1, j
        // i+1, j +1
    //assign i to x, j to y

    int i=1;
    int j=1;
    int c;
    int k;

            light_matrix.wr_pix(0,0,0x000000); //green
            light_matrix.wr_pix(0,1,0x000000); //green
            light_matrix.wr_pix(0,2,0x000000); //green
            light_matrix.wr_pix(1,0,0x000000); //blue
            light_matrix.wr_pix(1,1,0x000000); //blue
            light_matrix.wr_pix(1,2,0x000000); //blue
            light_matrix.wr_pix(2,0,0x000000); //green
            light_matrix.wr_pix(2,1,0x000000); //green
            light_matrix.wr_pix(2,2,0x000000); //green


    /* initialize random seed: */
        srand(time(NULL));

    while(1){

        int hex1 = rand() % 15;
        int hex2 = rand() % 15;
        int hex3 = rand() % 15;
        int hex4 = rand() % 15;
        int hex5 = rand() % 15;
        int hex6 = rand() % 15;

        std::string hex1_s = std::to_string(hex1);
        std::string hex2_s = std::to_string(hex2);
        std::string hex3_s = std::to_string(hex3);
        std::string hex4_s = std::to_string(hex4);
        std::string hex5_s = std::to_string(hex5);
        std::string hex6_s = std::to_string(hex6);

        std::string hex_string = hex1_s + hex2_s + hex3_s + hex4_s + hex5_s + hex6_s;


        if(btn.read(0)){
            for(c = 0; c < 8; c++){
                for(k = 0; k < 12; k++){
                    if( (c==i-1 || c==i || c == i+1 ) && (k==j-1|| k==j || k == j+1) && (c >= 0) && (k >=0) && (!(c==i && k==j))){
                            light_matrix.wr_pix(c,k,0x0000ff);
                            sleep_ms(100);
                            light_matrix.wr_pix(c,k,0x000000);
                    }

                }
            }
        }
    }

}//main

0 个答案:

没有答案