Convert a string of characters to ascii values

I have been searching through the internet on how to do this but have failed to find a way to actually do it..

Basically, I have a series of large dataframes, and I wish to convert one of the column vectors in all of these datasets from a string of characters, say for example: ASDFSDFSAFDSA, to its equivalent in ASCII format. I want to do this in bash because they are too large to process in R.

I know in java and R there are functions that do this, but have failed to find its equivalent in bash. I have looked at the xxd function as well as some posts on the forum but they end up giving me just a couple of integers rather than the proper ASCII format.

Here is the dput output of a small snippet of the data:

structure(list(Clone.ID = 0:5, Clone.count = c(2454L, 1915L,
1369L, 1255L, 1152L, 1099L), AA..Seq..CDR3 = c("CASSNSDRTYGDNEQFF",
"CATSSVLTQQETQYF", "CASSSRGLANTQYF", "CASSLGTALNTEAFF", "CASSRRHLGNTGELFF",
"CASSEGRSNQPQHF")), row.names = c(NA, 6L), class = "data.frame")

The data uploaded looks like this:

 Clone.ID Clone.count AA..Seq..CDR3
1 0 2454 CASSNSDRTYGDNEQFF
2 1 1915 CATSSVLTQQETQYF
3 2 1369 CASSSRGLANTQYF
4 3 1255 CASSLGTALNTEAFF
5 4 1152 CASSRRHLGNTGELFF
6 5 1099 CASSEGRSNQPQHF

The desired output would be for the col AA..Seq.CDR3 to have the following entries instead:

067 065 083 083 078 083 068 082 084 089 071 068 078 069 081 070 070
067 065 084 083 083 086 076 084 081 081 069 084 081 089 070
067 065 083 083 083 082 071 076 065 078 084 081 089 070 #and so on...

Also, it would be ideal if the ascii representation was as an integer rather than an array of ints as is the output for R's conversion - and pythons too I believe.

Any help would be much appreciated.

Thank you all for your time,

7

2 Answers

You could do something quick'n'dirty in Perl:

$ perl -alne 'print join " ", map { ord $_ } split //, $F[3] if $. > 1' datafile
67 65 83 83 78 83 68 82 84 89 71 68 78 69 81 70 70
67 65 84 83 83 86 76 84 81 81 69 84 81 89 70
67 65 83 83 83 82 71 76 65 78 84 81 89 70
67 65 83 83 76 71 84 65 76 78 84 69 65 70 70
67 65 83 83 82 82 72 76 71 78 84 71 69 76 70 70
67 65 83 83 69 71 82 83 78 81 80 81 72 70

However if you are already processing the data in R, then you should consider using its native utf8ToInt as described here R: How to convert characters into ASCII code?

Hmmm... may I ask why you want to do this? I would personally probably do the reverse, 'causr I'm more fluent in letters than in decimal codes..

However, let's rephrase your problem cl to more precisely define the goal: - you have in a table a column of ASCII chars in the range A-Z (or is it also lower case?) - you want each character to be translated with its decimal cvalue over one space and 3 digits

Bash has a powerful string / char replacement, eg

zz=CABXAB;echo ${zz//A/' 065'}

7

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like