1

I want to do the following but I get "Access violation" error.

type Bin = array of byte;

var s:string;

begin
 s:='some string';
 Bin(s)[3]:=ord('X');
 caption:=s;
end;

Why this doesn't work ?

8
  • Why do you want to? You can simply access s[3] directly - there's no need for the array of byte here at all. Commented Jun 12, 2014 at 19:54
  • Very hard to know what you are trying to do. Not least because we don't know what string is. UnicodeString or AnsiString? Commented Jun 12, 2014 at 19:56
  • @SertacAkyuz, yes var "s" it's local. @KenWhite, I want to do some work with the bytes of the string (encription algorithm) without the need to convert the char to byte with the ord() function and back to char with the chr() function. @DavidHeffernan, it is an AnsiString Commented Jun 12, 2014 at 20:04
  • How is this question not a duplicate of Using AnsiString like byte of array in Delphi XE4 or How to convert strings to array of byte and back then? Commented Jun 12, 2014 at 20:11
  • 1
    How about RawByteString? Commented Jun 12, 2014 at 21:25

1 Answer 1

6

This does not work because AnsiString and a dynamic array of byte are incompatible types. Your cast is invalid and anything can happen.

As it turns out, your string is a literal. The compiler handles that by putting the string in read only memory. Hence the access violation when you go behind its back.

The solution is easy enough. Use the [] indexing operator on the string directly:

s[i] := ...;

When you do this, the compiler knows that the string is read-only, and copies it to writeable memory to allow you to modify it.

You say that you don't want to use ord() and chr(). I don't know why. They are the correct things to use and, it's not as if they even result in any code being emitted. They are intrinsics that turn into no-ops.

You state in a comment that you are coding an encryption algorithm. This then points up the fundamental flaw in your approach. Encryption algorithms operate on byte arrays. Don't feed text into an encryption code. Convert to a byte array using some well-defined text encoding. And then operate on byte arrays. And don't reinvent the wheel. Use an existing crypto library.

Sign up to request clarification or add additional context in comments.

2 Comments

Please tell me if I understand corectly. You said that ord() and chr() are not normal functions and no code is generated for then ? Because I need to use them a lot and I was afraid to not slow down the program.
They are intrinsics and since they can be written as no-ops I expect they are. But you are missing the point. Stop using strings as a data type for encryption. Encrypt byte arrays. That's the beginning and the end of it. And when you ever care about performance don't guess. Measure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.