[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Conversion to unibyte, magic latin-1?
From: |
Stefan Monnier |
Subject: |
Re: Conversion to unibyte, magic latin-1? |
Date: |
Sat, 04 May 2019 18:53:46 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
> (secure-hash 'sha256 (concat [#x52 #xbc #xdd #x9e]))
> "cfdc1612961dc873079178b92bf0aafaa6bd33731cbaa60841eef163f85074e8"
(concat [#x52 #xbc #xdd #x9e]) takes the character codes you specified
and interprets them as unicode chars rather than as bytes. So that's
the origin of your problem.
You want to use (unibyte-string #x52 #xbc #xdd #x9e) instead.
> `string-make-unibyte':
Don't. This will just paper over problems.
If you have a multibyte string (i.e. a sequence of characters) and you
need to convert it to a unibyte string (i.e. a sequence of bytes), then
you want to use `encode-coding-string` where the CODING-SYSTEM indicates
how to convert each char to a corresponding sequence of bytes.
> with a few obvious encodings, but no luck:
`raw-text` is not an obvious encoding.
Encoding with `raw-text` only works in a meaningful way on sequences of
chars where the chars are themselves bytes (these are the char codes
0-127 and #x3fff80-#x3fffff).
> This works, but I'm confused... why does latin-1 work but raw-text or
> binary doesn't?
latin-1 will work in some cases, but only by accident.
Better go back to the origin of the problem (why do you end up with
a multibyte string when what you wanted was a unibyte string).
Stefan