int24_t, char24_t with exactly 3 bytes
Aug 28, 2016 at 5:47am UTC
Hello,
sizeof(wchar_t) is 2. It can store any character from 0 - 65535. That is why I need an universal integer which can store a Unicode character at any range. 2^24 = 16777216 should cover them all. char32_t is just too much and it wastes lots of spaces. I have tried, but suprisingly
the size of char24_t is still 4 bytes not 3 bytes.
1 2 3 4 5
#define char24_t int24_t
struct int24_t
{
int32_t val : 24;
};
Could anyone help me solve the problem? Thanks.
Aug 28, 2016 at 6:30am UTC
Create a user defined type
char24_t which holds three bytes, and has implict conversions to/from
char32_t .
Something along these lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
#include <limits>
#include <iterator>
#include <algorithm>
struct char24_t
{
static constexpr std::size_t NBITS = std::numeric_limits<unsigned char >::digits ;
static_assert ( NBITS == 8, "byte must be an octet" ) ;
char24_t() = default ;
char24_t( char32_t value )
{
// assert value within range of char24_t
lsb = value & 0xff ;
value >>= NBITS ;
midb = value & 0xff ;
msb = value >> NBITS ;
}
operator char32_t () const { return ( msb << (NBITS*2) ) + ( midb << NBITS ) + lsb ; }
std::uint8_t msb = 0 ;
std::uint8_t midb = 0 ;
std::uint8_t lsb = 0 ;
};
int main()
{
constexpr std::size_t N = 100 ;
const char32_t a32[N] = { 0, 111, 2222, 33333, 444444, 5555555, 16777215 } ;
char24_t a24[N] ;
std::cout << "sizeof(a32): " << sizeof (a32) << " sizeof(a24): " << sizeof (a24) << '\n' ;
std::copy( std::begin(a32), std::end(a32), std::begin(a24) ) ;
std::cout << std::boolalpha << "equal? " << std::equal( std::begin(a32), std::end(a32), std::begin(a24) ) << '\n' ;
}
http://coliru.stacked-crooked.com/a/184655a3b9cfab7b
Topic archived. No new replies allowed.