简体   繁体   中英

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 ac# string out of this?

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";

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 }

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM