Author Topic: hexadecimal conversion  (Read 8045 times)

0 Members and 2 Guests are viewing this topic.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
hexadecimal conversion
« on: August 17, 2010, 11:47:11 pm »
I'm currently working on rewriting Quigibo's HEXPIC sprite editor to work in Axe. The whole thing simplifies a lot due to the added graphics features of Axe over TI-BASIC. However, as far as I can tell, Axe has next to no string support.

The program should work as follows:

  • The user first draws a picture on an 8x8 grid using what are essentially magnified pixels.
  • After the picture is drawn, the program goes through each of the grid cells and determines whether they are on(1) or off(0).
  • This series of binary strings is then converted into a series of hexadecimal numbers, using the first and second four number string of each row. For example, given the first row 0 0 0 1 1 1 1 1, the program should output the hex string 1F
  • This list is then stored to the Ans variable for later recall by the user

The problem is that I can't figure out how to store anything to the strings. In BASIC, all you would do is
Code: [Select]
"0"->Str1
For(A,0,7
1->X
Y+offset->Y
For(B,0,7
X+offset->X
If pxl-Test(X,Y:Then
"1"+Str1->Str1
Else
"0"+Str1->Str1
End
End
End
sub(Str1,2,length(Str1->Str1

Then you simply convert Str1 to Hex, which is also easy in BASIC.

But would anyone know how you might implement something similar in Axe?
« Last Edit: August 17, 2010, 11:47:48 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: hexadecimal conversion
« Reply #1 on: August 18, 2010, 12:16:14 am »
axe has loads of support for strings. you can load strings. you can manipulate their data directly. you can even export them to OS variable strings! what other support were you looking for?

lastly, here's some code. assume the x and y position of the sprite is 0.
Code: [Select]
GetCalc("Str1",16)->I       .I points to the start of the data at Str1
For(P,0,15      .16 half-bytes
0->C   .Counter
For(T,0,3       .4 pixels per half byte.
e^(3-T)*pxl-Test(P^2*4+T,P/2)+C->C
End
{"0123456789ABCDEF"+C}->{I+P}  .store corresponding hex character into
End

i think this is the most optimized way, though i'm not sure. your sprite data is in str1.

an extremely optimized version in BASIC by yours truly can be found in this thread.
« Last Edit: August 18, 2010, 12:18:06 am by nemo »


Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: hexadecimal conversion
« Reply #2 on: August 18, 2010, 12:22:00 am »
Well I don't know if this helps any right now but a better way in basic to do this is:

Code: [Select]
"_?Str2
For(A,0,7
For(B,0,7,4
8pxl-Test(A,B)+4pxl-Test(A,B+1)+2pxl-Test(A,B+2)+pxl-Test(A,B+3
Str2+sub("0123456789ABCDEF",Ans+1,1?Str2
End
End
sub(Ans,2,16?Str2
Disp Ans

That's what I use, or will use, in my Sprite Maker in BASIC.

And knowing that nemo is watching this I'm gonna get ninja'd big time :P
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: hexadecimal conversion
« Reply #3 on: August 18, 2010, 12:26:46 am »
got you there, meishe (:

by the way, the BASIC version i linked to probably shouldn't be used if speed is important. it's slow. i think i timed it at 3 seconds, and it's about 92 bytes. then i clocked one that was about 120 bytes but it was twice as fast.


Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: hexadecimal conversion
« Reply #4 on: August 18, 2010, 12:28:23 am »
I had no idea you could manipulate strings like that in Axe. Hope you don't mind if I throw that bit into the code, nemo, once I finish up the last few editing features :)
« Last Edit: August 18, 2010, 12:31:06 am by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: hexadecimal conversion
« Reply #5 on: August 18, 2010, 12:33:01 am »
yeah, you can do that same thing with any data, aka map data, sprite data, etc. in axe, they're all just numbers.

go for it. by the way, what features are you looking to implement?


Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: hexadecimal conversion
« Reply #6 on: August 18, 2010, 12:45:23 am »
got you there, meishe (:

by the way, the BASIC version i linked to probably shouldn't be used if speed is important. it's slow. i think i timed it at 3 seconds, and it's about 92 bytes. then i clocked one that was about 120 bytes but it was twice as fast.

You're just to helpful of a person to leave this without a quick answer :P Ya, mine is 114 bytes but is twice as fast as your 95 byte one.

Glad you got an answer, Qwerty.55.
« Last Edit: August 18, 2010, 12:45:33 am by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: hexadecimal conversion
« Reply #7 on: August 18, 2010, 12:47:13 am »
The features I currently have written and debugged:

Moving cursor(s)
Plot pixel/invert pixel
Plot all pixels
Invert pixels
Plot all pixels in a row
Plot all pixels in a column
Exit to Hex code

I'll definitely be adding an Erase all pixels feature, an Erase all in Row feature, and an Erase all in Column. I'm going to try to implement "flip grid horizontally," and "Flip grid vertically" as well.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: hexadecimal conversion
« Reply #8 on: August 18, 2010, 12:49:59 am »
Sounds like a nice project ^^. I wished that would be ported to Axe at one point.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: hexadecimal conversion
« Reply #9 on: August 18, 2010, 12:50:07 am »
qwerty, may i suggest real-time hex display? the hex routine i wrote in this thread would take a few seconds to execute under TI-Basic, but with Axe it's not even humanly noticeable. maybe you could get hex to display every time you change a pixel on the screen? if you do this, you'll find flipping/rotating much easier, since you can then use the built-in functions for it.


Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: hexadecimal conversion
« Reply #10 on: August 18, 2010, 12:56:18 am »
I don't have room for a full Hex display. The grid is a 73x57 object and reducing it would mean that I'd have to recode all of the sprites or eliminate them. But I could use the real time hex code to draw a stationary sprite to illustrate how the final code will look.

@DJ, I'm writing it in Axe, so porting would essentially just be optimization, which I assure you my code could get.

Also, the Erase all feature is coded and working.

EDIT:

Nemo, I'm not sure I understand the routine you posted. Where does the Hex string store to and how would the user call it? Would it be {I+P} and Rcl {I+P}?

Also, since strings are numbers, could I use a For() loop to store generate the binary strings like

Code: [Select]
pxl-Test(X, Y)+"Str1"->Str1
« Last Edit: August 18, 2010, 01:35:34 am by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: hexadecimal conversion
« Reply #11 on: August 18, 2010, 01:39:43 am »
Hey that's cool!  Porting a program an excellent way to learn Axe given a background in BASIC.

By the way, would a >Hex command be useful?  I already have a >Dec, >Char, and >Tok so in a similar way I can add a >Hex.  There is even a really cool optimization for it that uses the daa instruction which is something I've never used but always wanted to.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: hexadecimal conversion
« Reply #12 on: August 18, 2010, 04:00:06 am »
qwerty, may i suggest real-time hex display? the hex routine i wrote in this thread would take a few seconds to execute under TI-Basic, but with Axe it's not even humanly noticeable. maybe you could get hex to display every time you change a pixel on the screen? if you do this, you'll find flipping/rotating much easier, since you can then use the built-in functions for it.

That sounds exactly like my sprite editor. :P But thanks for reminding me about it, I should work on it a bit more.
« Last Edit: August 18, 2010, 04:05:27 am by Runer112 »

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: hexadecimal conversion
« Reply #13 on: August 18, 2010, 09:25:29 am »
Hey that's cool!  Porting a program an excellent way to learn Axe given a background in BASIC.

By the way, would a >Hex command be useful?  I already have a >Dec, >Char, and >Tok so in a similar way I can add a >Hex.  There is even a really cool optimization for it that uses the daa instruction which is something I've never used but always wanted to.
Using daa seems so out of place in Axe... go ahead and add it ;D
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: hexadecimal conversion
« Reply #14 on: August 18, 2010, 12:14:59 pm »
That sounds exactly like my sprite editor. :P But thanks for reminding me about it, I should work on it a bit more.

cause i like real-time hex display (:

By the way, would a >Hex command be useful?  I already have a >Dec, >Char, and >Tok so in a similar way I can add a >Hex.  There is even a really cool optimization for it that uses the daa instruction which is something I've never used but always wanted to.


yesssss. do it.

qwerty: here's a very commented version of the code.
Code: [Select]
GetCalc("Str1",16)->I       
.GetCalc() is used to write to external variables. it reads "Str1" and locates the OS variable Str1
.It then makes Str1 16 bytes (which is equivalent to 16 characters) long.
.Storing this to pointer "I" will allow us to read and write to the data currently in OS var Str1.
For(P,0,15      .
.Loop through 16 times, as an 8x8 sprite has 16 hex characters
0->C   
.A holder variable, to get each value of the hex string. a better explanation below.
For(T,0,3       
.there are 4 pixels needed to test to determine. why 4 pixels?
.because a hex character can hold values 0-F, or 0-15.
.if you take 4 pixels as a binary string of 0's and 1's, the highest value
.possible is 16, or 2^4.
e^(3-T)*pxl-Test(P^2*4+T,P/2)+C->C
.This is tricky. first, the correct power of two is calculated. e^(3-T)
.which returns either a 8, 4, 2, or 1 depending on the value of T.
.pxl-Test() returns a 1 or a 0. if there's no pixel, the value won't be added.
.If there's a pixel, e^(3-T) will be multiplied by 1, added to C giving the value of the half-byte.
.P^2*4+T,P/2 is some arithmetic explained below.
End
{"0123456789ABCDEF"+C}->{I+P} 
.the part in quotation marks is 16 characters long. this means it's 16 bytes long.
.the curly {} brackets get the byte (in this case, a character) at a pointer.
."0123456789ABCDEF" is a pointer, and so is the variable C.
.Since C contains the value of the halfbyte, when you add the part in quotations to C,
.you get the corresponding character in the string to the value of the half-byte.
.Remember how I points to our OS var Str1? now it's storing our hex character
.into the string. P shifts the characters along the string.
End

alright. a little spiel about hex, binary and hex characters:

0111. this is a binary string. each value is either 1 or 0. to get the value in decimal, you start at the very rightmost bit (a bit is a 1 or a 0). the rightmost bit is 1. add 1 to your running total. then go left, to the next bit. the bit is also 1. however, binary and the powers of two are very related. this bit is worth 2. add two to the running total, you get 3. the next bit is also a one. this bit is worth 4. add it to your total, and you get 7. the next bit is 0 and therefore doesn't contribute. 0111binary = 7 decimal. 1111 = 15 decimal, because the last bit is worth 8 in decimal.

binary's pretty simple. you start at the rightmost bit, and count that as "1", multiply it by two and that's the value of the next bit, etc.

so try to decipher 01100101.
start from the right. you get 1+0+4+0+0+32+64+0 = 101.
some terms: 1 bit is either a 1 or a 0. a byte is 8 bits long. therefore, a half-byte is 4 bits long. a half-byte is also called a "nibble".
a hex character is 0-F, and therefore can hold 16 values. a half-byte can also hold 16 different values. general rule: 2^(# of bits) = numbers of possible values that can be held.

whew. alright... last thing. the arithmetic. (P^2*4+T,P/2).
the first expression is P^2*4+T.  P^2 is P modulus two, or the remainder after division. therefore, P^2 can either return a 1 or a 0. this is to determine which half-byte we're testing, the first or second? if it's the first, P^2 returns a 0, and multiplied by 4 does nothing and the pxl-Test() is determined by the value of T. otherwise, it adds four, and tests the second half-byte.
P/2 is rather simple, but it's a little neat that axe drops decimal points.
since P holds values 0-15 in the for loop, P/2 will return the following:
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7.
this way, the Y coordinating of the Pxl-Test is the same for two iterations of the For() loop, so we can get both Half-bytes in the row of the sprite.

hope that makes some sense.. i kinda got carried away, maybe, probably