jrhodes Posted October 10, 2022 Share Posted October 10, 2022 I hate reinventing the wheel, so I was wondering if there is a subroutine i could include in a standard (non-RXB) Extended Basic program to convert a binary string into a hex string. Quote Link to comment Share on other sites More sharing options...
WhataKowinkydink Posted October 10, 2022 Share Posted October 10, 2022 Here's a code segment - probably not exactly what you're looking for, but here it is. It assumes DEC is a DECimal input, and T(E) == 2^X for X=0 to 15 (so basically an extra conversion here from decimal to binary before then converting to hex). 100 BIN$="" :: FOR E=15 TO 0 STEP -1 110 IF DEC>=T(E)THEN BN16$(E)="1" :: DEC=DEC-T(E) 120 BIN$=BIN$&BN16$(E):: NEXT E 130 HEX$="" :: FOR E=3 TO 0 STEP -1 140 IF SEG$(BIN$,-4*E+13,4)="1111" THEN HX$(E)="F" 150 IF SEG$(BIN$,-4*E+13,4)="1110" THEN HX$(E)="E" 160 IF SEG$(BIN$,-4*E+13,4)="1101" THEN HX$(E)="D" 170 IF SEG$(BIN$,-4*E+13,4)="1100" THEN HX$(E)="C" 180 IF SEG$(BIN$,-4*E+13,4)="1011" THEN HX$(E)="B" 190 IF SEG$(BIN$,-4*E+13,4)="1010" THEN HX$(E)="A" 200 IF SEG$(BIN$,-4*E+13,4)="1001" THEN HX$(E)="9" 210 IF SEG$(BIN$,-4*E+13,4)="1000" THEN HX$(E)="8" 220 IF SEG$(BIN$,-4*E+13,4)="0111" THEN HX$(E)="7" 230 IF SEG$(BIN$,-4*E+13,4)="0110" THEN HX$(E)="6" 240 IF SEG$(BIN$,-4*E+13,4)="0101" THEN HX$(E)="5" 250 IF SEG$(BIN$,-4*E+13,4)="0100" THEN HX$(E)="4" 260 IF SEG$(BIN$,-4*E+13,4)="0011" THEN HX$(E)="3" 270 IF SEG$(BIN$,-4*E+13,4)="0010" THEN HX$(E)="2" 280 IF SEG$(BIN$,-4*E+13,4)="0001" THEN HX$(E)="1" 290 IF SEG$(BIN$,-4*E+13,4)="0000" THEN HX$(E)="0" 300 HEX$=HEX$&HX$(E):: NEXT E 6 Quote Link to comment Share on other sites More sharing options...
ti99iuc Posted October 11, 2022 Share Posted October 11, 2022 If it could be useful, I had also published this from the Programs for the TI Home Computer https://www.ti99iuc.it/web/index.php?pageid=database_cerca&archivioid=380#.Y0UzE3ZByUk 3 Quote Link to comment Share on other sites More sharing options...
sometimes99er Posted October 11, 2022 Share Posted October 11, 2022 Here's something to get you started. 😉 100 PRINT 110 INPUT "DEC VAL: ":D 120 CALL CONVERT((D),16,"HEX VAL: ") 125 CALL CONVERT((D),8,"OCT VAL: ") 130 CALL CONVERT((D),2,"BIN VAL: ") 140 GOTO 100 200 SUB CONVERT(D,B,T$) 210 R$="" 220 C=D-INT(D/B)*B 230 IF C<10 THEN R$=CHR$(C+48)&R$ ELSE R$=CHR$(C+55)&R$ 240 D=INT(D/B)::IF D>0 THEN 220 250 PRINT T$;R$ 260 SUBEND 9 Quote Link to comment Share on other sites More sharing options...
WhataKowinkydink Posted October 11, 2022 Share Posted October 11, 2022 (edited) 1 hour ago, sometimes99er said: Here's something to get you started. 😉 Hey, now that is clean - and interesting approach. I had to look step through it with an example number. I'm usually too bullheaded in my strict procedural approach to come up with something so tidy. Edited October 11, 2022 by WhataKowinkydink 3 Quote Link to comment Share on other sites More sharing options...
sometimes99er Posted October 11, 2022 Share Posted October 11, 2022 Binary to Hex ... 😊 100 c$="0000-0-0001-1-0010-2-0011-3-0100-4-0101-5-0110-6-0111-7-1000-8-1001-9-1010-A-1011-B-1100-C-1101-D-1110-E-1111-F" 110 accept validate("01") size(8):b$ ::b$=rpt$("0",8)&b$::b$=seg$(b$,len(b$)-7,8)::b1$=seg$(b$,1,4)::b2$=seg$(b$,5,4) 120 print :seg$(c$,pos(c$,b1$,1)+5,1)&seg$(c$,pos(c$,b2$,1)+5,1) 7 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.