Value conversion

Just starting out? Need help? Post your questions and find answers here.
EdzUp[SD]
Enthusiast
Enthusiast
Posts: 104
Joined: Thu Jun 26, 2008 10:53 pm
Location: Banstead, UK

Value conversion

Post 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
Fred
Administrator
Administrator
Posts: 18360
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Did you look at Val/ValF() and Str/StrF() ?
EdzUp[SD]
Enthusiast
Enthusiast
Posts: 104
Joined: Thu Jun 26, 2008 10:53 pm
Location: Banstead, UK

Post 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.
Fzarada
New User
New User
Posts: 5
Joined: Mon Aug 03, 2009 11:46 pm

Post 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:
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post 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
Fzarada
New User
New User
Posts: 5
Joined: Mon Aug 03, 2009 11:46 pm

Post 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 ?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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")
Post Reply