Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #73288

Re: How to encode/decode string of decimal digits

From Bartc <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: How to encode/decode string of decimal digits
Date 2015-10-19 14:07 +0100
Organization A noiseless patient Spider
Message-ID <n02pr6$sqa$1@dont-email.me> (permalink)
References <n02ni7$k3q$1@dont-email.me>

Show all headers | View raw


On 19/10/2015 13:28, pozz wrote:
> I have to manage string of decimal digits 0-9. They aren't integer
> numbers, because the string "01234" is completely different than the
> number 1234.
>
> I have a very limited embedded plaftorm, so I'm searching for a simple
> compression/decompression method of a single string.
>
> I thought to use a uint64_t to store a 16-digits string. One byte (the
> LSB) of uint64_t is used to save the number of leading zeros.
>
> uint64_t
> compress(const char *s)
> {
>    uint64_t ret;
>    unsigned char nz;
>
>    ret = strtoull(s, NULL, 10);
>    for (nz = 0; *s++ == '0'; nz++)
>      ;
>    ret = (ret << 8) + nz;
>    return ret;
> }
>
> void
> decompress(char *dst, uint64_t n)
> {
>    unsigned char nz;
>
>    nz = n & 0xFF;
>    n >>= 8;
>    memset(dst, '0', nz);
>    if (n > 0) {
>      sprintf(&dst[nz], "%lld", n);
>    } else {
>      dst[nz] = '\0';
>    }
> }
>
> In this way I can save one 16-digits string in 8 bytes.
>
> Do you think of a better method?

How many leading zeros are there likely to be? And how important that 
they be recovered?

Also, is performance important? Because above you are converting strings 
into binary, and converting from binary into strings, all of which can 
require quite a bit of arithmetic to do.

Assuming you can have up to 255 leading zeros, and up to 16 significant 
digits, your basic data is 17 bytes (16 chars + 1 byte count, no 
terminator). Your method above reduces that to 8 bytes. Not a bad 
reduction, but a lot of work, and we don't know whether space is more 
important or speed.

You can reduce that to 9 bytes by using a 4-bit representation for each 
digit (BCD, 2 digits per byte), an easier conversion. Or 8 bytes if your 
forget the leading zero count.

There are far more elaborate methods to reduce below 8 bytes per string, 
but they can depend on how random the strings are.

-- 
Bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-19 14:28 +0200
  Re: How to encode/decode string of decimal digits Bartc <bc@freeuk.com> - 2015-10-19 14:07 +0100
    Re: How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-19 15:58 +0200
      Re: How to encode/decode string of decimal digits Bartc <bc@freeuk.com> - 2015-10-19 15:22 +0100
      Re: How to encode/decode string of decimal digits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-19 15:58 +0100
        Re: How to encode/decode string of decimal digits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-19 22:28 +0100
  Re: How to encode/decode string of decimal digits Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-19 13:30 -0400
    Re: How to encode/decode string of decimal digits Robert Wessel <robertwessel2@yahoo.com> - 2015-10-19 16:18 -0500
      Re: How to encode/decode string of decimal digits Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-19 21:38 -0400
    Re: How to encode/decode string of decimal digits Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-20 08:00 +0000
  Re: How to encode/decode string of decimal digits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-19 15:50 +0100
  Re: How to encode/decode string of decimal digits Keith Thompson <kst-u@mib.org> - 2015-10-19 09:01 -0700
  Re: How to encode/decode string of decimal digits Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-19 12:11 -0700
    Re: How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-20 13:51 +0200
      Re: How to encode/decode string of decimal digits Bartc <bc@freeuk.com> - 2015-10-20 13:38 +0100
        Re: How to encode/decode string of decimal digits glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-20 16:05 +0000
        Re: How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-21 09:29 +0200
          Re: How to encode/decode string of decimal digits Bartc <bc@freeuk.com> - 2015-10-21 11:27 +0100
            Re: How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-21 17:55 +0200
          Re: How to encode/decode string of decimal digits Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-21 08:03 -0400
            Re: How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-21 17:53 +0200
              Re: How to encode/decode string of decimal digits supercat@casperkitty.com - 2015-10-21 09:37 -0700
              Re: How to encode/decode string of decimal digits David Brown <david.brown@hesbynett.no> - 2015-10-22 11:47 +0200
                Re: How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-22 13:44 +0200
                Re: How to encode/decode string of decimal digits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-22 13:45 +0100
                Re: How to encode/decode string of decimal digits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-22 15:51 +0100
                Re: How to encode/decode string of decimal digits supercat@casperkitty.com - 2015-10-22 09:31 -0700
                Re: How to encode/decode string of decimal digits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-22 20:48 +0100
                Re: How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-23 10:14 +0200
                Re: How to encode/decode string of decimal digits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-23 11:14 +0100
                Re: How to encode/decode string of decimal digits Bartc <bc@freeuk.com> - 2015-10-23 11:47 +0100
                Re: How to encode/decode string of decimal digits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-23 13:22 +0100
                Re: How to encode/decode string of decimal digits David Brown <david.brown@hesbynett.no> - 2015-10-22 15:07 +0200
                Re: How to encode/decode string of decimal digits glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-22 22:05 +0000
                Re: How to encode/decode string of decimal digits pozz <pozzugno@gmail.com> - 2015-10-23 10:15 +0200
  Re: How to encode/decode string of decimal digits glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-19 22:02 +0000
  Re: How to encode/decode string of decimal digits supercat@casperkitty.com - 2015-10-20 10:09 -0700

csiph-web