Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to check the Base64 value for an integer. There is a base64 linux command but I don't understand how I can apply it on integers.

I have tried with base64 10 but then I get the error message base64: 10: No such file or directory

I think that the problem can be that Base64 is used for Binary to Textual conversion, so there is no point to pass a Textual argument to it. So my argument 10 is read as an textual string and not as a binary number. Is there any way I can turn a texttual argument to a binary?

How can I use the base64 command to get the Base64 value for an integer?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Convert the number into hex than use echo to print the according byte sequence and pipe that into base64. So to encode the integer 10 with base64, you can use:

echo -en '\xA' | base64

To explain the result. The byte 10 has the following binary representation:

00001010

What base64 does is, it chunks those into groups of 6 bits. So with padding we get the following two 6lets:

000010 100000

Which in decimal are 2 and 32, which correspond to the letters C and g.

share|improve this answer
    
Thanks, but I think I get wrong result. I get Cg== where C=2 and g=32 according to Wikipedia, why is that? Shouldn't it be K and maybe some padding with ==? And a single number in base 10 should be only one number/letter in base 64. –  Jonas Oct 24 '10 at 16:31
1  
@Jonas: No, that's exactly the right result. See my explanation. –  sepp2k Oct 24 '10 at 16:37
    
Thanks for the explanation. I understand this now. But how can I check the Base64 for a number, without using the binary representation that is used by computers (always 32 bit) e.g. using 4 bits for the number ten and no padding. Maybe this isn't possible with the base64 command. In that way 10 should be K. I tried with echo -en '\b1010' | base64 but it didn't work. –  Jonas Oct 24 '10 at 16:52
    
I mean the base64 implementation referenced as base64url on Wikipedia. But I guess that this isn't available as a command in Bash. So I probably have to find it somewhere or implement it myself. Thanks anyway. –  Jonas Oct 24 '10 at 17:11
    
@Jonas: Yeah, you can't really do it. It's not possible to create a file that's less than one byte big (or write less than one byte to a stream or pass a number that's less than one byte big to a function in C - everything is at least one byte in the computer). What you can do is shift the number two bits to the left and then call base64 on that. This will give you two 6lets the latter of which will be 0 and the former will be the number you want. –  sepp2k Oct 24 '10 at 17:14
show 13 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.