Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ultra Quick Way To Concatenate Byte Values

Tags:

c++

string

byte

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

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

I am working in C++, and would settle for a concatenation of the bytes as a 24 bit string using their 8-bit representations.

It needs to be ultra fast because I am using this method on a lot of data, and it seems frustratingly like their isn't a way to just say treat this byte as if it were a string.

Many thanks for your help

To clarify, the reason why I'm particular about using 3 bytes is because the original data pertains to RGB values which are read via pointers and are stored of course as bytes in memory.

I want a way really to treat each color independently so you can think of this as a hashing function if you like. So any fast representation that does it without collisions is desired. This is the only way I can think of to avoid any collisions at all.

like image 403
Darius Avatar asked Jul 26 '11 17:07

Darius


People also ask

What is the result of byte string concatenation?

In Python terminology, the result of this byte string concatenation is the concatenation of the representations ( repr () / __repr__ ()) of these byte strings. In Python 3, the __add__ operator returns what we want:

How to concatenate two or more byte arrays in C#?

Concatenate two or more byte arrays in C# 1. Using Buffer.BlockCopy () method Here’s how we can concatenate two-byte arrays using the Buffer.BlockCopy () method. 2. Using LINQ’s Concat () method Another elegant solution is to use LINQ’s Concat () method for concatenating two or... 3. Using LINQ’s ...

What is the outcome of concatenation in Python?

The outcome is text (a sequence of unicode code points) instead of a byte string (a sequence of bytes). In Python terminology, the result of this byte string concatenation is the concatenation of the representations ( repr () / __repr__ ()) of these byte strings.

How do you concatenate two arrays in LINQ?

Another elegant solution is to use LINQ’s Concat()method for concatenating two or more byte arrays. The following code concatenate elements of the second array into the first array: 1 2 3 publicstaticbyte[]Combine(byte[]first,byte[]second){ returnfirst.


2 Answers

Did you consider instead just packing the color elements into three bytes of an integer?

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

like image 115
Mark B Avatar answered Sep 30 '22 13:09

Mark B


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 make use of manipulators to do this in hex or octal:

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

Update

Since you've clarified what you really want to do, I've updated my answer. You're looking to take RGB values as three bytes, and use them as a key somehow. This would be best done with a long int, not as a string. You can still stringify the int quite easily, for printing to 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 the long int rgb as your key, very efficiently. Whenever you want to print it out, you can still do that:

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

Depending on the endian-ness of your system, you may need to play around with which bytes of the long int you set. My example overwrites bytes 0-2, but you might want to write bytes 1-3. And you might want to write the order as z, y, x instead of x, y, z. That kind of detail is platform dependent. Although if you never want to print the RGB value, but simply want to consider it as a hash, then you don't need to worry about which bytes you write or in what order.

like image 36
Tim Avatar answered Sep 30 '22 14:09

Tim