Qbasicnews.com

Full Version: Need help wit LONG hex number conversion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
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.
*****
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.

Anonymous

are you absolutely restricted from using FreeBASIC?


btw this is hardly a "newb" question, heh... ;p
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.
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
Smile

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


( program outputs something like:
Quote:60D20C00E50100007F1B6305
011000001101001000001100000000001110010100000001000000000000000001111111000110110110001100000101
)
: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)
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!
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. Smile
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.
Pages: 1 2