/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_putstr_non_printable.c                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/06 13:56:27 by jayang            #+#    #+#             */
/*   Updated: 2022/02/06 17:12:08 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>

void	ft_putchar(char input)
{
	write(1, &input, 1);
}

void	change_hex(unsigned char element_str)
{
	char	*c_hex;
	char	hex[3];

	c_hex = "0123456789abcdef";
	hex[0] = '\\\\';
	hex[1] = c_hex[element_str / 16];
	hex[2] = c_hex[element_str % 16];

	write(1, hex, 3);
}

void	ft_putstr_non_printable(char *str)
{
	int	i;

	i = 0;
	while (str[i])
	{
		if (' ' <= str[i] && str[i] <= '~')
			ft_putchar(str[i]);
		else
			change_hex((unsigned char)str[i]);
		i++;
	}
}