I have this Delphi code:
function EnDeCrypt(const Value : String) : String;
var
CharIndex : integer;
begin
Result := Value;
for CharIndex := 1 to Length(Value) do
Result[CharIndex] := chr(not(ord(Value[CharIndex])));
end;
how can it be translated to .Net?
If the function that RRUZ provided (now deleted) is really what you want (and I'm still a little sceptical of the encoding issues) then you can write it like this:
Private Function EnDeCrypt(ByVal Value As String) As String
Dim transformed = Encoding.Unicode.GetBytes(Value).Select( _
Function(item) Not item)
Return Encoding.Unicode.GetString(transformed.ToArray())
End Function
Related
How do I return a result from a function?
For example:
Public Function test() As Integer
return 1
End Function
This gives a compile error.
How do I make this function return an integer?
For non-object return types, you have to assign the value to the name of your function, like this:
Public Function test() As Integer
test = 1
End Function
Example usage:
Dim i As Integer
i = test()
If the function returns an Object type, then you must use the Set keyword like this:
Public Function testRange() As Range
Set testRange = Range("A1")
End Function
Example usage:
Dim r As Range
Set r = testRange()
Note that assigning a return value to the function name does not terminate the execution of your function. If you want to exit the function, then you need to explicitly say Exit Function. For example:
Function test(ByVal justReturnOne As Boolean) As Integer
If justReturnOne Then
test = 1
Exit Function
End If
'more code...
test = 2
End Function
Documentation: Function Statement
VBA functions treat the function name itself as a sort of variable. So instead of using a "return" statement, you would just say:
test = 1
Notice, though, that this does not break out of the function. Any code after this statement will also be executed. Thus, you can have many assignment statements that assign different values to test, and whatever the value is when you reach the end of the function will be the value returned.
Just setting the return value to the function name is still not exactly the same as the Java (or other) return statement, because in java, return exits the function, like this:
public int test(int x) {
if (x == 1) {
return 1; // exits immediately
}
// still here? return 0 as default.
return 0;
}
In VB, the exact equivalent takes two lines if you are not setting the return value at the end of your function. So, in VB the exact corollary would look like this:
Public Function test(ByVal x As Integer) As Integer
If x = 1 Then
test = 1 ' does not exit immediately. You must manually terminate...
Exit Function ' to exit
End If
' Still here? return 0 as default.
test = 0
' no need for an Exit Function because we're about to exit anyway.
End Function
Since this is the case, it's also nice to know that you can use the return variable like any other variable in the method. Like this:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test <> 1 Then ' Test the currently set return value
test = 0 ' Reset the return value to a *new* value
End If
End Function
Or, the extreme example of how the return variable works (but not necessarily a good example of how you should actually code)—the one that will keep you up at night:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test > 0 Then
' RECURSIVE CALL...WITH THE RETURN VALUE AS AN ARGUMENT,
' AND THE RESULT RESETTING THE RETURN VALUE.
test = test(test - 1)
End If
End Function
How do I mirror the bits of an integer of unknown length in VB.Net?
By mirroring I mean putting the first bit last and so on.
i.E.:
10010100 mirrors to
00101001
The functions construct would be:
Public Function ReveseIntegerBitwise(ByVal initialInteger As Integer)
Dim reversedInteger As Integer
' Do Something with initialInteger and put it in reversedInteger
Return reversedInteger
End Function
You can do:
Function Foo (value as Integer) as String
Return new String(Array.Reverse(Convert.ToString(value,2).ToCharArray()))
End Function
and it returns a string with the reversed bits.
I have looked on the web and I cannot find anything that helps me, all I can find is changing the characters into ASCII or Hexadecimal. However I would like to do it a different way. For example, say the string that got passed in was abcdef, I would like to have a key which changes these characters into another string such as qwpolz. Is there an easier way than declaring each character in the alphabet to be another character like:
Dim sText As String = "Hello"
Dim sEncode As String = ""
Dim iLength As Integer
Dim i As Integer
iLength = Len(sText)
For i = 1 To iLength
sEncode = sEncode ????
Next
Return sEncode
And then have a very lengthy loop which checks for these loops? There must be a much simpler way. Can anybody help by pointing me in the right direction?
Edit: Why downvote? Seriously, it's a legitimate question. Instead of downvoting for no reason, just move onto another question.
Well actually, this sounds like a Caesar sipher
Private Overloads Shared Function Encrypt(ByVal ch As Char, ByVal code As Integer) As Char
If Not Char.IsLetter(ch) Then
Return ch
End If
Dim offset As Char = IIf(Char.IsUpper(ch), "A", "a")
Return CType((((ch + (code - offset)) Mod 26) + offset),Char)
End Function
Private Overloads Shared Function Encrypt(ByVal input As String, ByVal code As Integer) As String
Return New String(input.ToCharArray.Select(() => { }, Encrypt(ch, code)).ToArray)
End Function
Private Shared Function Decrypt(ByVal input As String, ByVal code As Integer) As String
Return Encrypt(input, (26 - code))
End Function
Note that this assumes, that you use English alphabet. In general case where for example you have 'ä', 'ö', 'š', 'ž', 'ß', 'õ', 'ü' etc. this would not work. In that case it is simpler to just create a list/dictionary of your ordered alphabet and use it.
Example use:
encrypted = Encrypt(sText, 5)
decypted = Decrypt(encrypted, 5)
Sounds as if you want to modify a string by replacing each character with a different character according to a mapping table. An efficient approach is to use a Dictionary(Of Char, Char). But easier to write and maintain is something like this:
Shared ReadOnly replaceChars As String = "abcdef"
Shared ReadOnly withChars As String = "qwpolz"
Public Shared Function ReplaceAll(input As String) As String
Dim newChars = From c In input
Let index = replaceChars.IndexOf(c)
Select If(index >= 0, withChars(index), c)
Return String.Concat(newChars)
End Function
So the first string contains the chars that you want to replace and the second the replacement characters. Both strings must have the same length.
If you want to support case insensitivity:
Public Shared Function ReplaceAll(input As String, comparison As StringComparison) As String
Dim newChars = From c In input
Let index = replaceChars.IndexOf(c.ToString(), comparison)
Select If(index >= 0, withChars(index), c)
Return String.Concat(newChars)
End Function
Note that this is also a loop. There is no way to avoid some kind of loops if you want to replace multiple characters or strings.
I read values from a cell and the return is not fix. Instead of writing many functions for each return type I try to write a function with a generic return type but I stuck with this:
Public Function GetValueFromCells(Of t)(ByVal CellName As String) As t
Return CType(_xlWorksheet.Range(CellName).Value2)
End Function
No matter what type I will find in the cell, I want to convert the value to the return type I set in the function. But all my attemps have been in vain. It would be great if anyone could help me get this accomplished.
It's been awhile since I touched VB.Net, but you should be able to do this:
Public Function GetValueFromCells(Of t)(ByVal CellName As String) As t
Return CType(_xlWorksheet.Range(CellName).Value2, t)
End Function
I would address a couple of things.
Use more descriptive Generic Type arguments and just declare a type of the Generic and return that.
Public Function GetValueFromCells(Of TCellValue)(ByVal cellName As String) As TCellValue
Dim specificValue As TCellValue = Nothing
specificValue = DirectCast(_xlWorksheet.Range(cellName).Value2, TCellValue)
Return specificValue
End Function
I've been given this simple VB application and library which I'm told can open a door/turnstyle attached to the printer port at 0x378 base address.
'Inp and Out declarations for port I/O using inpout32.dll.
Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" _
(ByVal PortAddress As Integer) _
As Integer
Public Declare Sub Out Lib "inpout32.dll" Alias "Out32" _
(ByVal PortAddress As Integer, _
ByVal Value As Integer)
------------------------------------------------------------------------------------
Option Explicit
Dim Value As Integer
Dim PortAddress As Integer
Private Sub cmdWriteToPort_Click()
'Write to a port.
Out PortAddress, Value
'Read back and display the result.
Text1.Text = Inp(PortAddress)
Value = Value + 1
If Value = 255 Then Value = 0
End Sub
Private Sub Form_Load()
'Test program for inpout32.dll
Value = 0
'Change PortAddress to match the port address to write to:
'(Usual parallel-port addresses are &h378, &h278, &h3BC)
PortAddress = &H378
End Sub
However I need to re-write it in Delphi 5 to integrate in to my application.
Is it possible to access the same library through D5?
Am I in the right direction with the following code?
//Inp and Out declarations for port I/O using library
function Inp(PortAddress:String); external 'inpout32.dll.dll'
begin
return ??
end;
procedure Output(PortAddress:String;Value:Integer); external 'inpout32.dll.dll'
procedure TForm1.FormActivate(Sender: TObject);
begin
//Test program for inpout32.dll
Value := 0;
//Change PortAddress to match the port address to write to:
//(Usual parallel-port addresses are &h378, &h278, &h3BC)
PortAddress := '&H378';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//Write to a port.
Output(PortAddress, Value);
//Read back and display the result.
Edit1.Text := Inp(PortAddress);
Value := Value + 1;
if Value = 255 then
Value := 0;
end;
I'm not sure exactly how to declare the library functions and what to declare the variables as (&H378 is obviously not an integer)
thanks
PortAddress is declared as an Integer, so don't use strings. Your code should look something like this:
//Inp and Out declarations for port I/O using inpout32.dll.
function Inp(PortAddress: Integer): Integer; stdcall; external 'inpout32.dll' name 'Inp32';
procedure Output(PortAddress, Value: Integer); stdcall; external 'inpout32.dll' name 'Out32';
procedure TForm1.FormActivate(Sender: TObject);
begin
//Test program for inpout32.dll
Value := 0;
//Change PortAddress to match the port address to write to:
//(Usual parallel-port addresses are $378, $278, $3BC)
PortAddress := $378;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//Write to a port.
Output(PortAddress, Value);
//Read back and display the result.
Edit1.Text := IntToStr(Inp(PortAddress));
Value := Value + 1;
if Value = 255 then
Value := 0;
end;
You should completely drop using inpout32.dll since it's only used to directly access printer port and complicates your code conversion. You can do the same much more efficient with ZLOPRTIO Delphi library.