From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.help Subject: Re: how to calculate the size of string in bytes? Date: Tue, 18 Aug 2015 17:34:58 +0300 Message-ID: <83oai4ll65.fsf@gnu.org> References: NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1439908559 7931 80.91.229.3 (18 Aug 2015 14:35:59 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 18 Aug 2015 14:35:59 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Aug 18 16:35:44 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZRhzL-0005PF-7c for geh-help-gnu-emacs@m.gmane.org; Tue, 18 Aug 2015 16:35:43 +0200 Original-Received: from localhost ([::1]:57571 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZRhzK-0000zQ-GE for geh-help-gnu-emacs@m.gmane.org; Tue, 18 Aug 2015 10:35:42 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42059) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZRhyr-0000rC-UU for help-gnu-emacs@gnu.org; Tue, 18 Aug 2015 10:35:14 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZRhyo-0008EL-NG for help-gnu-emacs@gnu.org; Tue, 18 Aug 2015 10:35:13 -0400 Original-Received: from mtaout20.012.net.il ([80.179.55.166]:54864) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZRhyo-0008Dl-Fk for help-gnu-emacs@gnu.org; Tue, 18 Aug 2015 10:35:10 -0400 Original-Received: from conversion-daemon.a-mtaout20.012.net.il by a-mtaout20.012.net.il (HyperSendmail v2007.08) id <0NTA00A0086BIZ00@a-mtaout20.012.net.il> for help-gnu-emacs@gnu.org; Tue, 18 Aug 2015 17:35:08 +0300 (IDT) Original-Received: from HOME-C4E4A596F7 ([87.69.4.28]) by a-mtaout20.012.net.il (HyperSendmail v2007.08) with ESMTPA id <0NTA00AOL8IK7RA0@a-mtaout20.012.net.il> for help-gnu-emacs@gnu.org; Tue, 18 Aug 2015 17:35:08 +0300 (IDT) In-reply-to: X-012-Sender: halo1@inter.net.il X-detected-operating-system: by eggs.gnu.org: Solaris 10 X-Received-From: 80.179.55.166 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106674 Archived-At: > Date: Tue, 18 Aug 2015 02:11:54 -0700 (PDT) > From: Sam Halliday > > Hi all, > > We've had to change the ENSIME protocol to be more friendly to other editors and this has meant changing how we frame TCP messages. > > We used to have a 6 character hex number at the start of each message that counted the number of multibyte characters, but we'd like to change it to be the number of bytes in the message. > > We're sending the string to `process-send-string' and `read'ing from the associated network buffer. But when calculating the outgoing length of the string that we want to send, we use `length' --- but we need this to be `length-in-bytes' not the number of multibyte chars. Is there a built in function to do this or am I going to have to iterate the string and count the byte size of each character? Emacs 25 has bufferpos-to-filepos, which I think does what you want. > A quick test shows that > > (length (encode-coding-string "EURO" 'raw-text)) > > seems to give the correct result (1 for ASCII, 2 for Pound Sterling, 3 for Euro), but I am not 100% sure if this is correct. It will fail if the string includes some exotic characters or raw bytes.