For Programmers: Free Programming Magazines  


Home > Archive > Tcl > January 2006 > Converting ASCII characters <-> binary string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Converting ASCII characters <-> binary string
Larry W. Virden

2006-01-24, 7:05 pm

Does anyone have a pointer for methods of converting a traditional into
a string of 1 and 0 ascii characters which represent the original
string?

For instance, such code might change "abc" into
"011000010110001001100011" (I think I did that right)... and vice
versa.

Andreas Leitgeb

2006-01-24, 7:05 pm

Larry W. Virden <lvirden@gmail.com> wrote:
> Does anyone have a pointer for methods of converting a traditional into
> a string of 1 and 0 ascii characters which represent the original
> string?
> For instance, such code might change "abc" into
> "011000010110001001100011" (I think I did that right)... and vice
> versa.


Have you looked at binary scan / binary format already?
% binary scan "abc" B* bstr; set bstr
011000010110001001100011
% binary format B* 011000010110001001100011
abc

PS:
When you mentioned "a traditional" I thought for a moment
you were talking about some sort of chinese (thus unicode)
charset/whatever.
% binary scan \u1234 B* bstr; set bstr
00110100
getting all the single bits of that requires some
encoding convertto unicode ..., and even then they
appear mixed up some way.

Don Porter

2006-01-24, 7:05 pm

Larry W. Virden wrote:
> Does anyone have a pointer for methods of converting a traditional into
> a string of 1 and 0 ascii characters which represent the original
> string?
>
> For instance, such code might change "abc" into
> "011000010110001001100011" (I think I did that right)... and vice
> versa.


First convert your string into bytes that represent that string in
the ASCII encoding:

set string abc
set bytes [encoding convertto ascii $string]

Next use [binary] to convert those bytes to a base 2 string rep:

binary scan $bytes B* bits
set bits

Reverse direction left as an exercise.

--
| Don Porter Mathematical and Computational Sciences Division |
| donald.porter@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|_______________________________________
_______________________________|
billposer@alum.mit.edu

2006-01-24, 7:05 pm

Larry W. Virden wrote:
> Does anyone have a pointer for methods of converting a traditional into
> a string of 1 and 0 ascii characters which represent the original
> string?
>
> For instance, such code might change "abc" into
> "011000010110001001100011" (I think I did that right)... and vice
> versa.


Well, sort of. I have a set of C subroutines that generate binary text
from integers of various sizes. It shouldn't be too hard to translate
to Tcl.
Here's the one for chars.
#ifndef CHAR_BITS
#define CHAR_BITS 8
#endif
#define CBITS (sizeof(unsigned char) * CHAR_BITS)
#define MASK 0x01

char *
binfmtc(unsigned char n)
{
int i;
static char str [CBITS+1];

for (i = CBITS-1; i >= 0;i--){
str[i] = (n & MASK ? '1' : '0');
n >>= 1;
}
str[CBITS] = '\0';
return(str);
}

Larry W. Virden

2006-01-24, 9:58 pm

I looked at them - your example helped me a bit. However, I keep
getting:
./tochar 0-0000--0
expected binary string but got "0-0000--0" instead
while executing
"binary format B* $i"
("foreach" body line 2)
invoked from within
"foreach i $argv {
binary format B* $i
puts $bstr
}"
(file "./tochar" line 3)
cat tochar
#! /var/tmp/ActiveTcl/bin/tclsh

foreach i $argv {
binary format B* $i
puts $bstr
}

So I'm missing something ... probably very simple.

Paul Whitfield

2006-01-24, 9:58 pm

Larry W. Virden wrote:
> I looked at them - your example helped me a bit. However, I keep
> getting:
> ./tochar 0-0000--0
> expected binary string but got "0-0000--0" instead
> while executing
> "binary format B* $i"
> ("foreach" body line 2)
> invoked from within
> "foreach i $argv {
> binary format B* $i
> puts $bstr
> }"
> (file "./tochar" line 3)
> cat tochar
> #! /var/tmp/ActiveTcl/bin/tclsh
>
> foreach i $argv {
> binary format B* $i
> puts $bstr
> }
>
> So I'm missing something ... probably very simple.



You must use 1 and 0 for binary ... not 0 and - ?

eg:

%binary format B* 0110001
b
% binary format B* 0--000-
expected binary string but got "0--000-" instead

Regards

Palu
slebetman@yahoo.com

2006-01-25, 4:12 am

Paul Whitfield wrote:
> Larry W. Virden wrote:
>
> You must use 1 and 0 for binary ... not 0 and - ?
>
> eg:
>
> %binary format B* 0110001


Yes a binary string as understood by 'format' and 'scan' must contain
only 1s and 0s. However, if Larry still wants to use his funky -s and
0s format, Tcl can do that too:

set result [binary format B* [string map "- 1" $input_string]]

which basically substitutes - with 1 before formatting.

Larry W. Virden

2006-01-25, 7:57 am

I apologize - obviously, my brain wasn't in gear last nite, forgetting
to do the map.

Sigh. I have to get over this cold!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com