* Convert wide_string to string (as the same byte array)
@ 2012-02-24 22:01 Erich
2012-02-24 22:25 ` Adam Beneschan
2012-03-06 1:58 ` Randy Brukardt
0 siblings, 2 replies; 6+ messages in thread
From: Erich @ 2012-02-24 22:01 UTC (permalink / raw)
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?
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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Convert wide_string to string (as the same byte array)
2012-02-24 22:01 Convert wide_string to string (as the same byte array) Erich
@ 2012-02-24 22:25 ` Adam Beneschan
2012-02-24 22:58 ` Erich
2012-03-06 1:58 ` Randy Brukardt
1 sibling, 1 reply; 6+ messages in thread
From: Adam Beneschan @ 2012-02-24 22:25 UTC (permalink / raw)
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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Convert wide_string to string (as the same byte array)
2012-02-24 22:25 ` Adam Beneschan
@ 2012-02-24 22:58 ` Erich
0 siblings, 0 replies; 6+ messages in thread
From: Erich @ 2012-02-24 22:58 UTC (permalink / raw)
> You'll probably want something like this, for each Wide_Character,
> where S is the String and WS is the Wide_String:
<snip>
Thanks a lot! Seems to work fine.
function To_String (WS : Wide_String) return String is
S : String (1 .. WS'Length*2);
J : Positive;
begin
J := 1;
for I in WS'Range loop
S (J) := Character'Val (Wide_Character'Pos (WS (I)) /
256);
S (J + 1) := Character'Val (Wide_Character'Pos (WS (I)) mod
256);
J := J + 2;
end loop;
return S;
end To_String;
Best,
Erich
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Convert wide_string to string (as the same byte array)
2012-02-24 22:01 Convert wide_string to string (as the same byte array) Erich
2012-02-24 22:25 ` Adam Beneschan
@ 2012-03-06 1:58 ` Randy Brukardt
2012-03-06 15:54 ` Adam Beneschan
1 sibling, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2012-03-06 1:58 UTC (permalink / raw)
"Erich" <john@peppermind.com> wrote in message
news:f88cc8ca-183a-40c7-a01c-2adc1137d845@b18g2000vbz.googlegroups.com...
>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?
An alternative to Adam's solution would be to use the Ada2012 encoding
functions (A.4.11), specifically Ada.Strings.UTF_Encoding.Wide_Strings, and
use a UTF-8 encoding. That would be shorter, but not fixed length, so
whether that would work for you depends on the API you are feeding these
into.
Randy.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Convert wide_string to string (as the same byte array)
2012-03-06 1:58 ` Randy Brukardt
@ 2012-03-06 15:54 ` Adam Beneschan
2012-03-07 1:04 ` Randy Brukardt
0 siblings, 1 reply; 6+ messages in thread
From: Adam Beneschan @ 2012-03-06 15:54 UTC (permalink / raw)
On Monday, March 5, 2012 5:58:48 PM UTC-8, Randy Brukardt wrote:
>
> An alternative to Adam's solution would be to use the Ada2012 encoding
> functions (A.4.11), specifically Ada.Strings.UTF_Encoding.Wide_Strings, and
> use a UTF-8 encoding. That would be shorter, but not fixed length, so
> whether that would work for you depends on the API you are feeding these
> into.
This may seem like a dumb question, but does that preserve order?
-- Adam
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Convert wide_string to string (as the same byte array)
2012-03-06 15:54 ` Adam Beneschan
@ 2012-03-07 1:04 ` Randy Brukardt
0 siblings, 0 replies; 6+ messages in thread
From: Randy Brukardt @ 2012-03-07 1:04 UTC (permalink / raw)
"Adam Beneschan" <adam@irvine.com> wrote in message
news:5368448.8.1331049289886.JavaMail.geo-discussion-forums@pbbpr1...
> On Monday, March 5, 2012 5:58:48 PM UTC-8, Randy Brukardt wrote:
>>
>> An alternative to Adam's solution would be to use the Ada2012 encoding
>> functions (A.4.11), specifically Ada.Strings.UTF_Encoding.Wide_Strings,
>> and
>> use a UTF-8 encoding. That would be shorter, but not fixed length, so
>> whether that would work for you depends on the API you are feeding these
>> into.
>
> This may seem like a dumb question, but does that preserve order?
My understanding was that UTF-8 was designed so that ordinary byte
comparison operations would work "properly" on UTF-8 strings (presuming no
"overlong encodings" are used; there is no point in such things, it's like
including NOPs in your generated instructions). That's surely true if only
equality is involved; I believe it is also true for ordering, but as I've
never tried it I don't want to say for absolutely certain.
Randy.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-03-07 1:04 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-24 22:01 Convert wide_string to string (as the same byte array) Erich
2012-02-24 22:25 ` Adam Beneschan
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox