vbscript to Delphi - c#

I have a VBScript which I'm converting to Delphi.
I'm struggling with these 2 lines.
Can someone explain what do they do:
init_lib.Flags(&H7FFF)
init_lib.Environment(1)
Couldn't find any documentation related to the 1st line.
I kept the 2nd line as it is in Delphi but not sure how to convert the 1st line.
What's their equivalent in Delphi (or C#) ?

&H is a VB shorthand for hexadecimal value.
In order to use the value in Delphi you have to use the string without the prefix or to convert the string '7FFF' to a decimal value: you know what type of argument Flags accepts.
The '7FFF' hex string corresponds to the decimal 32767.
The shorthand for hexadecimal values in Delphi is $: $7FFF evaluates to an Integer type that can be assigned to a variable or a constant directly.

Related

Convert python byte in string format to byte array in c#

In my python code i have a value in byte code, whenever print that byte code it will give something like this,
b'\xe0\xb6\x9c\xe0\xb7\x92\xe0\xb6\xb1\xe0\xb7\x8a\xe0\xb6\xaf\xe0\xb6\xbb'
now, that value in string format in c# that is,
string byteString = "b'\xe0\xb6\x9c\xe0\xb7\x92\xe0\xb6\xb1\xe0\xb7\x8a\xe0\xb6\xaf\xe0\xb6\xbb'";
so question is how can i convert that byteString to byte array in c#
but, my actual problem is i have a string value in python which is not in English, when i run the python code it will print the string(in non English, work fine).
But, whenever run that python code in c# from process class it work fine for English and i can get the value. but it not working for non English characters, it was a null value. therefore, in python if i print that non English value in byte code i can get the value in c#. problem is how can i convert that in byte code into byte array in c#.
First, you want to modify your string slightly for usage in C#.
var str = "\xe0\xb6\x9c\xe0\xb7\x92\xe0\xb6\xb1\xe0\xb7\x8a\xe0\xb6\xaf\xe0\xb6\xbb";
You can then get your bytes fairly easily with LINQ.
var bytes = str.Select(x => Convert.ToByte(x)).ToArray();
An odd case that can occur with trying to use byte strings between Python and C# is that python will sometimes put out straight ASCII characters for certain byte values, leaving you with a mixed string like b'\xe0ello'. C# recognizes \x##, but it also attempts to parse \x####, which will tend to break when dealing with the output of a python bytestring that mixes hex codes and ascii.

How to decode an integer in MVC5?

I have a method that prints labels from a Web Application in MVC5, for this I am using the Neodinamyc plugin
The problem is that this method delivers the encoded parameters, for the first printerName parameter I was able to decode it in the following way and I succeeded
But I think that the second parameter called location that is of integer type gives it to me as hexadecimal or at least I think so, since the error says: "can not be converted from int to string"
I'm currently testing with
ubicacion = Convert.ToInt32(ubicacion);
and it does not work for me, it still has the same value
Why do I get this hexadecimal type number when I pass an integer?
How can I convert this parameter to an integer? any help for me?
The value doesn't need to be converted, it's already an integer.
Your tooltip is just displaying it in hexadecimal. You can change this by right-clicking the tooltip and unselecting Hexadecimal Display

Need help converting a expression in python to C#

Maybe someone in either of the camps can tell me whats going on here:
Python:
temp = int('%d%d' % (temp2, temp3)) / 10.0;
I'm working on parsing temperature data, and found a piece of python that I can't understand. What is going on here? Is python adding together two numbers here and casting them to int, and then divide by 10?
C# might look like:
temp = ((int)(temp2+temp3))/10;
But I am not sure what that % does? Data is jibberish so I don't know what is correct translation for that line in python to C#
In C# it looks like:
var temp = int.Parse(temp2.ToString() + temp3.ToString())/10f;
or:
var temp = Convert.ToInt32(string.Format("{0}{1}", temp2, temp3))/10f;
this is similar: What's the difference between %s and %d in Python string formatting?
name = 'marcog'
number = 42
print '%s %d' % (name, number)
will print marcog 42. Note that name is a string (%s) and number is an integer (%d for decimal).
See
http://docs.python.org/library/stdtypes.html#string-formatting-operations
for details.
So it seems like the "%" is just telling python to put the values on the right into the placeholders on the left.
from the documentation linked in the answer I quoted:
Given format % values (where format is a string or Unicode object), % conversion specifications in format are replaced with zero or more elements of values. The effect is similar to the using sprintf() in the C language. If format is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object.
Would probably want to set up a python script and try it out, placing your own values into the variables.

Convert expression from C# into Java

I am to convert a C# program into Java. At one point I really don't get what is done, there. I changed the code to clarify the types and give an example.
string myString = "Test";
long l = (long)myString[0];
1) What is the [0] doing with a normal string? Is that even possible? Is it just the substring, in this case "T"?
2) How could you cast a String or character to long, if the String represents a text?
long l = (long)myString[0];
the index of a string gives the char. A char is castable as a long. This will give you the long-value (unicode value) of the character in the first position of the string, i.e., A will be 65.
The cast (long) is not needed in C#, because char has what's called a "implicit cast operator" for long and int. The following statement is just as well correct:
long l = myString[0];
The equivalent of char in C# is char in Java, albeit that implementations are slightly different. In Java, a char can be cast to an int or a long. To get the character, use CharAt().
I think it's converting a char code to long. In Java you would have to:
long l = (long) myString.charAt(0)
A string is an array of characters, so that makes it possible to access given position.
Having said that, myString[0] is accessing the first char, 'T'.
You can then cast a char to a long, converting it to its ASCII position value.
You can use this converter http://www.tangiblesoftwaresolutions.com/index.htm - the trial version is able to convert some amount of lines for free :)

Encoding issue: vbscript "Chr()" to .Net C#

I can't seem to find the answer to this question.
It seems like I should be able to go from a number to a character in C# by simply doing something along the lines of (char)MyInt to duplicate the behaviour of vb's Chr() function; however, this is not the case:
In VB Script w/ an asp page, if my code says this:
Response.Write(Chr(139))
It outputs this:
‹ (character code 8249)
Opposed to this:
(character code 139)
I'm missing something somewhere with the encoding, but I can't find it. What encoding is Chr() using?
Chr() uses the system default encoding, I believe - so it's roughly equivalent to:
byte[] bytes = new byte[] { 139 };
char c = Encoding.Default.GetString(bytes)[0];
On my box (Windows CP1252 as the default) that does indeed give Unicode 8249.
If you want to call something that has exactly the behaviour of VB's Chr from C#, then, why not simply call it rather than trying to deduce its behaviour?
Just put a "using Microsoft.VisualBasic;" at the top of your C# program, add the VB runtime DLL to your references, and go to town.
If you cast an int to a char, you will get the character with the Unicode character code that was in the integer. The char data type is just a 16 bit UTF-16 character code.
To get the equivalent of the VBScript chr() function in .NET you would need something like:
string s = Encoding.Default.GetString(new byte[]{ 139 });

Categories