toNameValuePairs :: [ (String, String) ] -> String
toNameValuePairs (x:xs) = toNameValuePair x ++ toNameValuePairs xs
    where
        toNameValuePair (name, value) = 
                (showLength . length $ name) ++ (showLength . length $ value)
                ++ name ++ value
            where
                showLength :: Int -> String
                showLength num 
                    | num <= 127 = [chr num]
                    | otherwise = map chr . map (.&. 0xff) $
                        [ clearBit (shiftR num 24) 7
                        , shiftR num 16
                        , shiftR num 8
                        , num 
                        ]
toNameValuePairs :: [ (String, String) ] -> String
toNameValuePairs (x:xs) = toNameValuePair x ++ toNameValuePairs xs
    where
        toNameValuePair (name, value) = 
                (showLength . length $ name) ++ (showLength . length $ value)
                ++ name ++ value
            where
                showLength :: Int -> String
                showLength num 
                    | num <= 127 = [chr num]
                    | otherwise = map chr . map (.&. 0xff) $
                        [ clearBit (shiftR num 24) 7
                        , shiftR num 16
                        , shiftR num 8
                        , num 
                        ]
toNameValuePairs (x:xs) = toNameValuePair x ++ toNameValuePairs xs
    where
        toNameValuePair (name, value) = 
                (showLength . length $ name) ++ (showLength . length $ value)
                ++ name ++ value
            where
                showLength :: Int -> String
                showLength num 
                    | num <= 127 = [chr num]
                    | otherwise = map chr . map (.&. 0xff) $
                        [ clearBit (shiftR num 24) 7
                        , shiftR num 16
                        , shiftR num 8
                        , num 
                        ]