Wednesday, April 22, 2015

How to encode ASCII text to BASE64 without last Line Feed character?

Hi,
I am using gVim 7.4 on Windows 7. I would like to encode ASCII text into BASE64 text.

1. First I have set code page to UTF-8 and file format to unix.
:set encoding=utf-8
:set fencoding=utf-8
:set fileformat=unix

2. Then I have typed in text: Man
This is the sample from: https://en.wikipedia.org/wiki/Base64#Examples
3. Saved to file: :w
4. Converted text from ASCII to BASE64:
:%!base64
and text Man is converted into:
TWFuCg==
This is not correct. Why? The mail logic: three ASCII character should be converted into 4 BASE64 characters, but I get 8 characters as BASE64 output.

Man should be converted into:
TWFu

This can also be verified with: https://www.base64encode.org/ typing Man in top input box and click on Encode.

What I suspect is that Vim is also converting Line Feed character (the last character in the line). Why? Type in:
Man
and execute command:
:%!xxd and output is hex number of Man characters:
4d616e0a

Where the meaning is:
4d=M
61=a
6e=n
0a=Line Feed

Now I suspect that Man+(line_feed_character) is converted from ASCII to base64. This are 4 characters that can't be divided by 3, so two extra = characters are added to get 6 ASCII characters (6 can be divided by 3). Because of logic to get 4 base64 out of 3 input ASCII characters, so formula: 6 * 4 / 3 = 8 characters returned by Vim.

If I am correct and Vim does include Line Feed character when converting text from ASCII to BASE64, then:
a) Is there a way to convert from ASCII to BASE64 without line feed character.
b) Is there a way to remove line feed character from end of line in Vim? So when converted it will not get included into conversion.

Thanks

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: