VB ASCB in LotusScript? - vb6

Working on converting a Visual Basic SHA-256 encryption routine to work in LotusScript.
Is going well except for the VB's AscB command.
Found: "Use the AscB function to return the first byte of a string containing byte data."
Not finding way to do same in LotusScript.
See the LS CByte command comes close: "CByte returns an expression that has been converted to Byte."
Don't see way to have it return just the first Byte of the expression.
Any suggestions?
Derek

AscB is only appropriate for strings in single-byte character encoding. All LotusScript string data is Unicode represented in UTF16 double-byte encoding.
The LotusScript Uni() function returns a Long containing the integer value of the Unicode character. Since the input is a double byte character, the value returned by Uni() ranges from 0 to 65535. If you want to get the values of each of the two bytes, code like this will do the trick:
Dim ws As New NotesUIWorkspace
Dim s1 As String
Dim u1 As Long
Dim u2 As Long
Dim lowbyte As Integer
Dim highbyte As Integer
Dim b1 As Byte
Dim b2 as Byte
s1 = "Ʃ"
u1 = Uni(s1)
lowbyte = u1 Mod 256
highbyte = (u1 - lowbyte) / 256
b1 = Cbyte(lowbyte)
b2 = Cbyte(highbyte)
Call ws.Prompt(prompt_ok,"test",s1 + " " + Cstr(Cint(b1)) + " " + Cstr(Cint(b2)))

Would Asc, LeftB and RightB do what you need?
In my testing...
Lenb("A") = 2
Leftb("A", 1) = "A"
Asc(Leftb("A", 1)) = 65
Leftb("A", 2) = "A"
Asc(Leftb("A", 2)) = 65
Asc(Rightb(Leftb("A", 2), 1)) = 0

Related

typecasting error in 64 bit. type mismatch

I am having problem in deploying VB6 apps into 64 bit windows. When I run the EXE, It gives out error "type mismatch"
The code is below :
Private Sub writedate (loc as string)
Dim dout(0 To 15) As Byte
Dim strdate_day as string
Dim strdate_mon as string
:
:
dout(0) = strdate_day 'This is the problem in 64 bit
dout(1) = strdate_mon
End sub
Public Function **Hex_Dec2**(ByVal val As String, ByVal start As Byte) As Byte
Dim MSB As Byte
Dim LSB As Byte
Dim Fbyte As Byte
Fbyte = 0
MSB = 0
LSB = 0
MSB = StrHEX_Dec(Mid(val, start, 1))
LSB = StrHEX_Dec(Mid(val, start + 1, 1))
Fbyte = (MSB * 16) + LSB
Hex_Dec2 = Fbyte
Debug.Print "val : " & val & " Fbyte : " & Fbyte
End Function
If I run the EXE file on 32 bit machine, it works fine. Absolutely no error.
The code HEX_DEC2 is a special function that converts string into byte. I have to use this function otherwise the value written will be incorrect. When I do : dout(0) = HEX_DEC2(strdate_day), it does not give "type mismatch" error on 64bit. For technical reason, i don't want to run this function
Strangely, the laptop that I am using to develop the app is 64 bit as well and doesn't give out any error when i run it from both the source code and the EXE file.
How do i overcome this problem ?

How do I extract numbers from a string in VB6?

I have a string that looks something like 'NS-BATHROOMS 04288'
I only want the numbers.
I hve searched for answers, but none so far even get pst the compiler.
How can I do this?
without regex you can do it with: (altough VB6/VBA Code really isn`t nice to look at)
Public Function ReturnNonAlpha(ByVal sString As String) As String
Dim i As Integer
For i = 1 To Len(sString)
If Mid(sString, i, 1) Like "[0-9]" Then
ReturnNonAlpha = ReturnNonAlpha + Mid(sString, i, 1)
End If
Next i
End Function
I'd personally use regex. You can match given regex patterns to achieve what you need. This function matches only digits.
For VB6 you'd do something like:
Dim myRegExp, ResultString
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "[\d]"
Then you'd go against your String.
https://support.microsoft.com/en-us/kb/818802
You can use this function for extract numerical chr as string value:
Public Function Str_To_Int(MyString As Variant) As Long
Dim i As Integer
Dim X As Variant
If IsNull(MyString) Or MyString = "" Then
Str_To_Int = 0
Exit Function
End If
For i = 1 To Len(MyString)
If IsNumeric(Mid(MyString, i, 1)) = True Then
X = Nz(X, "") & Mid(MyString, i, 1)
End If
Next i
Str_To_Int = Nz(X, 0)
End Function

vbscript: convert unicode string to array of bytes

I have unicode string passed to vbscript procedure (not visual basic 6, but vbscript). I want to iterate unicode string char by char, get code for every symbol, truncate code to byte range [0..255] and create array of bytes.
This way new array of bytes should be twice smaller in memory compared to original unicode string. I am going save this array to file via ADODB.Stream object further
How can I convert unicode string to bytes array with symbol code truncated to byte range?
Thank you in advance!
Firstly, translating unicode to ascii will only work if your string only contains ascii characters. Since unicode contains ascii, it is just a matter of removing every second character.
Look up unicode on the internet for details.
EDIT: In unicode, every ascii character is proceeded with a NULL (0) byte. Remove this byte to convert the string to ASCII.
It seems there is no way to create array of bytes in vbs (though it's very straightforward in visual basic) -- all arrays are arrays of variants.
The task was to send binary stream from server to vbs script via string type. I have found the solution by creating Xml Document on the server with CDATA section that contains base64 coded array of bytes as string data.
Client (vbs) do the following:
set xmlDoc = CreateObject("Microsoft.XmlDom")
xmlDoc.loadXML(dataFromServer)
base64str = xmlDoc.DocumentElement.Text ' it's base64 coded binary stream
arrayOfBytes = decodeBase64(base64str)
Function decodeBase64(base64)
set dm = CreateObject("Microsoft.XMLDOM")
set el = dm.createElement("tmp")
el.DataType = "bin.base64"
el.Text = base64
decodeBase64 = el.NodeTypedValue
set dm = Nothing
End Function
This function creates an array of bytes:
' http://www.motobit.com/tips/detpg_binarytostring/
Function MultiByteToBinary(MultiByte)
'� 2000 Antonin Foller, http://www.motobit.com
' MultiByteToBinary converts multibyte string To real binary data (VT_UI1 | VT_ARRAY)
' Using recordset
Dim RS, LMultiByte, Binary
Const adLongVarBinary = 205
Set RS = CreateObject("ADODB.Recordset")
LMultiByte = LenB(MultiByte)
If LMultiByte>0 Then
RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
RS.Open
RS.AddNew
RS("mBinary").AppendChunk MultiByte & ChrB(0)
RS.Update
Binary = RS("mBinary").GetChunk(LMultiByte)
End If
MultiByteToBinary = Binary
End Function
This function creates a multi-byte string.
' http://www.motobit.com/help/regedit/pa26.htm
'Converts unicode string to a multibyte string
Function StringToMB(S)
Dim I, B
For I = 1 To Len(S)
B = B & ChrB(Asc(Mid(S, I, 1)))
Next
StringToMB = B
End Function

Converting Decimal to ASCII Character

I am trying to convert an decimal number to it's character equivalent. For example:
int j = 65 // The character equivalent would be 'A'.
Sorry, forgot to specify the language. I thought I did. I am using the Cocoa/Object-C. It is really frustrating. I have tried the following but it is still not converting correctly.
char_num1 = [working_text characterAtIndex:i]; // value = 65
char_num2 = [working_text characterAtIndex:i+1]; // value = 75
char_num3 = char_num1 + char_num2; // value = 140
char_str1 = [NSString stringWithFormat:#"%c",char_num3]; // mapped value = 229
char_str2 = [char_str2 stringByAppendingString:char_str1];
When char_num1 and char_num2 are added, I get the new ascii decimal value. However, when I try to convert the new decimal value to a character, I do not get the character that is mapped to char_num3.
Convert a character to a number in C:
int j = 'A';
Convert a number to a character in C:
char ch = 65;
Convert a character to a number in python:
j = ord('A')
Convert a number to a character in Python:
ch = chr(65)
Most languages have a 'char' function, so it would be Char(j)
I'm not sure what language you're asking about. In Java, this works:
int a = 'a';
It's quite often done with "chr" or "char", but some indication of the language / platform would be useful :-)
string k = Chr(j);

how do you parse a string in vb6?

Some of us unfortunately are still supporting legacy app like VB6. I have forgotten how to parse a string.
Given a string:
Dim mystring As String = "1234567890"
How do you loop in VB6 through each character and do something like
for each character in mystring
debug.print character
next
In C# i would do something like
char[] myChars = mystring.ToCharArray();
foreach (char c in theChars)
{
//do something with c
}
Any ideas?
Thanks a lot
You can use the 'Mid' function to get at the individual characters:
Dim i As Integer
For i = 1 To Len(mystring)
Print Mid$(mystring, i, 1)
Next
Note this is untested.
There is no possibility to use foreach on strings.
Use
Dim i As Integer
For i = 1 To Len(YourString)
Result = Mid$(YourString, i, 1)
Next
note that the type of Result is a length-1 string, no char or byte type.
If performance is important, you'll have to convert the string to a bytearray fist (using StrConv) and then loop through it like this.
Dim i As Long
For i = 0 To UBound(Data)
Result = Data(i) ' Type is Byte '
Next
This is much more efficient.
The easiest way is to convert the string into an array of bytes and iterate over the byte array (converting each byte to a character).
Dim str As String
Dim bytArray() As Byte
Dim count As Integer
str = "This is a string."
bytArray = str
For count = 0 To UBound(bytArray)
Debug.Print Chr(bytArray(count))
Next
Don't loop; rather, set a reference to Microsoft VBScript Regular Expressions library and use regular expressions to achieve your 'do something' goal.

Resources