Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Convert number to Hex value and back?

Author
Message
Hubdule
22
Years of Service
User Offline
Joined: 3rd Sep 2002
Location: Gundelsheim
Posted: 27th Oct 2011 14:11
Hi

I've a question about how to convert a hex string back to integer in AppGameKit?

I use HEX(number) to get a hex string from a given number. But how do I convert it back? Is there a command for that? Or do I've to recalculate it the long way back?

Hubdule
22
Years of Service
User Offline
Joined: 3rd Sep 2002
Location: Gundelsheim
Posted: 27th Oct 2011 14:25
Here's what I use at the moment. What do you think, is it correct?



JavierTF
14
Years of Service
User Offline
Joined: 15th Feb 2010
Location: Going out, to the right
Posted: 27th Oct 2011 17:38 Edited at: 27th Oct 2011 17:53
Hi

Take x numbers:

example1: AF1

then

AF1 = (A * 16^2) + (F*16^1) + 1 = 2801 (dec)

example2: AAF1

then

AAF1 = (A * 16^3) + (A * 16^2) + (F*16^1) + 1 = 2801 (dec)


- Get the place of the digit. May be a For?

- (If) it's a letter, (then) translate it to it's corresponding value (else) calculate the number.

- Apply the formula.

See you.
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 28th Oct 2011 04:56
All you need to do is grab the character and convert it ASCII.. Which gets rid of the string thrashing from the original loop.

(PB code.. but the principal is the same, will need re-ordering for the most optimal loop)



Hubdule
22
Years of Service
User Offline
Joined: 3rd Sep 2002
Location: Gundelsheim
Posted: 28th Oct 2011 09:38
Hi Kevin, thanks, that loop looks quite optimized. So I'll use this one now. Thanks again!

Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 28th Oct 2011 18:10 Edited at: 28th Oct 2011 18:31
This from the help on Val(), implying that you can just do the conversion with one command.

BIG EDIT - this command works just as expected. In fact, it is not limited to hex, binary, octal or decimal. Try it in base 5, or whatever. Neat.



Quote: "
Val
Description
Converts a string into an integer value. For example Val("10") would return the value 10.

Definition
integer Val( str )

int agk::Val( const char* str )

integer Val( str, base )

int agk::Val( const char* str, int base )

Parameters
•str - The string to convert to an integer.
•base - The base that the string number is in.


"


Hubdule
22
Years of Service
User Offline
Joined: 3rd Sep 2002
Location: Gundelsheim
Posted: 28th Oct 2011 19:08
Nice! Thank you. I saw the "base" in the VAL description but couldn't understand what the base is for Should be included in the manual.

Login to post a reply

Server time is: 2024-11-22 10:15:05
Your offset time is: 2024-11-22 10:15:05