0

I am trying to convert a string of 6 hex digits into a 0xffffff type of format. What is the data type for hex in C++? I currently have a string for, as an example, ffffff, but I need to get it into the 0xffffff for a function call. Basically, my code generate random digits of hex as an int, then converts those ints into the hex code counterpart. I then append those string together to create a hex string, but now I'm stuck on how to convert that into a 0xffffff format for a light_matrix.wr_pix() function call. Here is my code:

#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
hippoman
  • 187
  • 2
  • 10
  • There is no data type for hex in C++. Hex is not a data type; it describes a **text representation** of a value. – Pete Becker Mar 30 '19 at 19:02
  • Note that the `getHex` function can be much simpler if it simply returns a `char` instead of a `std::string`, and calculates the value of that char by array lookup: `static const char hexes[] = "0123456789abcdef"; return hexes[digit];`. But that doesn't really matter, since the code doesn't call that function. – Pete Becker Mar 30 '19 at 19:04
  • @hippoman - I looked at the other questions you asked about that "wr_pix" function. I think you need to look at how to translate numbers into different systems. Check out a video on the topic "hexadecimal to decimal" so you can see those how those numbers are the same. – Johannes Mar 30 '19 at 19:13

0 Answers0