July 04, 2024, 04:46:45 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Value to String

Started by Andy, March 27, 2014, 06:53:16 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

Thought I could go back to sleep and stop bothering everyone but I've come across this problem:

I was using ckoehn's example to convert Hex to Decimal - which works great, however there is a problem
using the STR$ function.

If I convert a hex value of "7FFFFFFF" (2147483647) to decimal and the use the STR$ command to convert the decimal conversion into a string - it's ok i.e. the string displays / holds the correct value.

If I go any higher e.g.  "8FFFFFFF" or  "9FFFFFFF" (higher than 7FFFFFFF) the STR$ function returns
negative numbers.

I need the result to be a string because it has to be added to a listbox.

Anyone come across this before? and how can you get around this?

Can't find the answer on the forum and can't see an answer in the help file.

Thanks,
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

GWS

Tomorrow may be too late ..

LarryMc


Thanks Graham, I had forgotten about that one.
That one was to handle up to 64 bit

Here's one that I just did for 32 bit
I suspect I could modify it to do 64 bit(another day, maybe)
DECLARE CDECL EXTERN _sprintf(buffer as string,format as STRING,...),INT '<===============

OPENCONSOLE
double myres = Hex2Dec("9FFFFFFF") 'is ok '<===============
PRINT hex$(myres) '<===============
string buffer '<===============
_sprintf(buffer," %.*f",0,myres) '<===============
PRINT buffer '<===============
DO:UNTIL INKEY$ <> ""
END

SUB Hex2Dec(STRING txt),double '<===============
INT i
double pow,res '<===============
res=0
FOR i=1 to LEN(txt)
pow=16^(LEN(txt)-i)
res+=(INSTR("0123456789ABCDEF",MID$(txt,i,1),1)-1)*pow
NEXT i
RETURN res
ENDSUB


LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Andy

Hi Larry,

Thanks for the modification - that works for values up to and including 0xFFFFFFFF

Also, another thanks because it also gave me a way to add / edit / delete Qwords as well as Dwords in the
registry (not that I every use Qwords).

I did notice one thing though (but it doesn't effect me) the Hex2Dec routine doesn't quite do it for a very large number i.e. 0xFFFFFFFFFFFFFFFF (16 F's), there is a discrepancy between the value returned and the value shown in Regedit.

Like I say, it doesn't effect me too much but it may do with someone else.

Anyway,

Thats great - thanks again!
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

Quote from: andy1966 on March 28, 2014, 08:02:07 AM
I did notice one thing though (but it doesn't effect me) the Hex2Dec routine doesn't quite do it for a very large number i.e. 0xFFFFFFFFFFFFFFFF (16 F's), there is a discrepancy between the value returned and the value shown in Regedit.
that's what I said
DOUBLE is 32 bit
16 F's is 64 bit so the appropriate variables should be changed to INT64 or UINT64
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library