Ultra fast way to combine byte values

Given 3 different bytes, such as say x = 64, y = 90, z = 240 I am looking to combine them into a string like 6490240. It would be great if this worked, but it doesn't:

string xx = (string)x + (string)y + (string)z; 

I work in C ++ and agree to concatenate bytes as a 24-bit string using their 8-bit representations.

It should be very fast, because I use this method for a lot of data, and it seems disappointing, since this is not a way to just say treat this byte, as if it were a string.

Many thanks for your help

To clarify, the reason I mean using 3 bytes is because the source data refers to RGB values ​​that are read using pointers and stored, of course, as bytes in memory.

I want to really process each color independently so that you can think of it as a hash function if you want. Therefore, any quick view that does this without collisions is desirable. This is the only way I can think of all the collisions.

+4
source share
5 answers

Instead, you just pack color elements into three bytes of an integer?

uint32_t full_color = (x << 16) | (y << 8) | z;

+8
source

The easiest way to turn numbers into a string is to use ostringstream

 #include <sstream> #include <string> std::ostringstream os; os << x << y << z; std::string str = os.str(); // 6490240 

You can even use the manipulators for this in hexadecimal or octal:

 os << std::hex << x << y << z; 

Update

Since you clarified what you really want to do, I updated my answer. You want to take the RGB values ​​as three bytes and somehow use them as a key. This would be best done using long int, rather than as strings. You can still simplify int ints to print on the screen.

 unsigned long rgb = 0; byte* b = reinterpret_cast<byte*>(&rgb); b[0] = x; b[1] = y; b[2] = z; // rgb is now the bytes { 0, x, y, z } 

Then you can use long int rgb as your key, very efficiently. Whenever you want to print it, you can still do it:

 std::cout << std::hex << rgb; 

Depending on the finiteness of your system, you may need to reproduce which bytes of the long int you specify. My example overwrites bytes 0-2, but you can write bytes 1-3. And you can write the order as z, y, x instead of x, y, z. Such details are platform dependent. Although if you never want to print the RGB value, but just want to consider it a hash, you do not need to worry about what bytes you write or in which order.

+10
source

try sprintf(xx,"%d%d%d",x,y,z);

+2
source

Use a 3-character character array as a 24-bit representation and assign each char the value of one of your input values.

+2
source

Converting 3 bytes to bits and storing the result in an array can be done easily, as shown below:

 void bytes2bits(unsigned char x, unsigned char y, unsigned char z, char * res) { res += 24; *res-- = 0; unsigned xyz = (x<<16)+(y<<8)+z; for (size_t l = 0 ; l < 24 ; l++){ *res-- = '0'+(xyz & 1); xyz >>= 1; } 

}

However, if you are looking for a way to store three byte values ​​in an ambiguous and compact way, you should probably settle for hexadecimal. (each group of four bits of the binary representation corresponds to a digit from 0 to 9 or a letter between A and F). It is very simple and easy to encode and decode, and also suitable for human reading.

If you never need to print the result, simply combining the values ​​as a whole and use it as a key, since the proposed Mark is certainly the fastest and easiest solution. Assuming your own integer is 32 bits or more on the target system, simply do:

 unsigned int key = (x<< 16)|(y<<8)|z; 

You can easily return the original values ​​from the key, if necessary:

 unsigned char x = (key >> 16) & 0xFF; unsigned char y = (key >> 8) & 0xFF; unsigned char z = key & 0xFF; 
+1
source

All Articles