Jump to content
Sign in to follow this  
baoquan.zuo

Blog: Byte Loss in String-Literal Concatenation

Recommended Posts

Hi,

 

I'd like to share a post. It addressed a byte loss issue captured from a discussion.

// Compile with code page 936
program Problem;

const
  strPublicKey: RawByteString =
    #$30#$3C#$30#$0D#$06#$09#$2A#$86#$48#$86#$F7#$0D#$01#$01#$01#$05 +
    #$00#$03#$2B#$00#$30#$28#$02#$21#$00#$A4#$65#$B8#$CD#$B4#$29#$A9 +
    #$64#$1A#$C5#$80#$55#$22#$1B#$BB#$C5#$98#$36#$B9#$23#$0C#$CA#$D4 +
    #$A8#$B8#$7C#$E6#$32#$E3#$89#$3D#$77#$02#$03#$01#$00#$01;
begin
  Writeln(Length(strPublicKey)); // expected 62 got 58 - why?
  Readln;
end.

https://devjetsoftware.com/delphi/byte-loss-in-string-literal-concatenation/

  • Like 2

Share this post


Link to post

What is wrong with the world of Delphi programmers that in 2025 there are still people who can't understand the difference between text and bytes? 

 

The article you link to goes on and on about text but your data is bytes. Why not just use the correct data type? 

  • Like 1

Share this post


Link to post
2 minutes ago, David Heffernan said:

What is wrong with the world of Delphi programmers that in 2025 there are still people who can't understand the difference between text and bytes? 

 

The article you link to goes on and on about text but your data is bytes. Why not just use the correct data type? 

Yes — it’s clear we should use the proper data type to represent raw bytes.

 

What the article (and my curiosity) really digs into is why the data loss occurs and how DCC handles string literals (as there is no formal Delphi language specification). That matters to me because I’m also writing some Delphi compiler-frontend code in my products.

 

In any case, these insights should help when migrating legacy ANSI-based Delphi projects.

Share this post


Link to post
5 minutes ago, baoquan.zuo said:

In any case, these insights should help when migrating legacy ANSI-based Delphi projects

I don't see this as helpful to anyone. Use bytes to represent bytes. Use strings to represent text. Don't use ANSI strings. 

Share this post


Link to post
13 minutes ago, David Heffernan said:

I don't see this as helpful to anyone. Use bytes to represent bytes. Use strings to represent text. Don't use ANSI strings. 

Thanks for sharing your view.

Share this post


Link to post

Of course there ARE times when the use of ANSI strings makes sense. One example is when sending data to/from an external device down an RS232 port where the external device uses a protocol based on simple ANSI text. We have many real world cases such as this (eg Eurotherm temperature controllers). The key point that @David Heffernan makes is that you should choose your types carefully to closely (or exactly!) reflect your needs. Time spent thinking carefully about your type selection will save you time in the long run.....

  • Like 2

Share this post


Link to post
2 hours ago, Roger Cigol said:

Of course there ARE times when the use of ANSI strings makes sense. One example is when sending data to/from an external device down an RS232 port where the external device uses a protocol based on simple ANSI text. We have many real world cases such as this (eg Eurotherm temperature controllers). The key point that @David Heffernan makes is that you should choose your types carefully to closely (or exactly!) reflect your needs. Time spent thinking carefully about your type selection will save you time in the long run.....

I mean, you work with strings and do TEncoding.ASCII.GetBytes

Share this post


Link to post
20 minutes ago, David Heffernan said:

I mean, you work with strings and do TEncoding.ASCII.GetBytes

All good ! There is more than one way to skin a cat.....

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×