Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Long String to ASCII

Posted by neilvp (neilvp), 8 April 2005
In my application I receive a string back from the target that is an 8 byte string of hex values representing an 8 digit ascii string.
e.g. 3938373635343332 = 98765432

and I want to display the 98765432 version.

It occurs to me that there should be an elegant way of doing this. I have tried regsub but can't get it to work.

Can anyone offer any hints as to how to do this please?

Thanks





Posted by admin (Graham Ellis), 9 April 2005
I think you want something like this (it's very similar to what we do on web pages to translate the hex codes or URL encoding

regsub -all -nocase {([0-9a-f][0-9a-f])} $value \
                             {[format %c 0x\1]} value


Posted by neilvp (neilvp), 11 April 2005
Oh Great Guru Graham
Thanks for example, unfortunately I find that even this does not work in that it does not do as I would expect it to.  


My code

# regsub1.tcl
# TCL Script to test regsub
#
# 11 Apr 05
#
# N Peers
#
# Idea is to convert a string of ascii values into a string # of ascii characters
# e.g. 39383736 into 9876


set svalue 3938

regsub -all -nocase {([0-9a-f][0-9a-f])} $svalue \
    {[format %c 0x\1]} svalue

puts "String is now $svalue"

The result


%
% source regsub1.tcl
String is now [format %c 0x39][format %c 0x38]
%
%
%


Whereas I would expect it to put

String is now 98

What am I doing wrong?

Yours very frustratedly

Neil







Posted by admin (Graham Ellis), 11 April 2005
You're doing nothing wrong ... you're just short of the punchline because I failed to give it to you ...

Code:
set value 5965732021
regsub -all -nocase {([0-9a-f][0-9a-f])} $value {[format %c 0x\1]} value
puts [subst $value]


Prints

Yes !

Posted by neilvp (neilvp), 13 April 2005
Apologies. Now I understand what the manual was saying all the time!
A classic case of 'Read what is written, not what you think is written'

Thank you.



Posted by patthoyts (patthoyts), 18 April 2005
on 04/08/05 at 09:57:50, neilvp wrote:
In my application I receive a string back from the target that is an 8 byte string of hex values representing an 8 digit ascii string.
e.g. 3938373635343332 = 98765432

and I want to display the 98765432 version.

It occurs to me that there should be an elegant way of doing this. I have tried regsub but can't get it to work.

Can anyone offer any hints as to how to do this please?


set data [binary format H* $hexdata]




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho