ZAky
ZAky

Reputation: 1307

How to convert binary to bytes in bash

How to convert the following go code to bash

    data, _ := base64.StdEncoding.DecodeString("nJpGBA==")
    fmt.Println(data)

    //Output
    [156 154 70 4]

I got up to here

    echo nJpGBA== |base64 -d 

https://play.golang.org/p/OfyztKQINg9

Upvotes: 0

Views: 997

Answers (1)

Ralf
Ralf

Reputation: 1813

Not a exact match, but:

echo nJpGBA== |base64 -d  | od -A n -t u1

Output: 156 154 70 4

Note leading space and multiple spaces between.

Other solution. Assign it to an array:

val_array=( $(echo nJpGBA== |base64 -d  | od -A n -t u1) )
echo "${val_array[@]}"

Output: 156 154 70 4

The command od dumps any binary files, by default in octal values. Here it reads from stdin, as no file is given.

  • -A n suppresses the output of byte addresses
  • -t u1 prints one byte unsigned decimals

Upvotes: 3

Related Questions