Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Storing 4 characters using 3 bytes (http://www.velocityreviews.com/forums/t315788-storing-4-characters-using-3-bytes.html)

 becte 10-15-2003 07:40 PM

Storing 4 characters using 3 bytes

I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
like this
11111122|22223333|33444444
Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
and the output is int o1,o2,o3, range 0 .. 2^8-1
How to do this in a clever way?

(The 6 bits integers represent characters in range A-Z and 0-9)

 Tristan Miller 10-15-2003 07:43 PM

Re: Storing 4 characters using 3 bytes

Greetings.

In article <b406cd26.0310151140.58d15360@posting.google.com >, becte wrote:
> I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
> like this
> 11111122|22223333|33444444
> Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
> and the output is int o1,o2,o3, range 0 .. 2^8-1
> How to do this in a clever way?

Sounds suspiciously like a homework problem, so you're unlikely to get
complete solutions here. Have you read up on C's bit-shifting and bitwise
comparison operators? If not, do so and ask again if you still need help.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you

 Mark A. Odell 10-15-2003 07:53 PM

Re: Storing 4 characters using 3 bytes

henrik_gustafsson@hotmail.com (becte) wrote in

> I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
> like this
> 11111122|22223333|33444444
> Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
> and the output is int o1,o2,o3, range 0 .. 2^8-1
> How to do this in a clever way?
>
> (The 6 bits integers represent characters in range A-Z and 0-9)

How about the untested:

#define PACKED_INT_RANGE 0x3F /* 6-bits */

/* pPacked had better be 3 bytes or more
*/
int packInts(int first, int second, int third, int fourth, char *pPacked)
{
int failures = !0;

if ((first & PACKED_INT_RANGE) == first
&& (second & PACKED_INT_RANGE) == second
&& (third & PACKED_INT_RANGE) == third
&& (fourth & PACKED_INT_RANGE) == fourth)
{
pPacked[0] = (first & PACKED_INT_RANGE) << 2;
pPacked[0] |= (second & PACKED_INT_RANGE) >> 4;
pPacked[1] = (second & PACKED_INT_RANGE) << 4;
pPacked[1] |= (third & PACKED_INT_RANGE) >> 2;
pPacked[2] = (third & PACKED_INT_RANGE) << 6;
pPacked[2] |= (fourth & PACKED_INT_RANGE) >> 0;

failures = 0;
}

return failures;
}

--
- Mark ->
--

 Ivan Vecerina 10-15-2003 07:54 PM

Re: Storing 4 characters using 3 bytes

"becte" <henrik_gustafsson@hotmail.com> wrote in message
> I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
> like this
> 11111122|22223333|33444444
> Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
> and the output is int o1,o2,o3, range 0 .. 2^8-1
> How to do this in a clever way?
>
> (The 6 bits integers represent characters in range A-Z and 0-9)

You could store the 4 6-bit values contiguously into a 32-bit
unsigned long, then split these 24-bit value back into 3 bytes.

I think you should give it a try, and post some code if you
need additional help. All you need is the bit-shift and
bit-or/bit-and operators ( << >> | & ).

Cheers,
Ivan
--
http://ivan.vecerina.com

 Eric Sosman 10-15-2003 08:06 PM

Re: Storing 4 characters using 3 bytes

becte wrote:
>
> I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
> like this
> 11111122|22223333|33444444
> Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
> and the output is int o1,o2,o3, range 0 .. 2^8-1
> How to do this in a clever way?

"Straightforward" suffices:

o1 = (c1 << 2) | (c2 >> 4);
o2 = ((c2 & 0xF) << 4) | (c3 >> 2);
03 = ((c3 & 0x3) << 2) | c4;

For "clever" you might try something like this (requires
an `int' of 25 or more bits):

o1 = (c1 << 18) | (c2 << 12) | (c3 << 6) | c4;
o3 = o1 & 0xFF;
o1 >>= 8;
o2 = o1 & 0xFF;
o1 >>= 8;

--
Eric.Sosman@sun.com

 Christopher Benson-Manica 10-15-2003 08:06 PM

Re: Storing 4 characters using 3 bytes

Mark A. Odell <nospam@embeddedfw.com> spoke thus:

> int packInts(int first, int second, int third, int fourth, char *pPacked)
> int failures = !0;

Just FMI, this assumes that !0 isn't a trap representation and that CHAR_BIT
is 8, right? Does it do anything else non-portable?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

 Joona I Palaste 10-15-2003 08:08 PM

Re: Storing 4 characters using 3 bytes

Christopher Benson-Manica <ataru@nospam.cyberspace.org> scribbled the following:
> Mark A. Odell <nospam@embeddedfw.com> spoke thus:
>> int packInts(int first, int second, int third, int fourth, char *pPacked)
>> int failures = !0;

> Just FMI, this assumes that !0 isn't a trap representation and that CHAR_BIT
> is 8, right? Does it do anything else non-portable?

Umm... what do you think !0 equals?

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller

 Christopher Benson-Manica 10-15-2003 08:40 PM

Re: Storing 4 characters using 3 bytes

Joona I Palaste <palaste@cc.helsinki.fi> spoke thus:

> Umm... what do you think !0 equals?

Um... nevermind? :(

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

 Nudge 10-15-2003 08:45 PM

Re: Storing 4 characters using 3 bytes

becte wrote:
> I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
> like this
> 11111122|22223333|33444444
> Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
> and the output is int o1,o2,o3, range 0 .. 2^8-1
> How to do this in a clever way?

How about bit fields?

 cody 10-15-2003 09:20 PM

Re: Storing 4 characters using 3 bytes

typedef union
{
char chars[3];
struct
{
int _1 : 6;
int _2 : 6;
int _3 : 6;
int _4 : 6;
} BITS

} PACKED

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

All times are GMT. The time now is 10:07 AM.