[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

How to write to high and low bytes of an element in a widestring

Hi,
   I think this is an easy if you know how question.  How do you write to the
high and low bytes of an element in a widestring array individually.

If you know of a way to convert strings to widestings without adding on an extra
00 byte to each character and just kind of "cramming the bytes" in then that would be just as good.

Thanks

David
0
dlking
Asked:
dlking
  • 7
  • 5
  • 3
1 Solution
 
LischkeCommented:
Hi,

for converting a string to a wide string just use an assigment. Delphi will take care about the conversion:

var
  S: String;
  WS: String;

begin
  S := 'Hallo';
  WS := S;
  // WS contains now a valid wide string
end;

Ciao, Mike
0
 
KECommented:
1. To access the high and low bytes use the WideChar type cast.

ws: WideString;
w: Word;
bh, bl: Byte;
ws := 'test';
ws[1] := WideChar($2034);
ws[2] := WideChar(w);
ws[3] := WideChar( (bh shl 8)+bl );

2. To convert to and from strings, typecast to widestring.

ws: WideString;
s: String;

ws := WideString(s);
s := String(ws);


3. Beware that you need to cast characters to wide char/string when you concatenate strings...
If not, the compiler will convert the WideString into a string, concatenate the character/string and then convert it back into a WideString.

ws: WideString

ws := WideString('Test');
ws := ws + 'ing'; // This will mess up your widestring characters
ws := ws + WideString('ing'); // this is OK
// Same for chars...
ws := ws + WideChar('.');

Regards

0
 
dlkingAuthor Commented:
hi,
    thanks a lot for your help

Bye
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
LischkeCommented:
KE,

to 1): "If you know of a way to convert strings to widestings without adding on an extra
00 byte to each character and just kind of "cramming the bytes" in then that would be just as good."
--> I did this and so your point 1) is superfluous.

to 2) This kind of casting is superfluous as I have shown in my comment and you can easily follow by looking at the disassembly view while executing string code.

to 3) What's all this good for? Please, write a little demo program and look at the CPU window. Writing:

var
  S: WideString;

begin
  S := 'Blah';
  S := S + 'Blah';
end;

results in this code sequence:

  :
  call @WStrAsgn
  :
  call @WStrCat
  :
  call @WStrClr

show it is simple nonsense what you wrote.

Please, be so kind and use COMMENTS instead of answers in particular if you either only repeat already given suggestions/solutions and/or if you aren't unsure and have not verified your own suggestion!

Ciao, Mike
0
 
LischkeCommented:
dlking,

WHAT ABOUT MY HELP???
0
 
dlkingAuthor Commented:
hi lischke,
 
I tried your suggestion but as i thought
it does add extra bytes. I think perhaps my question was not clear enough

what i wanted to do was to take a string of the form ( in bytes)

 $a2 $67 $a3 $45

and create a widestring with exactly the same sequence ie not

  $a2 $00 $67 $00 $a3 $00 $45 $00

which delphi does when it converts to
widestring.
At least my delphi 3 does.

Thanks though for your help and i'll try
and be clearer with my questions in future

Bye
0
 
LischkeCommented:
Okay, but why did you then accept KE's answer which has nothing to do with your problem? For your problem you could esily use:

var
  Buffer: array[0..1024] of Byte;
  S: WideString;

begin
  ReadBytesIntoBuffer(Buffer);
  S := PWideChar(@Buffer);
end;

Quite easy, huh? :-)

Ciao, Mike
0
 
dlkingAuthor Commented:
Hi,
    KE's answer was useful - i did want to know how to manipulate the bytes etc
in the way he showed me.  Your solution
above is also useful and tidier for the
actual string conversion so thats some points i owe you....

Thanks Again

David
0
 
KECommented:
Mike, I'm glad that you are watching me over the shoulder, but is it possible to do it a little more quiet ?

I've worked a LOT with WideStrings, actually from D3 and up. D3 and D4 did not handle WideStrings correctly so that's why I wrote these "idiot proof" typecasting's.

Load up your D3 (or D4) and try this example:

ws: WideString;
ws := some_wide_string_containing_TRUE_widechars
ws := ws + 'flaming out';

What you will get is a WideString where all TRUE widechars are converted to questionmarks = '?' = $2E as they would be in a normal WideString to String conversion.

However if you do as I describe:
ws := ws + WideString('flaming out');

your widestring will be CORRECTLY concatenated and contain it's original WideChars intact...

This is (what you may call) a BUG in the older versions...


BTW. This is the the first part of the question...
"How do you write to the high and low bytes of an element in a widestring array individually"
Where the HELL is your answer to that ???

And SORRY that I posted my comment as an answer - As I started on writing my answer I didn't see your comment, since it wasn't posted at that time - and I have to admit that I don't press refresh before submit - do you ?

Regards

PS. When I speak of TRUE WideChars I mean characters that is not within the bounds of a normal char.

PPS. I also think you are overreacting...
0
 
dlkingAuthor Commented:
Hi,
    KE's answer was useful - i did want to know how to manipulate the bytes etc
in the way he showed me.  Your solution
above is also useful and tidier for the
actual string conversion so thats some points i owe you....

Thanks Again

David
0
 
LischkeCommented:
KE, you may have right in many aspects here (in particular regarding overreaction), but the main problem is that you actually posted an ANSWER! There are only two cases when I post an answer. One is for questions like "How many is 2 + 2?" and the other is the questioneer requests it. How does it make sense to discuss questions if there's already an answer telling all other: "get out of here, I found this one first, this is mine, mine, mine"?

Proposing an answer should totally be disabled as it causes often frustration (not only for me :-)) and with the "accept comment as answer" option all what's needed is there. I'll suggest that to the EE stuff...

I have tried what you wrote regarding string casts with D4 and D5. Neither shows any unusual behaviour nor does they need explicit type casts, so the problem you mentioned might be caused by bugs in D3 and earlier.

Unfortunately, David didn't mention the Delphi version he's using, so it seems we both are true with our stuff. We had, though, discuss this normally if you hadn't proposed an answer in the first place...

Ciao, Mike
0
 
dlkingAuthor Commented:
Hi,
    KE's answer was useful - i did want to know how to manipulate the bytes etc
in the way he showed me.  Your solution
above is also useful and tidier for the
actual string conversion so thats some points i owe you....

Thanks Again

David
0
 
LischkeCommented:
David, don't use the refresh button of your browser or you'll resend your last comment. There's a link "Reload Question" at the top of this page. Use this one instead :-)

Ciao, Mike
0
 
KECommented:
Lische I agree that I shouldn't post my answer as the very first thing - you are right I should have posted a comment. Believe it or not, I normally do this. However I may have seen this question as a "2+2", since I've worked so much with WideStrings and especially BUGS concerning these. I'll try to remember not to post my answers so quickly as it will also leave other's out of the discussion - this is especially a problem if you give wrong answers - so I'm sorry.

Still, I think my answer is more complete than your's so I might have got the points anyway ;-)

Anyway, I can transfer the point to you, as I'm not here for the points - only for the excitement.

Regards

PS. No hard feelings - OK !
0
 
LischkeCommented:
Hi KE,

now that we cleared that I'm feeling much better :-) Okay, in this case you gave what was needed but often an (wrong) answer is given before even a discussion started and that's not the kind EE is working nor should it.

Ciao, Mike

PS: No need to transfer any points to me...
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now