Qbasicnews.com
February 28, 2020, 06:44:03 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Need help wit LONG hex number conversion  (Read 3552 times)
Bornloser
New Member

Posts: 4


« on: April 16, 2005, 12:34:37 AM »

I am not NEW to Quickbasic, nor am I an expert, but I have a problem I need help with.  I need to convert a 26 digit decimal number to binary, and then (after changing the binary slightly) change it to hex.  Everything I have tried end up in the fact that the number is too long even with the DEFDBL and DEFLNG commands, and even an overflow error with the HEX$ command.  Any Gurus out there have any handy  little program I can implement into mine to do the job?
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #1 on: April 16, 2005, 02:12:46 AM »

Very interesting problem, BL. However, it sounds like a homework assignment, which we are not allowed to help with. So, honestly, tell us if it's homework or not.
*****
Logged
Bornloser
New Member

Posts: 4


« Reply #2 on: April 16, 2005, 02:43:31 AM »

I am 51 yrs old, no its not homework. It IS FOR work, though. I have the rest of the program written, but got stumped at the sheer number of numbers....it is for a 96 bit RFID tag, but the first bits are constant, so no problem there.  
Any help here GREATLY appreciated..it has been a LONG time since college (where I learned Fortran, Basic, And Cobol), but I am limited to QBasic for this project.  I am NOT a programmer (obviously), but i WAS assigned this project.
Logged
Anonymous
Guest
« Reply #3 on: April 16, 2005, 05:48:54 AM »

are you absolutely restricted from using FreeBASIC?


btw this is hardly a "newb" question, heh... ;p
Logged
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #4 on: April 16, 2005, 06:34:25 AM »

Chaos: This thread is for grey bearded adults only!!!  :rotfl:  :rotfl:  :rotfl:  

If speed is not an issue, find Neo's BIGINT library here
http://www.petesqbsite.com/downloads/libraries.shtml
It uses strings to deal with integers of any size.
Neo is a regular of this forum so yo can PM him if you have problems.
Logged

Antoni
Neo
Na_th_an
*****
Posts: 2150



« Reply #5 on: April 16, 2005, 06:43:12 AM »

I have no idea how the RFID 12-byte type is stored in memory, but you can at least print out this memory this RFID tag contains.
Note that a RFID tag size corresponds with a type of 3 SINGLEs/LONGs, that's why I'm using that type here. In your own program, you need to store the RFID tag somewhere in memory, or convert it to an array of 3 SINGLEs/LONGs for this to work.

[syntax="QBasic"]'$DYNAMIC
DEFINT A-Z

' just my definition of rfid, no relevancy
TYPE RFIDpseudo
   Bit95to64 AS LONG
   Bit63To32 AS LONG
   Bit31To0 AS LONG
END TYPE

' make a rfid
DIM RFID AS RFIDpseudo
' fill with some test values
RFID.Bit95To64 = 840288&
RFID.Bit63To32 = 485&
RFID.Bit31TO0 = 90381183&

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'@ Relevant part starts here
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

' You have a RFID structure somewhere in memory
' retrieve the memory location if you didn't do this yet
' also possible to have this structure or memory passed to this program
' if you adapt the code a bit to support this
MSegment = VARSEG(RFID)
MOffset = VARPTR(RFID)
DEF SEG = MSegment

' Read out the 12 bytes and get these in hex
MyHex$ = ""
FOR I = 0 TO 11
   Temporary$ = "00" + HEX$(PEEK(MOffset + I))
   MyHex$ = MyHex$ + RIGHT$(Temporary$, 2)
NEXT I

' print out this hex string
PRINT MyHex$

' now it's easy to convert this hex string to binary
MyBin$ = ""
FOR I = 0 TO 11
  Subchar = VAL("&H" + MID$(MyHex$, I * 2 + 1, 2))
  FOR J = 7 TO 0 STEP -1
     IF (Subchar AND (2 ^ J)) > 0 THEN MyBin$ = MyBin$ + "1" ELSE MyBin$ = MyBin$ + "0"
  NEXT J
NEXT I

' print out this binary string
PRINT MyBin$[/syntax]

In fact, if you have the memory location, or a wrapped-around structure of the same size of the RFID tag (don't forget to convert this RFID to the structure appropriately. As I don't know about RFID I can't do this), a lot can be done.

Hope I was of any help...

Neo
Smiley

Note: Rebuild this code for the correct endian system... =)


( program outputs something like:
Quote from: "output"
60D20C00E50100007F1B6305
011000001101001000001100000000001110010100000001000000000000000001111111000110110110001100000101

)
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #6 on: April 16, 2005, 06:51:10 AM »

:lol: Antoni again! I was busy with that post for 15 minutes so you posted first Wink

Anyway, BIGINT can be used too as you said, although it might be harder then to convert to hexadecimal (since BIGINT stores numbers alphanumerically and not bitwise).

Thanks for recommending it though. Really appreciate that ^_^


(btw did you imply I'm a grey bearded adult? uhhh lol Tongue)
Logged
Bornloser
New Member

Posts: 4


« Reply #7 on: April 16, 2005, 11:35:54 AM »

WOW Lots of help at this site!
No, I  AM restricted to what the company has in its computer, so quickbasic is it.
The rfid IS a 96 bit binary digit, so it looks like the program here should work.  The boss just needs the data developed, the actual writing of the tag comes a bit later (we as a company are just getting started in the rfid mess.  He assigned me to let him input the data (6 fields of assorted lengths) on a screen ant the rfid tag data be displayed back to him in both binary and hex.  The binary was no problem, but the conversion to HEX had me stumped.  It would have been EASY to convert each seqment to hex, but to convert the final 96 bit to hex as a whole was beyond me!!!
I am headed out for the day, but will be back tonite to check all the help out.  I will post back.
Again, thanks for all the help!
Logged
adosorken
*/-\*
*****
Posts: 3655



WWW
« Reply #8 on: April 16, 2005, 01:40:44 PM »

Another point of help: unless your company is still stuck in DOS, you might want to see if you can get some policies changed. Technology advances and companies that want to stay competitive have to change with it. Part of our own advancement here at QBN has been the embracing of FB over QB. Your company needs to upgrade. Smiley
Logged

I'd knock on wood, but my desk is particle board.
Ralph
Ancient Guru
****
Posts: 544


« Reply #9 on: April 16, 2005, 11:13:24 PM »

How about the calculator that comes with Windows?  I believe it's "shortcut" is in the Accessories folder, under the name of "Calculator".  The actual program is called "calc.exe".  In the "Scientific" mode, you can enter HUGE numbers in either decimal, hexadecimal or binary, and just toggle from any one to any other one.  It's easier than falling off of the traditianal log.  Try it, and let us know if this gets you to become the "Genius" in your office.
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Bornloser
New Member

Posts: 4


« Reply #10 on: April 17, 2005, 03:19:02 AM »

Neo:
I just got back in and was able to look at the program.  Maybe I am wrong, but it looks like the OPPOSITE of what I need.  I ALREADY have the 96 bit BINARY rfid number, what I need is to convert the 96 BINARY that I have created in my own program into hex to be used in a seperate routine.
If it helps, the original I created is a 96 bit binary, however, the 36 most significant bits are not changed, so I CAN get by with just the rightmost 58 digits being converted to hex
Ralph:
 As for using the windows calculator, it will convert a number from one base to another, but it wont create the 96 bit binary from the required fields.  Again, I have already written the program to generate the binary, I need to convert the binary to hex, and have it incorporated into the existing program.
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #11 on: April 17, 2005, 06:17:20 AM »

If you have a binary string of any length (multiple of 8, which indicates a round number of bytes), you can do the following.

Code:
' This is an example of a 96-bit binary string
MyBinaryString$ = "011000001101001000001100000000001110010100000001000000000000000001111111000110110110001100000101"

' Empty hex string
MyHexString$ = ""

' Loop all bytes of it
For byteLoop = 0 To (Len(MyBinaryString$) - 1) Step 8
  ' new variable
  TempStorage% = 0
  ' loop all 8 fields
  For fieldLoop = 7 To 0 Step -1
    TempStorage% = TempStorage% OR ((2 ^ (fieldLoop)) * VAL(MID$(MyBinaryString$, byteLoop + 8 - fieldLoop, 1)))
  Next fieldLoop
  ' attach to the hex string
  MyHexString$ = MyHexString$ + RIGHT$("00" + HEX$(TempStorage%), 2)
Next byteLoop

' print out the hex string
PRINT MyHexString$


It isn't the fastest code existing, but it works.
Program output:
Quote from: "Output"
60D20C00E50100007F1B6305

Which is exactly what the previous program outputted.

Hope this helped some more =),

Neo
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #12 on: April 17, 2005, 01:14:53 PM »

BornLoser:
Sorry, after going back and trying the Windows Calculator, I see that it is limited to 16 digits in Decimal notation, so, it's fine only for integer up to that size only, and, as you pointed out, no good for your problem.

So, I surfed the Internet, as I knew there was a good calculator for large numbers, and, I found one!  I tried it with 39 decimal intgers, converted to binary, to hex, and back, all worked fine!  

The program in called BigCalc.exe, is only 600+ KB, and can be downloaded from:

http://www.bigcalc.scriptmania.com/

Does this help?
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!