Author Topic: looking for a function to convert Hex to numbers  (Read 2591 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
looking for a function to convert Hex to numbers
« on: March 14, 2018, 12:39:15 PM »
Hello

is there a function that can convert Hex to ordinary numbers?

for example, i have these HEX     A0DD7CBFF9FED839      and   5E65A486   
and want them to convert back to ordinary numbers.

appreciate any help

chrisc

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #1 on: March 14, 2018, 04:54:57 PM »
you see in PB, they have a function  VAL() 
which can do this conversion from Hex to ordinary numbers

for example,  VAL("&H0F5F3")      ' it will  returns 62963

i wonder if there is an equivalent function in O2 ?

JRS

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #2 on: March 14, 2018, 05:41:26 PM »
I created a couple functions for Script BASIC to emulate the Business BASIC HTA() and ATH() HEX related functions.

Code: Script BASIC
  1. ' ASCII2HEX - HEX2ASCII
  2.  
  3. ' BB_HTA - Business BASIC HTA() function
  4. '
  5. ' Returns the hexadecimal text string of the passed argument string.
  6. '
  7. FUNCTION BB_HTA(AsciiStr)
  8.   LOCAL AsciiLen,ScanPos,HexStr
  9.   AsciiLen = LEN(AsciiStr)
  10.   IF AsciiLen THEN
  11.     FOR ScanPos = 1 TO AsciiLen
  12.       HexStr &= RIGHT("0" & HEX(ASC(MID(AsciiStr, ScanPos, 1))),2)
  13.     NEXT ScanPos
  14.   ELSE
  15.     HexStr = ""
  16.   END IF
  17.   BB_HTA = HexStr
  18. END FUNCTION
  19.  
  20. ' BB_ATH - Business BASIC ATH() function
  21. '
  22. ' Converts a text string of hex character pairs to ASCII values.
  23. '
  24. FUNCTION BB_ATH(HexStr)
  25.  
  26.   LOCAL LenHex, AsciiStr, HexTable, ScanPos, HiByte, LowByte
  27.   LenHex = LEN(HexStr)
  28.   IF LenHex % 2 = 0 THEN
  29.     HexTable = "0123456789ABCDEF"
  30.     FOR ScanPos = 1 TO LenHex STEP 2
  31.       HiByte = INSTR(HexTable,UCASE(MID(HexStr, ScanPos, 1))) - 1
  32.       LowByte = INSTR(HexTable,UCASE(MID(HexStr, ScanPos + 1, 1))) - 1
  33.       IF ISINTEGER(HiByte) AND ISINTEGER(LowByte) THEN
  34.         AsciiStr &= CHR(HiByte * 16 + LowByte)
  35.       ELSE
  36.         AsciiStr = ""
  37.         GOTO Exit_For
  38.       END IF
  39.     NEXT ScanPos
  40.     Exit_For:
  41.   ELSE
  42.     AsciiStr = ""
  43.   END IF
  44.   BB_ATH = AsciiStr
  45.  
  46. END FUNCTION
  47.  
  48. hexstr = BB_HTA("123")
  49. PRINT hexstr,"\n"
  50. PRINT BB_ATH(hexstr),"\n"
  51.  


jrs@jrs-laptop:~/sb/examples/test$ scriba hextest.sb
313233
123
jrs@jrs-laptop:~/sb/examples/test$


Charles Pegge

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #3 on: March 14, 2018, 09:47:03 PM »
Hi Chris,

O2 also has val(), which can handle up to 8-digit hexadecimal strings.

If your number is up to 8 digits (32bit) literal then you can handle it directly

print &H0F5F3

but 64bit literal and string numbers require extra attention...

I'm working on it  ::)


Charles Pegge

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #4 on: March 15, 2018, 04:56:49 AM »
The best solution for handling 64bit literals, I think, is to make use of msvcrt (c runtime) functions.

sscanf takes the role of val
sprintf takes the role of str$, hex$, and format$

Code: [Select]
'2018-03-15 T 10:03:22
'msvcrt val/str
'http://www.cplusplus.com/reference/cstdio/scanf/
'http://www.cplusplus.com/reference/cstdio/printf/
% filename "t.exe"
use rtl64
use msvcrt
'
quad a
string idata="0xA0DD7CBFF9FED839"
string odata=nuls 32
'
sscanf(idata,"%llx",@a) 'hexadecimal input to 64bit
sprintf(odata,"%llu",a)'64bit unsigned decimal output
print idata chr(13,10) odata

ps: this will also work with 32 bit compiling, with a slight alteration:
quad a
« Last Edit: March 15, 2018, 05:05:49 AM by Charles Pegge »

chrisc

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #5 on: March 15, 2018, 05:02:27 AM »
Thanxx a lot Charles  :)

i will test it,  O2 is really good as it can use c functions and i 'm new to c

chrisc

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #6 on: March 15, 2018, 06:04:21 AM »
Well done Charles
i had changed your code to take in the smaller  hex 5E65A486

Code: [Select]
'http://www.oxygenbasic.org/forum/index.php?topic=1579.0
'msvcrt val/str
'   sscanf takes the role of val
'   sprintf takes the role of str$, hex$, and format$
'http://www.cplusplus.com/reference/cstdio/scanf/
'http://www.cplusplus.com/reference/cstdio/printf/

% filename "Hex2Dec_64.exe"
use rtl64
use msvcrt
'
quad anum
' input Hex string
string idata=    "0x5E65A486"                 '    "0xA0DD7CBFF9FED839"
' Output decimal string
string odata=nuls 32


'hexadecimal input to 64bit
' convert the Hex string idata into a  number anum
sscanf(idata,"%llx",@anum)


'64bit unsigned decimal output
' format the anum into an output string odata
sprintf(odata,"%llu",anum)

' the results
print    " input Hex : " +     idata chr(13,10)   " Output number " + odata




and match the results with the online hex to decimal converter at

https://www.binaryhexconverter.com/hex-to-decimal-converter


the result is correct except that the internet link above couldn't handle the large Hex A0DD7CBFF9FED839



chrisc

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #7 on: March 15, 2018, 06:15:12 AM »
That's how good the comparison is  :)

JRS

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #8 on: March 15, 2018, 06:21:59 PM »
I fired up the Windows 7 64 bit partition on my old laptop and thought I would give Script BASIC 64 bit a try with it's HEX function.

Code: Script BASIC
  1. PRINT HEX(1234567890),"\n"
  2. PRINT 0x499602d2,"\n"
  3.  


C:\sb22_64\test>scriba hexnum.sb
499602D2
1234567890

C:\sb22_64\test>


The same results on Linux 64 bit.


jrs@jrs-laptop:~/sb/examples/test$ scriba hexnum.sb
499602D2
1234567890
jrs@jrs-laptop:~/sb/examples/test$


Script BASIC returned -6855185893813200839 for your A0DD7CBFF9FED839 HEX number.

The number returned from an online converter was 11591558179896350000.
« Last Edit: March 15, 2018, 06:49:41 PM by John »

Charles Pegge

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #9 on: March 15, 2018, 09:35:25 PM »
Hi John,
You can verify the decimal number by turning it back into hex.

Mike Lobanovsky

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #10 on: March 16, 2018, 12:29:35 AM »
John,

Hex numbers are unsigned integers in strongly typed languages. SB's untyped engine doesn't support unsigned integers and substitutes them with signed longs or signed longlongs depending on SB bitness. Hence the minus sign you get when printing the conversion results. It means the given hex value is too large to fit into the signed long/longlong's range of allowable positive values. Unsigned int's positive range is twice larger but its negative range is entirely non-existent.

JRS

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #11 on: March 16, 2018, 02:55:48 AM »
Hi John,
You can verify the decimal number by turning it back into hex.

Code: Script BASIC
  1. PRINT HEX(-6855185893813200839),"\n"
  2.  


jrs@jrs-laptop:~/sb/examples/test$ scriba num2hex.sb
        F9FED800
jrs@jrs-laptop:~/sb/examples/test$


Mike Lobanovsky

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #12 on: March 16, 2018, 03:18:15 AM »
John,

There may be some deficiencies in migrating SB to 64 bits on both platforms. It looks like its current HEX() and/or PRINT implementations aren't able to cope with 64-bit interpretation of longlong hex values correctly. Or like SB isn't aware of longlong/ulonglong ranges in either bitness and platform implementation altogether.

JRS

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #13 on: March 16, 2018, 03:28:56 AM »
I agree that SB has issues with big numbers and it's 64 bit PRINT function. I noticed it long ago with FORMAT() returning funky results.

Charles Pegge

  • Guest
Re: looking for a function to convert Hex to numbers
« Reply #14 on: March 16, 2018, 04:30:52 AM »
John, I checked your decimal number, it matches the sprintf output, for signed integers.

sprintf(odata,"%lli",a)'64bit signed decimal output