1

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?

Dave
  • 253
  • 6
  • 14
  • possible duplicate of [What's the equivalent of VB's Asc() and Chr() functions in C#?](http://stackoverflow.com/questions/721201/whats-the-equivalent-of-vbs-asc-and-chr-functions-in-c) – DavidG Nov 07 '14 at 13:24
  • I've tried to convert them with Convert.ToChar() but it doesn't recognize the values – Dave Nov 07 '14 at 13:24
  • 1
    Look at the answer posted by DavidG. Also, yes `&H31` is a hexadecimal value. So in C# you'd write: `0x31` – MrPaulch Nov 07 '14 at 13:26
  • Ty MrPaulch, please write an answer so I can accept – Dave Nov 07 '14 at 13:28

3 Answers3

8

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";
Richard
  • 106,783
  • 21
  • 203
  • 265
  • 3
    Note that "special" characters like `\x01` (anything below `\x20`) indicate binary data are not necessarily safe in a string. Depending on the use, a byte array may be better. – Deanna Nov 07 '14 at 13:44
1

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 }
Deanna
  • 23,876
  • 7
  • 71
  • 156
0

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

Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61