Binary to character function?

So far I have this function:
function char2bin
char=input('Enter a character: ', 's');
char2=(dec2bin(char))
sprintf('%08s',char2)
which takes in a character, and converts it into a vector of 8 0's and 1's, forced to be 8 with the '%08s' thingy.
Next, I need to make a function which will:
1. Convert the vector of bits to a vector ASCII numbers (0 becomes 48, 1 becomes 49).
2. Convert the vector of ASCII numbers to a character string (use the char function).
3. Use the bin2dec function to convert the string into a single ASCII number.
4. Convert the ASCII number into a character.
and I have no idea where to start with that, any help would be great, thank you in advance!

3 Comments

Erik Dekelbaum
Erik Dekelbaum on 24 Nov 2014
Edited: Erik Dekelbaum on 24 Nov 2014
(Note: For #1)
Can you make a function where you add 1 to each "bit"?
Pretty much a function with something like x = x+1 in it
not sure, do you think that'd work?
It might. Try making a new function where the output is the input vector of bits + 1. Then check the new output variable in the workspace. If the values are all incremented by 1, you know it worked.

Sign in to comment.

Answers (2)

Guillaume
Guillaume on 24 Nov 2014
  1. simply use double(somestring) to get the ASCII values of the character
  2. to reverse that operation: char(asciivalues)
  3. doesn't make sense to me
  4. see 2.

7 Comments

Okay so in my first function (I'm still not sure if it's done correctly)
function char2bin(charInput)
charInput = input('Enter a character: ', 's');
binvec = logical(dec2bin(charInput, 8) - '0');
sprintf('%x', binvec)
this function is supposed to allow the user to input a character, then convert it into a vector of logical bits, which is the binary equivalent of the ASCII value of the character.
In my next function, so far what I have is
function bin2char
x='01011000'
a=double(x)
chars=char(a)
dec=bin2dec(x)
again, not sure if this one is right either, but how can I get bin2char to accept the output from the other function? Thanks so much for your help in advance
First function is correct for what you want it to do.
Second function is correct for the steps 1 and 2 you asked. I don't see the point of step 2, since you get back the original x. (i.e. chars is the same as x).
However, if this second function is supposed to do the opposite of the first function, then your description is completely wrong, Such a function would be:
function character = bin2char(binvec)
svec = char(binvec + '0'); %convert logical vector to a string of 0-1
character = char(bin2dec(svec)); %convert string to decimal, then to char
end
The above code also shows you how to return a value from a function, so that you can pass it on to another function.
Hmmmm. Okay I'm not sure what the issue is now, but for my first function, I have:
function bin2char
x='01011000'
a=double(x)
chars=char(a)
dec=bin2dec(x)
and for the second function I tried what you said, so I had
function character = bin2char(binvec)
svec = char(binvec + '0'); %convert logical vector to a string of 0-1
character = char(bin2dec(svec)); %convert string to decimal, then to char
end
But when I run,
char2bin
bin2char(binvec)
I get
>> test
Enter a character: X
ans =
01011000
ans =
a
Which clearly isn't right. Here's exactly what I need to do if i'm wording it wrong or something:
bin2char function This function does the opposite of the char2bin function. Actually this function even works in the opposite way of the char2bin function, but thankfully it's simpler.
1. Convert the vector of bits to a vector ASCII numbers (0 becomes 48, 1 becomes 49).
2. Convert the vector of ASCII numbers to a character string (use the char function).
3. Use the bin2dec function to convert the string into a single ASCII number.
4. Convert the ASCII number into a character
Here's an example that shows that char2bin and bin2char are complimentary functions:
>> char2bin('X')
ans =
0 1 0 1 1 0 0 0
>> bin2char(ans)
ans =
X
I'm just not sure why it wouldn't work? it's weird. Thanks so much for your help already though. Just wondering if there's any way you know of that will let me fix this.
The bin2char I wrote does the steps you've described. I must have misunderstood something in my earlier answer:
  1. is binvec + '0'
  2. is char(result_of_1)
  3. is bin2dec(svec)
  4. is char(result_of_3)
To be able to pass a result from a function to another, you need to tell the function to return that result, just like I have done with my bin2char
function binvec = char2bin(charinput)
I would actually take out the charinput = input(...) line out of that function and use it in the main script instead
charinput = input(...);
binvec = char2bin(charinput)
origchar = bin2char(binvec)
binvec = char2bin(charinput)
origchar = bin2char(binvec)
Please share the code of the function char2bin(charinput) and bin2char(binvec)
out1 = char2bin('Hello, Usha!')
out1 = 1×96
0 1 0 0 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 1 1 0 1 1
out2 = bin2char(out1)
out2 = 'Hello, Usha!'
function binvec = char2bin(charinput)
binvec = reshape((dec2bin(charinput,8) - '0').',1,[]);
end
function origchar = bin2char(binvec)
origchar = char(reshape(bin2dec(reshape(char(binvec + '0'),8,[]).'),1,[]));
end
Thank you. It helped me a lot

Sign in to comment.

See my latest answer in your original question: http://www.mathworks.com/matlabcentral/answers/163995#comment_251754 I think it does what you want. For example you can send in '123abc' and it will return
strAscii =
011000101100100110011110000111000101100011
asciiLogicalArray =
Columns 1 through 29
0 1 1 0 0 0 1 0 1 1 0 0 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 1
Columns 30 through 42
1 0 0 0 1 0 1 1 0 0 0 1 1

2 Comments

my char2bin function works correctly, but for the next function, he wants us to do almost the exact opposite thing, my current issue is in the above answer. Ugh thank you though, the other one does almost exactly what I need it to do, I just need to add an sprintf that incorporates '%08s' to force it to be 8 chars long. Do you have any idea for the other function though? Thanks so much for your help
You can force it to be 8 characters long by using a second argument with your dec2bin call.
From the documentation (for R2014b):
str = dec2bin(d,n) produces a binary representation with at least n bits.

Sign in to comment.

Categories

Asked:

on 24 Nov 2014

Commented:

on 14 Jun 2021

Community Treasure Hunt

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

Start Hunting!