Convert VB.net to Gambas
Posted
#1
(In Topic #705)
Enthusiast

I have managed to convert some code from Java to VB.net but I was wondering if someone could look over it and see what I need to change to covert it to Gambas (so it would work)
This is the code that would send the image to the Printer
Code
Dim bitmapString As String = New String(bitmapBytes, Charsets.US_ASCII)
bitmapString = bitmapString.substring(DataOffset, bitmapStringSize)
Dim commandString As String = ""
Dim commandLength As Integer = (bitmapStringSize.length + 11)
pL = (commandLength Mod 256)
If (commandLength < 256) Then
pH = 0
Else
pH = ((commandLength - pL) _
/ 256)
End If
xL = (bitmapWidth Mod 256)
If (bitmapWidth < 256) Then
xH = 0
Else
xH = ((bitmapWidth _
- (bitmapWidth Mod 256)) _
/ 256)
End If
yL = (bitmapHeight Mod 256)
If (bitmapHeight < 256) Then
yH = 0
Else
yH = ((bitmapHeight _
- (bitmapHeight Mod 256)) _
/ 256)
End If
commandString = (commandString + Utils.H("1B"))' 27
Utils.H("40")' 64
Utils.H("1B")' 27
Utils.H("3D")' 61
Utils.H("01")' 1
Utils.H("1D")' GS = 29
Utils.H("28")' ( = 40
Utils.H("4C")' L = 76
Utils.D(pL)' pL
Utils.D(pH)' pH
Utils.H("30")' m = 48
Utils.H("43")' fn = 67
Utils.H("30")' a = 48
Utils.H(KC1)' kc1
Utils.H(KC2)' kc2
Utils.H("01")' b = 1
Utils.D(xL)' xL
Utils.D(xH)' xH
Utils.D(yL)' yL
Utils.D(yH)' yH
Utils.H("31")' c = 49
commandString = (commandString + bitmapString)
Code
Public Shared Function decodeBitmap(ByVal bitmapBytes() As Byte) As Byte()
Dim bmp As Bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length)
Dim zeroCount As Integer = (bmp.getWidth Mod 8)
Dim zeroStr As String = ""
If (zeroCount > 0) Then
Dim i As Integer = 0
Do While (i < (8 - zeroCount))
zeroStr = (zeroStr + "0")
i = (i + 1)
Loop
End If
Dim list As List(Of String) = New ArrayList
Dim i As Integer = 0
Do While (i < bmp.getHeight)
Dim sb As StringBuilder = New StringBuilder
Dim j As Integer = 0
Do While (j < bmp.getWidth)
Dim color As Integer = bmp.getPixel(j, i)
Dim r As Integer = ((color + 16) _
And 255)
Dim g As Integer = ((color + 8) _
And 255)
Dim b As Integer = (color And 255)
' if color close to whitebit='0', else bit='1'
If ((r > 160) _
AndAlso ((g > 160) _
AndAlso (b > 160))) Then
sb.append("0")
Else
sb.append("1")
End If
j = (j + 1)
Loop
If (zeroCount > 0) Then
sb.append(zeroStr)
End If
list.add(sb.toString)
i = (i + 1)
Loop
Dim bmpHexList As List(Of String) = binaryListToHexStringList(list)
Dim commandList As List(Of String) = New ArrayList
commandList.addAll(bmpHexList)
Return hexListToBytes(commandList)
End Function
I Know i am asking a lot but I am still not 100% up on what I have to do to convert from VB to Gambas
Below is the Java Code (I managed to use a online service that converted it from Java to VB.net)
Code
public static byte[] decodeBitmap(byte[] bitmapBytes) \{
Bitmap bmp = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
int zeroCount = bmp.getWidth() % 8;
String zeroStr = "";
if (zeroCount > 0) \{
for (int i = 0; i < (8 - zeroCount); i++) \{
zeroStr = zeroStr + "0";
}
}
List<String> list = new ArrayList<>();
for (int i = 0; i < bmp.getHeight(); i++) \{
StringBuilder sb = new StringBuilder();
for (int j = 0; j < bmp.getWidth(); j++) \{
int color = bmp.getPixel(j, i);
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = color & 0xff;
// if color close to white,bit='0', else bit='1'
if (r > 160 && g > 160 && b > 160)
sb.append("0");
else
sb.append("1");
}
if (zeroCount > 0) \{
sb.append(zeroStr);
}
list.add(sb.toString());
}
List<String> bmpHexList = binaryListToHexStringList(list);
List<String> commandList = new ArrayList<>();
commandList.addAll(bmpHexList);
return hexListToBytes(commandList);
}
Posted
Enthusiast

The best thing you can do is go ahead and cut and paste your code and see what errors you get. Then ask about the specifics. I converted much of my stuff from VB to gambas and found I made significant changes mostly due to gambas being easier. That caused me to overthink many things and after getting my head out of MS VB and into the more simple gambas I was fine. Try the cut and paste and come back with just the problems and you will get help. I will try to keep a watch.
Posted
Guru

sadams54 said
where to start? first of all gambas is not a loose as VB. So the first thing is you CAN NOT dim anything in the middle of code or conditionally. ALL dim must be done in the beginning of a function or sub.
Sorry fella that's just not true.
You can Dim anywhere you want inside a function.
You can even Dim inside a For/While loop
sadams54 said
You will also find that all the weird things you do in VB are not necessary in gambas.
The best thing you can do is go ahead and cut and paste your code and see what errors you get. Then ask about the specifics. I converted much of my stuff from VB to gambas and found I made significant changes mostly due to gambas being easier. That caused me to overthink many things and after getting my head out of MS VB and into the more simple gambas I was fine. Try the cut and paste and come back with just the problems and you will get help. I will try to keep a watch.
All that's true though
A VB to Gambas converter been asked for many times. It's just not possible.
THE ONLY way is to learn Gambas, once you know how to code with Gambas then converting VB code yourself is easy, albeit time consuming
Posted
Guru

Posted
Enthusiast

And I am also trying to convert a DLL that add promotions support to my EPoS application in windows.
Posted
Guru

Posted
Enthusiast

BruceSteers said
sadams54 said
where to start? first of all gambas is not a loose as VB. So the first thing is you CAN NOT dim anything in the middle of code or conditionally. ALL dim must be done in the beginning of a function or sub.
Sorry fella that's just not true.
You can Dim anywhere you want inside a function.
You can even Dim inside a For/While loop
sadams54 said
You will also find that all the weird things you do in VB are not necessary in gambas.
The best thing you can do is go ahead and cut and paste your code and see what errors you get. Then ask about the specifics. I converted much of my stuff from VB to gambas and found I made significant changes mostly due to gambas being easier. That caused me to overthink many things and after getting my head out of MS VB and into the more simple gambas I was fine. Try the cut and paste and come back with just the problems and you will get help. I will try to keep a watch.
All that's true though
A VB to Gambas converter been asked for many times. It's just not possible.
THE ONLY way is to learn Gambas, once you know how to code with Gambas then converting VB code yourself is easy, albeit time consuming
looks like you are correct I was wrong about the DIM. this was something that was true in earlier versions as I ran into it trying to conditionally dim things like in VB. I never checked to see if it changed and it has. Although not usually a good practice to do that.
Posted
Enthusiast

AndyGable said
What I want to do is create a function that would upload a black and white image to a Epson, NCR, IBM printer (each uses differ ESC/PoS codes but the conversion is the same)
And I am also trying to convert a DLL that add promotions support to my EPoS application in windows.
I sent you a private message but cogier knows his stuff well and has helped me before also. Printing in gambas is a little less than intuitive. But it is very good and stable. I already wrote a nice POS program in gambas and it may be something you can modify for your use. Attempting to print with esc codes may not be necessary.
1 guest and 0 members have just viewed this.



