need help with ascii to decimal conversion

Hi,

Can anyone please help me ascci to decimal conversion in bash

I have a file which contains stream of numbers like this,these are ascci values

729711810132973278105991013268971213233

I want to covert it to its actual value like upper code's decimal is

"Have a Nice Day!"

Thanks In advance
Sunil

The correct spelling is "ascii". See "man ascii".
What software are you using to view the file which produces the character record you describe?
Recording characters as two-digit? decimal is much too weird. Can you post a definitive unix hexadecimal dump af a couple of records such that the bit pattern of the data is 100% clear.

And just how did you figure out that out ?

You'll need to add a separator character (space or comma etc.) to delimit individual ASCII decimal values. Otherwise it's difficult to build the string from your input data.

tyler_durden

Can you provide sample input and sample output with an explanation of the process required to convert input to output? Then we can consider code for the conversion.

I can giv u logic.

we have only 1st chracter to control the flow,else we cant handle..

letc consider ascii values 1st

[A-Z]=[65-96]
[a-z]=[97-132]
and special characters like space=32 etc..

so if 1st character is 1 then we have to read 3 charaters continusly then assign this value to a valiable and print its decimal value

then read 4th character and check if its 1 again same logic,

in case its not one we have to read 2 characters and and print its decimal value..and so on till EOF

Did any one get any idea of my logic or correct me if any one has any thing else suggest?

--------------------------------------------------------------------------------

Methyl sample code for u..
a file contails a values (acsii) like this
65666768

I have to read the file and convert these values in its actual values
so out put of above should be
ABCD

exsample 2
729711810132973278105991013268971213233
Have a Nice Day!

Note: there may be special chacters too ,for e$xample "!" in upper example

If any one can giv me code for vice versa of it i.e. changing simple text of decimal to ascii, it wud be appreciated

eg:Have a Nice Day!
729711810132973278105991013268971213233

(IMHO. This is interesting but impossible).

Almost everything is possible :cool:, try this:

awk '{
  for(i=1;i<=length;i+=2){
    if(int(substr($0,i,2)) < 20) {
      printf("%c",int(substr($0,i,3)))
      i++
    }
    else {
      printf("%c",int(substr($0,i,2)))
    }
  }
}
END{print ""}'

This is what I get:

$ echo '729711810132973278105991013268971213233' |
awk '{
  for(i=1;i<=length;i+=2){
    if(int(substr($0,i,2)) < 20) {
      printf("%c",int(substr($0,i,3)))
      i++
    }
    else {
      printf("%c",int(substr($0,i,2)))
    }
  }
}
END{print ""}'
Have a Nice Day !
$

Regards

Cool. Nice code.

My main issue was with ambiguous data. An ASCII character equates to any decimal value in the range 0-255 . Unless you cut the target range severely it is impossible to pick characters in their decimal representation out of a lengthy stream without delimiters.

Consider: 
2222222

It is however 100% definite with hexadecimal characters (providing that all leading zeros are stated).

This isn't a direct answer to how to do it, I last was involved at this level in converting ASCII to Hollerith, round holes in tape to rectangular holes in cards, but an understanding of what is represented might help you.
ASCII data is 2 channels of control and 5 channels of data or basically 4 blocks of 32 possibilities. The 0 to 32 block with no control value consists of control codes. The 32 block with channel 6 are printable special characters and numbers, with channel 7 are uppercase alphabetic, and with 6 plus 7 are lower case alphabetic. The numeric representation of any character is the number of the control block*32+the value represented by the 5 channels.

One way to do it using Perl:

$ 
$ echo "729711810132973278105991013268971213233" |
> perl -lne 'chomp($x=$_);
>            do { $inc = substr($x,$i,1)<2 ? 3 : 2; push @s,substr($x,$i,$inc); $i+=$inc
>               } while $i<length($x); print pack("C*",@s)'
Have a Nice Day !
$ 

And another way, especially if that string is too long:

$ ##
$ echo "729711810132973278105991013268971213233" |
> perl -ne 'chomp($x=$_);
>            do {if (substr($x,$i,1)<2) {$e=substr($x,$i,3);$i+=3}
>                else {$e=substr($x,$i,2);$i+=2} printf("%c",$e)
>               } while $i<length($x); print "\n"'
Have a Nice Day !
$ 

As a side note, this seems to be overkill for a simple task. It's kind of hard to imagine why the source system could not generate an array or a delimited string thereby marking boundaries for individual characters.

tyler_durden

Surely Hollerith (ISO) pre-dates ASCII by many years.
Edfair is quite correct in the origins of the collating sequence which relied on the position of holes in the punch card to perform a sort. Without a Hollerith card sorter what would the early James Bond movies use to represent a computer?

---------------------------------------------------------------------
Thanks you very much!

actully I have been asked to convert data into ascii stream with no delimiters.The data is store in a file say "codefile", it contains data like this
and contains multuiple lines

Have a Nice Day!

I wrote follwing script

-------------------------------------------------------
#!/bin/bash
while read -n1 char
do
printf "%d" \'$char
done < codefile
----------------------------------------------------------
Output is

./coder.sh
72971181010970781059910106897121330

But instead of spaces it prints 0(bolded in upper output), Which is supposed to be 32 (Ascii vale of space),Also it print an exta 0 at the end (marked as red).
Can any one please help me how to debug, Does printf not take special charaters as variable to print ? or the variable is not able to store the spcial characters like space?..Please help

Unfortunetly I am not familor with perl

Thanks in advance

appreciting all ur earlier efforts

-S

Methyl,
IBM's data transmission protocol was EBCDIC using 8 track tape through their 047 tape to card machine. One customer wanted to use a KSR33 to create ASCII tape and another to receive and generate a duplicate tape then convert the data to punch cards through their 047. To keep the machine whole, for later sale or lease, I hung a relay rack on the back to handle the decoding, bringing channel signals out and sending Hollerith in.
I would assume that IBM eventually worked up stuff to handle ASCII but at the time there wasn't anything in their playbook.

I'm confused. One part of the post says that the source is an A/N data stream and the requested output is to be a stream of ASCII characters. Then stuff shows like decimal representation but is incomplete.

If I'm confused, maybe someone else is too.

My posting here is more about my education than an attempt to help. I've done this in a unix to dos environment but handled it on the dos side.

Thanks you very much!
####This is a separate request###
I have been asked also to convert data into ascii stream with no delimiters.The data is store in a file say "codefile", it contains data like this

Have a Nice Day!

I wrote follwing script

-------------------------------------------------------
#!/bin/bash
while read -n1 char
do
printf "%d" \'$char
done < codefile
----------------------------------------------------------
Output is

./coder.sh
72971181010970781059910106897121330

But instead of spaces it prints 0(bolded in upper output), Which is supposed to be 32 (Ascii vale of space),Also it print an exta 0 at the end (marked as red).
Can any one please help me how to debug, Does printf not take special charaters as variable to print ? or the variable is not able to store the spcial characters like space?..Please help

Unfortunetly I am not familor with perl

Thanks in advance

appreciting all ur earlier efforts

-S

An example with multiple lines:

$ 
$ cat codefile
Hi! How are you?
Have a nice day!
Also sprach Zarathustra.
$ 
$ perl -lne 'chomp; @x=unpack("C*",$_); print @x' codefile
7210533327211111932971141013212111111763
729711810132973211010599101321009712133
65108115111321151121149799104329097114971161041171151161149746
$ 

If you are unfamiliar with the syntax and function, then have a look at the manual pages of the Perl documentation at The Perl Directory - perl.org

If you have no idea of what the heck Perl is, then check out books by O'Reilly Media for a good introduction (provided you have the willingness to learn, of course).

tyler_durden

Great Jobe! thanks very much

Any links to O'Reilly Media perl tutorial? I do have willingness and I am googling it.

-S

Thanks Again!