Dave
Dave

Reputation: 253

Translating VB6 chars to C# strings

I want to upgrade an old VB6 project to newer technologies and I have these values below and don't know how to translate them.

Chr(&H31) & Chr(&H1) & Chr(&H50) & Chr(&H31) & Chr(&H17)

So my first question is how do I identify these? Is it hexadecimal values or something else? I don't seem to find them in a ascii table. What does the 'H' stand for?

Secondly, how do I make a c# string out of this?

Upvotes: 1

Views: 175

Answers (3)

Deanna
Deanna

Reputation: 24313

As your "characters" include special/control/unprintable characters (1<SOH>P1<ETB>), I expect this is actually non string data, and as such should not be stored as a string.

Depending on the actual desired usage, you will be better off with a byte array:

byte[] data = new byte[] { 0x31, 0x01, 0x50, 0x31, 0x17 }

Upvotes: 1

Richard
Richard

Reputation: 109180

Chr converts a character code into the character, in C# you can just cast:

char c1 = (char)0x31;

(Also changing to use C#'s hexidecimal literals rather than VB6's.)

But when building a string, using escapes is easier:

string s1 = "\x31\x01\x50\x31\x16";

Upvotes: 8

Nadeem_MK
Nadeem_MK

Reputation: 7699

If you are not able to identify the function parameter and still wnat the same result, you can always call the native VB function from your C# application.

  • You just need to add the reference Microsoft.VisualBasic
  • Call the function Strings.Chr

You are more confident of the result :)

Upvotes: 0

Related Questions