You could do something quick'n'dirty in Perl:
$ perl -alne 'print join " ", map { ord $_ } split //,
. > 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?
Convert string to ASCII value python - Stack Overflow
Function to convert ASCII to a string
I'm a little confused by what you are trying to show here. What is the content of $ascii?
A sequence of ASCII-encoded characters is already a string...in ASCII encoding. Are you trying to change encodings?
More on reddit.comcoverting string to ascii value in Matlab
How do I convert a list of ascii values to a string in python? - Stack Overflow
Videos
You could do something quick'n'dirty in Perl:
$ perl -alne 'print join " ", map { ord $_ } split //,
. > 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'}
You can use a list comprehension:
>>> s = 'hi'
>>> [ord(c) for c in s]
[104, 105]
Here is a pretty concise way to perform the concatenation:
>>> s = "hello world"
>>> ''.join(str(ord(c)) for c in s)
'10410110810811132119111114108100'
And a sort of fun alternative:
>>> '%d'*len(s) % tuple(map(ord, s))
'10410110810811132119111114108100'
EDIT 2: See this comment for a more elegant solution using .NET.
EDIT: Sorry, I wasn't clear on what this actually does. It converts an array of character codes to ASCII text. Thanks u/ihaxr for pointing that out.
Because this was a source of some frustration to me today and I would like to save you all the trouble. Especially useful for converting values returned by System.DirectoryServices.
function Convert-ASCII {
param($ascii)
$letter = @()
foreach ($char in $ascii){
$char = [int[]]$char
$letter += [char[]]$char
}
$final = ("$letter").Replace(" ","")
Return $final
I'm a little confused by what you are trying to show here. What is the content of $ascii?
A sequence of ASCII-encoded characters is already a string...in ASCII encoding. Are you trying to change encodings?
You have an array of bytes, turn it into an array of char, and join it into a string
PS C:\> $x = 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100
PS C:\> [char[]]$x -join ''
hello world
Or ... not ascii:
PS C:\> $q = [int[]][char[]]'héllo wórld'
PS C:\> [char[]]$q -join ''
héllo wórld
You are probably looking for 'chr()':
>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> ''.join(chr(i) for i in L)
'hello, world'
Same basic solution as others, but I personally prefer to use map instead of the list comprehension:
>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> ''.join(map(chr,L))
'hello, world'