How to convert decimals to Ascii characters?

9 views (last 30 days)
Jefe on 30 Sep 2022
Edited: James Tursa on 30 Sep 2022
So I have a char with a bunch of decimals in each line, these decimals go from 0 to 63. I'm trying to convert these decimals to the one that represents it in the image below. So 0 should be changed to A, 1 should be changed to B and so on. Anyone know if there are some functions to make this easier?

John D'Errico on 30 Sep 2022
Edited: John D'Errico on 30 Sep 2022
The ascii encoding is not what you are asking to find, since this produces a different result.
D = 0:63;
char(D + 'A')
ans = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
However, nothing stops you from using a simple lookup table.
CharLut = ['A':'Z','a':'z','0':'9','+/']
CharLut = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
Now you can evaluate any set of integers through that lut.
N = randi(63,[1,50])
N = 1×50
32 15 58 4 11 56 51 28 37 14 47 14 20 47 54 24 16 21 7 48 43 55 52 8 15 25 39 28 6 34
CharLut(N+1)
ans = 'gP6EL4zclOvOUv2YQVHwr30IPZncGie5P0GaMCDsVlCxb/v63p'
Dont forget that MATLAB uses a 1 index origin, so you need to add 1.

James Tursa on 30 Sep 2022
Edited: James Tursa on 30 Sep 2022
Your table isn't ASCII encoding of characters, so you can't use simple functions such as double( ) etc. You are probably going to have to write your own conversion function. I would suggest looking at the ismember( ) function using the 2nd Locb output. E.g. start with this char string:
S = ['A':'Z','a':'z','0':'9','+','/']
S = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
Then you can use this for mapping back & forth between the numbers and the characters, offsetting the index by 1. E.g.,
c = 'THEstring'
c = 'THEstring'
[~,N] = ismember(c,S); % From char string to numbers
x = N-1 % Offset by 1
x = 1×9
19 7 4 44 45 43 34 39 32
C = S(x+1) % From numbers back to char string, offset by 1
C = 'THEstring'
It would also help if you provided example inputs and desired outputs in your question so we know exactly what you want.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by