Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: How to encode/decode string of decimal digits |
| Date | 2015-10-22 13:45 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87fv13gjgr.fsf@bsb.me.uk> (permalink) |
| References | (4 earlier) <n07ep5$oi1$1@dont-email.me> <n07uqm$g1h$1@dont-email.me> <n08c8u$6ef$1@dont-email.me> <n0ab85$e5r$1@dont-email.me> <n0ai26$56d$1@dont-email.me> |
pozz <pozzugno@gmail.com> writes:
> Il 22/10/2015 11:47, David Brown ha scritto:
>> On 21/10/15 17:53, pozz wrote:
>>> Il 21/10/2015 14:03, Eric Sosman ha scritto:
>> <snip>
>>>> Why "variable-length?" You've told us the digit strings are short,
>>>> and people have proposed at least four ways to encode those shortish
>>>> strings as 64-bit numbers. Just store the encoded number as a uint64_t
>>>> and be done with it.
>>>
>>> The strings are short, but not fixed-length. Here a small list of
>>> examples of *different* strings:
>>> 1234
>>> 01234
>>> 00001234
>>> 123456789012345
>>> 9874321
>>>
>>
>> The simple solution here would be to map decimal digits 0 to 9 to hex
>> digits 0 to 9, and "space" or "no digit" to hex 'a'. This would turn
>> those strings into:
>>
>> 0xaaaaaaaaaaaa1234
>> 0xaaaaaaaaaaa01234
>> 0xaaaaaaaa00001234
>> 0xa123456789012345
>> 0xaaaaaaaaa9874321
>>
>> Clear, simple, fast to implement and easy for debugging (since the hex
>> values are human-readable).
>
> Ok for "easy for debugging" (you can read directly the string), but is
> the encoding process "simpler" than strtoull()/strlen()?
>
> I can think of (I didn't test it):
>
> uint64_t encode(const char *s) {
> uint64_t ret;
> char buf[16 + 1];
> memset(buf, 0xAA, sizeof(buf) - 1);
> sprintf(&buf[sizeof(buf) - strlen(s)], string_to_encode);
> return strtoull(buf, NULL, 16);
> }
>
> IMHO it isn't simpler.
uint64_t encode(const char *str)
{
uint64_t r = 0xaaaaaaaaaaaaaaaa;
while (*str) r = (r << 4) + *str++ - '0';
return r;
}
Hardly very complex, but it does use an explicit loop where strtoull and
strlen hide them (not that that's bad, just that there always going to
be a loop).
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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