comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Convert wide_string to string (as the same byte array)
Date: Fri, 24 Feb 2012 14:25:43 -0800 (PST)
Date: 2012-02-24T14:25:43-08:00	[thread overview]
Message-ID: <9b005a0a-989a-4c00-8d8e-6f93fa1cf9e3@kh11g2000pbb.googlegroups.com> (raw)
In-Reply-To: f88cc8ca-183a-40c7-a01c-2adc1137d845@b18g2000vbz.googlegroups.com

On Feb 24, 2:01 pm, Erich <j...@peppermind.com> wrote:
> A newbie question: I need to convert a wide_string to a (platform/
> endian independent) string that represents all the bytes of the
> wide_string. How do you do that?

Each Wide_Character of the Wide_String will have to map to two
Characters of the String.  Also, I assume you want things to work so
that when your older package does a lexicographic comparison on
Strings, it will return the correct result based on how the original
Wide_Strings compared.

You'll probably want something like this, for each Wide_Character,
where S is the String and WS is the Wide_String:

   S (J)     := Character'Val (Wide_Character'Pos (WS (I)) / 256);
   S (J + 1) := Character'Val (Wide_Character'Pos (WS (I)) mod 256);

I'll let you fill in the rest of the details.

If you knew you were working with a big-endian machine, I think you
could just do an Unchecked_Conversion on the whole string, but you did
say endian-independent.

                       -- Adam


> Just to make this clear, I'm not looking for a lossy conversion based
> on mapping wide_characters to characters, but for a lossless
> conversion.
>
> The reason is that I have an older package that takes a key as string
> and need to use it with wide_strings as keys.




  reply	other threads:[~2012-02-24 22:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-24 22:01 Convert wide_string to string (as the same byte array) Erich
2012-02-24 22:25 ` Adam Beneschan [this message]
2012-02-24 22:58   ` Erich
2012-03-06  1:58 ` Randy Brukardt
2012-03-06 15:54   ` Adam Beneschan
2012-03-07  1:04     ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox