Getting Ascii string from hex 'string' 
Author Message
 Getting Ascii string from hex 'string'

I presume this is done with binary scan or format or scan, but my dull
mind can't figure it out. I want to pass a hex variable/buffer to a
proc and return the ascii character string.

Something like set hex_str 4f4f4f
set ascii_str [my_proc $hex_str]

puts $ascii_str

Quote:
>OOO

? sugestions

This is other stuff I was playing with:

#Returns hex value of an ascii character
proc Ascii { char } {
binary scan $char "H2" val
puts $val

Quote:
}

#returns hex value of buffer
proc ascii2hex { char } {
set size [ expr [string length $char] * 2]
binary scan $char "H$size" val
puts $val

Quote:
}

#returns ascii value of buffer
proc hex2ascii { hexval } {
binary scan  ????????

Quote:
}

Sent via Deja.com http://www.*-*-*.com/
Before you buy.


Sat, 24 May 2003 03:00:00 GMT  
 Getting Ascii string from hex 'string'
Try,
proc htoa {Hex} {
   set len [string length $Hex]
   for {set s 0; set e 1} {$e < $len} {incr s 2;incr e 2} {
      set pick [format %c "0x[string range $Hex $s $e]"]
      append result $pick
   } ;# end for
   return $result

Quote:
}

Tom Wilkason
Quote:

> I presume this is done with binary scan or format or scan, but my dull
> mind can't figure it out. I want to pass a hex variable/buffer to a
> proc and return the ascii character string.

> Something like set hex_str 4f4f4f
> set ascii_str [my_proc $hex_str]

> puts $ascii_str
> >OOO

> ? sugestions

> This is other stuff I was playing with:

> #Returns hex value of an ascii character
> proc Ascii { char } {
> binary scan $char "H2" val
> puts $val
> }

> #returns hex value of buffer
> proc ascii2hex { char } {
> set size [ expr [string length $char] * 2]
> binary scan $char "H$size" val
> puts $val
> }

> #returns ascii value of buffer
> proc hex2ascii { hexval } {
> binary scan  ????????
> }

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sat, 24 May 2003 03:00:00 GMT  
 Getting Ascii string from hex 'string'
I've tested this and it works fine, thanks a lot. I'll have a good look
at it now to see how it works.

Cheers
Diarmait



Quote:
> Try,
> proc htoa {Hex} {
>    set len [string length $Hex]
>    for {set s 0; set e 1} {$e < $len} {incr s 2;incr e 2} {
>       set pick [format %c "0x[string range $Hex $s $e]"]
>       append result $pick
>    } ;# end for
>    return $result
> }

> Tom Wilkason




Quote:
> > I presume this is done with binary scan or format or scan, but my
dull
> > mind can't figure it out. I want to pass a hex variable/buffer to a
> > proc and return the ascii character string.

> > Something like set hex_str 4f4f4f
> > set ascii_str [my_proc $hex_str]

> > puts $ascii_str
> > >OOO

> > ? sugestions

> > This is other stuff I was playing with:

> > #Returns hex value of an ascii character
> > proc Ascii { char } {
> > binary scan $char "H2" val
> > puts $val
> > }

> > #returns hex value of buffer
> > proc ascii2hex { char } {
> > set size [ expr [string length $char] * 2]
> > binary scan $char "H$size" val
> > puts $val
> > }

> > #returns ascii value of buffer
> > proc hex2ascii { hexval } {
> > binary scan  ????????
> > }

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.


Fri, 30 May 2003 18:34:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. From Hex #'s to string

2. convert hex #'s and letters to one string constant

3. Help converting hex string to 16-bit signed two's complement integer

4. STRING 'make'/'remake'

5. number's to string -) tcp-ip -) scan from string problem

6. First X Words in a String can't use [string wordend] nor [lrange]

7. Hex String to Decimal String ocnversion.

8. Fast conversion of hex string to character string

9. Ascii to Hex, Hex to Ascii convertion

10. TIP #188: Add 'string is wide' to the 'string is' Subcommand

11. Error in 'string toupper' and 'string tolower'

12. Convert a hex string to the same hex number

 

 
Powered by phpBB® Forum Software