Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

The command below converts a decimal integer to radix 64. It does not use the Base64 encoding scheme, but it uses the characters used by the URL-safe variant of the Base64 encoding scheme as digits.

awk 'NR==FNR{a[NR-1]=$0;next}{if($0==0){print"A";next}o="";for(n=$0;n!=0;n=int(n/64))o=a[n%64]o;print o}' <(printf %s\\n {A..Z} {a..z} {0..9} - _) -

The command below does the same in Bash. It also works with Zsh if you replace l%64 with l%64+1.

unset a;a=({A..Z} {a..z} {0..9} - _);while IFS= read l;do if ((l==0));then echo A;continue;fi;o=;for ((;l!=0;l/=64));do o=${a[l%64]}$o;done;printf %s\\n "$o";done

This converts radix 64 to decimal:

awk 'NR==FNR{a[$0]=NR-1;next}{o=0;for(i=NF;i>=1;i--)o+=a[$i]*64^(NF-i);print o}' FS= <(printf %s\\n {A..Z} {a..z} {0..9} - _) -

This does the same in Bash:

unset a;i=0;declare -A a;for x in {A..Z} {a..z} {0..9} - _;do a[$x]=$((i++));done;while IFS= read l;do o=0;for((i=${#l};i>=1;i--));do let o+=${a[${l:i-1:1}]}*64**(${#l}-i);done;echo "$o";done

The awk commands result in loss of precision with 2**53+1 and larger numbers, but to avoid it you can add -M (--bignum) in gawk 4.1 and later.

The Bash commands result in integer overflow with 2**63 and larger numbers.

share|improve this answer

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.