unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Converting UTF-8 strings to hex in Emacs
@ 2010-03-03 14:25 白い熊
  2010-03-03 15:03 ` Andreas Schwab
  2010-03-03 15:08 ` Harald Hanche-Olsen
  0 siblings, 2 replies; 3+ messages in thread
From: 白い熊 @ 2010-03-03 14:25 UTC (permalink / raw)
  To: emacs-devel

Hi:

I'm looking for the Emacs way to convert UTF-8 strings to Hex, so they
can be sent as hex codes to modem AT commands.

For interactive mode hexl-mode lets you view strings and see their
representation, however I need a way to do this in elisp, i.e. pass the
UTF-8 string and receive back its hex representation.

There's uni2ascii which does this, and I can call it via a call-process,
however would like to do it in elisp.

Anyone know how?
-- 
C уважением / 宜しく御願い致します / Best regards / S pozdravem / Z poważaniem / Mit freundlichen Grüßen

白い熊





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Converting UTF-8 strings to hex in Emacs
  2010-03-03 14:25 Converting UTF-8 strings to hex in Emacs 白い熊
@ 2010-03-03 15:03 ` Andreas Schwab
  2010-03-03 15:08 ` Harald Hanche-Olsen
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Schwab @ 2010-03-03 15:03 UTC (permalink / raw)
  To: 白い熊; +Cc: emacs-devel

白い熊 <emacs-devel_gnu.org@sumou.com> writes:

> For interactive mode hexl-mode lets you view strings and see their
> representation, however I need a way to do this in elisp, i.e. pass the
> UTF-8 string and receive back its hex representation.

(defun uni2ascii (s)
  (mapconcat (lambda (c) (format "%02X" c))
             (encode-coding-string s 'utf-8) ""))

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Converting UTF-8 strings to hex in Emacs
  2010-03-03 14:25 Converting UTF-8 strings to hex in Emacs 白い熊
  2010-03-03 15:03 ` Andreas Schwab
@ 2010-03-03 15:08 ` Harald Hanche-Olsen
  1 sibling, 0 replies; 3+ messages in thread
From: Harald Hanche-Olsen @ 2010-03-03 15:08 UTC (permalink / raw)
  To: emacs-devel_gnu.org; +Cc: emacs-devel

+ 白い熊 <emacs-devel_gnu.org@sumou.com>:

> I'm looking for the Emacs way to convert UTF-8 strings to Hex, so they
> can be sent as hex codes to modem AT commands.

This is the emacs developer list; I think such questions are not too
welcome here. Still, I'll risk the ire of the regulars by responding.

> There's uni2ascii which does this, and I can call it via a call-process,
> however would like to do it in elisp.

Something along the lines of

(defun string-to-numeric (string)
  (let (nums)
    (dotimes (n (length string) (apply #'concat (nreverse nums)))
      (setq nums (cons (format "%d;" (aref string n))
		       nums)))))

might work; you'll have to adjust the format string to give the
desired format (I haven't the foggiest notion what uni2ascii
produces). Warning: This only works on modern emacsen for which
unicode is the internal encoding. for compatibility with older ones,
an extra function call will be needed (but I don't know which).

- Harald




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-03-03 15:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-03 14:25 Converting UTF-8 strings to hex in Emacs 白い熊
2010-03-03 15:03 ` Andreas Schwab
2010-03-03 15:08 ` Harald Hanche-Olsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).