Convert letters to hex
Hello!
I have a question about convert letters to hex.
example.
string text contains "AAABBB"
and then i wont string hextext to contain "414141424242"
41 for A (ascii in hex)
42 for B (ascii in hex)
How do i do that?
Martin
I have a question about convert letters to hex.
example.
string text contains "AAABBB"
and then i wont string hextext to contain "414141424242"
41 for A (ascii in hex)
42 for B (ascii in hex)
How do i do that?
Martin

Comments
It's already in that form. Just look at the memory dump.
So,
You have to get every byte and convert it to the appropriate two digit equivalent, but you knew that.
In Spin, I do not know, there is no String handling... but... if you use a pointer to a byte array, that will/may work:
mystr : is your string...
myhex: is your hex result
Other better and more optimized versions could exist
PRI Set_Preferences | field, position, keyok, k, in_value, process_char, change_position, vptr, form_exit, new_field, wmask, wval, wtime {Trigger Mask&Value, Edge Mask, Slow Delta} Display.out(cls) color(white) wmask := tmask 'copy global variables into wval := tval 'local variables wtime := sval Display.hex(tmask,8) 'display the current values Display.str(string(" Mask")) gotoxy(0,1) Display.hex(tval,8) Display.str(string(" Value")) gotoxy(0,2) Display.hex(sval,8) Display.str(string(" Time")) gotoxy(0,FrameBottom - 2) Display.str(string("Press <Enter> to accept")) gotoxy(6,FrameBottom - 1) Display.str(string("<Esc> to cancel")) gotoxy(0,FrameBottom) color(highlight) Display.str(string("Trigger pin mask, 00000000=off "))'display first help line display_nibble(0,0,tmask>>28) 'set current nibble to inverse color(white) position := 0 'initialize field and position being edited field := 0 [color=red] repeat repeat keyok := false 'clear working flags process_char := false change_position := false form_exit := false k := key.key 'get a keystroke case k Tab, UpArrow, DownArrow, LeftArrow, RightArrow: 'position changing keystrokes change_position := true keyok := true Enter, Esc: 'leave form keystrokes form_exit := true keyok := true "0".."9": in_value := k-"0" 'hexadecimal charactor process_char := true keyok := true "A".."F": in_value := k-"A"+ $A process_char := true keyok := true "a".."f": in_value := k-"a"+ $A process_char := true keyok := true [/color] until keyok [color=black] if(process_char) 'record char vptr := lookupz(field: @wmask, @wval, @wtime) 'retrieve pointer to variable [/color][color=red] if(position//2 == 0) 'change high nibble BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] &= NMSK 'clear high nibble BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] |= in_value << 4 'set high nibble value else 'change high nibble BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] &= !NMSK 'clear low nibble BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] |= in_value 'set low nibble value [/color] 'display char in normal color(white) display_nibble(position,field,in_value) 'display the new nibble value 'advance position, boundry check/change field if(++position > 7) position := 0 if(++field > 2) field := 0 'highlight new char vptr := lookupz(field: @wmask, @wval, @wtime) 'retrieve pointer to variable if(position//2 == 0) 'read high nibble in_value := BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] >> 4 else 'read low nibble in_value := BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & NMSK color(highlight) display_nibble(position,field,in_value) 'highlight next nibble 'if new field, display in context help line if position == 0 gotoxy(0,FrameBottom) case field 0: Display.str(string("Trigger mask, $00000000=off ")) 1: Display.str(string("Trigger value for bits in mask ")) 2: Display.str(string("Clocks/sample for slow aquire ")) new_field := false 'reset new field flag if(change_position) color(white) vptr := lookupz(field: @wmask, @wval, @wtime) 'retrieve pointer to variable if(position//2 == 0) 'read high nibble in_value := (BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & !NMSK) >> 4 else 'read low nibble in_value := BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & NMSK display_nibble(position,field,in_value) 'set nibble in normal text case k Tab: ++field 'increment feild field //= 3 'wrap around position:=0 'set position to zero new_field := true 'indicate new field reached UpArrow: --field 'decrement feild being edited field #>= 0 'boundry limit new_field := true 'indicate new field reached DownArrow: ++field 'increment feild being edited field <#= 2 'boundry limit new_field := true 'indicate new field reached LeftArrow: --position 'decrement position being edited position #>= 0 'boundry limit RightArrow: ++position 'increment position being edited position <#= 7 'boundry limit color(highlight) if new_field 'if new field get new variable vptr := lookupz(field: @wmask, @wval, @wtime) if(position//2 == 0) 'retrieve high nibble in_value := (BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & !NMSK) >> 4 else 'retrieve low nibble in_value := BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & NMSK display_nibble(position,field,in_value) 'highlight new nibble 'if new field, display in context help line if new_field gotoxy(0,FrameBottom) case field 0: Display.str(string("Trigger pin mask, 00000000=off ")) 1: Display.str(string("Trigger value for bits in mask ")) 2: Display.str(string("Clocks/sample for slow aquire ")) if(form_exit) 'exit the form case k Enter: 'if enter tmask := wmask 'update global variables tval := wval & wmask 'use only bits same as mask sval := wtime #> $12 'ensure waitcnt miss wont occur return▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.