Page 1 of 1

Value conversion

Posted: Mon Sep 29, 2008 9:29 pm
by EdzUp[SD]
I have some code (coverted from another language to convert from long ints to strings and back again (also floats) ), in the other language the value comes through perfectly but in PB the 0 values get stripped is there any way to stop it from doing that.

Heres the code:

Code: Select all

;-------------------------------------------------------------------------------------------------------------------
Procedure.f StrToFloat( Value.s )
  ;converts a string (4 bytes ) to float
  Static ReturnValue.f = 0
  
  PokeS( *ConversionBuffer, Value, 4 )
  ReturnValue = PeekF( *ConversionBuffer )
  FreeMemory( *ConversionBuffer )
  
  ProcedureReturn ReturnValue
EndProcedure

;-------------------------------------------------------------------------------------------------------------------
Procedure.s FloatToStr( Value.f )
  ;converts a float to a string
  PokeF( *ConversionBuffer, Value )
  
  ProcedureReturn PeekS( *ConversionBuffer, 4 )
EndProcedure

;-------------------------------------------------------------------------------------------------------------------
Procedure.l StrToInt( Value.s )
  ;converts from String (4 byte) to Long
  PokeS( *ConversionBuffer, Value, 4 )
  
  ProcedureReturn PeekL( *ConversionBuffer )
EndProcedure

;-------------------------------------------------------------------------------------------------------------------
Procedure.s IntToStr( Value.l )
  ;converts from Long to String

  PokeL( *ConversionBuffer, Value )
  
  ProcedureReturn PeekS( *ConversionBuffer, 4 )  
EndProcedure

Posted: Mon Sep 29, 2008 9:59 pm
by Fred
Did you look at Val/ValF() and Str/StrF() ?

Posted: Tue Sep 30, 2008 11:38 am
by EdzUp[SD]
Had a look and from what I can gather they convert to string, binary or hex representations not the byte representation of the value so 0 would become '00000000' etc. What I need is the byte representation IE 0 would be four chr( 0 )'s. Normally poking and peeking it into a memory address would return the value required but it doesnt seem to be the case.

Posted: Tue Aug 04, 2009 12:01 am
by Fzarada
Is there a fix yet for this problem?
Sometimes is necessary to not have the zeros stripped from a Hex value. working on nibbles level is an example.

So, is there a way to have (let's say) Hex(10) returns 0A instead of just A.
the parametre #PB_Byte should've take care of that at least, but it doesn't :cry:

Posted: Tue Aug 04, 2009 12:08 am
by Henrik
Hi Welcome
Didn't read the Tread, just your question

From the help file
Debug RSet(Hex(12), 4, "0")

Code: Select all


Debug RSet(Hex(10), 2, "0")


Best Regrads Henrik

Posted: Tue Aug 04, 2009 10:24 am
by Fzarada
Thanks Henrik,
You're right, but i just realize i didn't point at the real problem:
actually the problem is not with Hexadecimal Strings but it is with Hexadecimal numbers.
My example of Hex(10) isn't a good neither the right one cause it returns a string not a number, sorry about that.
To reformulate the question: Is there a way in PB to have

Code: Select all

debug $B4 ! $B1
return 05 instead of just 5 ?

Posted: Tue Aug 04, 2009 11:05 am
by Kaeru Gaman
to be exact, it is IMPOSSIBLE to display any number at all.
you are ALWAYS displaying strings.
if you send a 65 to a display, you will see an A.

even when you write

Code: Select all

debug $B4 ! $B1
, there is an auto conversion number-to-string implemented.

if you use auto-conversion, there will be no leading zeros included, and aditionally, the result is displayed in decimal.
the 5 you see is no hexadecimal numerical string, but a decimal numerical string.

so, if you want to display some HEX, you MUST convert it manually.


PS:
this is no PureBasic limitation, this is a general limitation for ALL programming languages.
some languages have more comprehensive auto conversions, others have none at all.
every text display device displays strings, descripted by ASCII or Unicode or EBCDIC encoded numerical Arrays.

Posted: Tue Aug 04, 2009 11:35 am
by Demivec
Fzarada wrote:Thanks Henrik,
You're right, but i just realize i didn't point at the real problem:
actually the problem is not with Hexadecimal Strings but it is with Hexadecimal numbers.
My example of Hex(10) isn't a good neither the right one cause it returns a string not a number, sorry about that.
To reformulate the question: Is there a way in PB to have

Code: Select all

debug $B4 ! $B1
return 05 instead of just 5 ?
It's the same answer:

Code: Select all

Debug RSet(Hex($B4 ! $B1), 2, "0")