Hi
Here we go again!!..
First of all, the code.
Below the subs..
Sub Hexa2Decimal (sNumber As String) As Int
Dim i, k As Int = 0
Dim iSuma As Int = 0
Dim sCar As String
Dim iBase As Int = 1
If sNumber.Contains("h") Then
sNumber = sNumber.SubString2(0,sNumber.Length-1)
End If
If sNumber.Contains("0x") Then
sNumber = sNumber.SubString(3) 'will be #~2
End If
For i = sNumber.Length -1 To 0 Step -1
sCar = sNumber.CharAt(i)
Select sCar
Case "A", "a"
sCar = "10"
Case "B", "b"
sCar = "11"
Case "C", "c"
sCar = "12"
Case "D", "d"
sCar = "13"
Case "E", "e"
sCar = "14"
Case "F", "f"
sCar = "15"
End Select
'If k = 0 Then iBase = 1
If k = 1 Then iBase = 16
If k = 2 Then iBase = 256
If k = 3 Then iBase = 4096
If k = 4 Then iBase = 65536
If k = 5 Then iBase = 1048576
iSuma = iSuma + (sCar * iBase)
k = k + 1
Next
Return iSuma
End Sub
Sub PeterSimpsonHexa2Decimal(sNumber As String) As Int
Dim BC As ByteConverter
'Log($"Value ${BC.IntsFromBytes(BC.HexToBytes(sNumber.Replace("0x", "00000")))(0)}"$) ' 1046
Return BC.IntsFromBytes(BC.HexToBytes(sNumber.Replace("0x", "00000")))(0)
End Sub
Sub MaharesHexa2Decimal(sNumber As String) As Int
'Dim strCC As String="0x416"
Dim intCC As Int = Bit.ParseInt(sNumber.Replace("0x",""), 16)
Return intCC
End Sub
'endless...
Sub JAVAHexa2Decimal(sNumber As String) As Int
Return jo.RunMethod("hex2decimal",Array(sNumber))
End Sub
private Sub jo As JavaObject
Return Me
End Sub
#if java
import java.util.Scanner;
public static int hex2decimal(String s)
{
String digits = "0123456789ABCDEF";
int val = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
#End If
Sub from
@Peter Simpson Fails in this line
BC.IntsFromBytes(BC.HexToBytes(sNumber.Replace("0x", "00000")))(0)
Log Says
main_petersimpsonhexa2decimal (B4A line: 4846)
java.lang.Exception: hex string has odd number of characters
at anywheresoftware.b4a.agraham.byteconverter.ByteConverter.HexToBytes(ByteConverter.java:254)
at com.dbsaiz.caja.venta.free.main._petersimpsonhexa2decimal(main.java:9884)
at com.dbsaiz.caja.venta.free.main._btnpnlprinter_click(main.java:2986)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:4787)
at android.view.View$PerformClick.run(View.java:19873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Sub from
@Mahares fails in this line
Dim intCC As Int = Bit.ParseInt(sNumber.Replace("0x",""), 16)
log Says
main_mahareshexa2decimal (B4A line: 4855)
Dim intCC As Int = Bit.ParseInt(sNumber.Replace("
java.lang.NumberFormatException: Invalid int: " 416"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at anywheresoftware.b4a.keywords.Bit.ParseInt(Bit.java:86)
at com.dbsaiz.caja.venta.free.main._mahareshexa2decimal(main.java:9712)
at com.dbsaiz.caja.venta.free.main._btnpnlprinter_click(main.java:2986)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:4787)
at android.view.View$PerformClick.run(View.java:19873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Zyg
Java code also fails
log says
JAVAHexaToDecimal Sub
---------------------
Hexadecimal: 0x416 ---------- Decimal: -1051626
0k... I am going to do...
of the variable sNumber (holds the number)
Log says
PeterSimpsonHexaToDecimal Sub
----------------------------
Hexadecimal: 0x416 ---------- Decimal: 1046
----------------------------------------------------------------------
MaharesHexaToDecimal Sub
----------------------
Hexadecimal: 0x416 ---------- Decimal: 1046
----------------------------------------------------------------------
JAVAHexaToDecimal Sub
---------------------
Hexadecimal: 0x416 ---------- Decimal: -3050
----------------------------------------------------------------------
Dadaista Sub
----------------------------
Hexadecimal: 16 ---------- Decimal: 22
My sub returns an invalid decimal.... as
@Mahares says... the line
sNumber = sNumber.SubString(3)
is wrong.... I think Anybody can see it and that is the problem... data is wrong... I am very stupid.
I modify my sub... change #2 for #3 and log says
PeterSimpsonHexaToDecimal Sub
────────────────────────────
Hexadecimal: 0x416 ---------- Decimal: 1046
══════════════════════════════════════════════════════════════════════
MaharesHexaToDecimal Sub
──────────────────────
Hexadecimal: 0x416 ---------- Decimal: 1046
══════════════════════════════════════════════════════════════════════
JAVAHexaToDecimal Sub
─────────────────────
Hexadecimal: 0x416 ---------- Decimal: -3050
══════════════════════════════════════════════════════════════════════
Dadaista Sub
────────────────────────────
Hexadecimal: 416 ---------- Decimal: 1046
══════════════════════════════════════════════════════════════════════
I do not know why the JAVAHexaToDecimal Sub, returns wrong number
The problem was that I could not see the space between ":" and "0x" in this line from text file
Below I Got the line from text file
If sLinea.Contains("ProductId") Then
sP = sLinea.SubString(sLinea.IndexOf(":")+1)
End If
I did not check the spaces.... When I made the sub... the line
sNumber = sNumber.SubString(3)
... smells....
"0x416" returns "0x4"... if "0x" is returned, something goes wrong
...
I am very stupid. The problem came from source data
I am very grateful for all members interesated in my problem.
Thank all members for your time!!
it was a silly problem.
Regards all of you!!!!!!
Wow.... saturday night and I am writting post in forum... Something goes wrong...