Click here to Skip to main content
16,017,167 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys.
How can I print a null character in the middle of a string and how to tell the program that this is not the null character of string's end?

I want to print characters of a string one by one, but when it reaches to null in the middle, it quits.
I receive a non-printable character and pass it to a function to print the hex value of that non-printable char.

What I have tried:

char	i;
	char	j;

    char chars[] = "0123456789abcdef";
	i = chars[c / 16];
	j = chars[c % 16];
	write (1, "\\", 1);
	write (1, &i, 1);
	write (1, &j, 1);
Posted
Updated 12-Feb-23 8:18am
Comments
Richard MacCutchan 12-Feb-23 3:09am    
Where is the code thar looks for the null character?
Navid Rigi Ebrahimi 12-Feb-23 3:16am    
int main()
{
char str[] = "Coucou\rtu vas bien ?";
str[6] = 0;
printf("%zu\n", sizeof(str));
ft_putstr_non_printable(str);
write (1, "\n", 1);
}

void ft_putstr_non_printable(char *str)
{
int i;
// int len;
char *chars;

i = 0;
chars = "0123456789abcdef";
// len = ft_strlen(str);
printf("%zu\n", sizeof(str));
while (str[i] != '\0')
{
if (str[i] >= 32 && str[i] <= 126)
write (1, &str[i], 1);
else
calcs(str[i], chars);
i++;
}
}

void calcs(char c, char *chars)
{
char i;
char j;

i = chars[c / 16];
j = chars[c % 16];
write (1, "\\", 1);
write (1, &i, 1);
write (1, &j, 1);
}
Richard MacCutchan 12-Feb-23 3:44am    
The problem is that the while loop will see the null character first, and terminate the loop. But even if you do check and print it, you do not know if it is in the middle of the string, or the actual string terminator. So if you do capture it and print the hex value, your code could then run on into unassigned memory.
Navid Rigi Ebrahimi 12-Feb-23 4:01am    
Yeah, exactly but I have no idea how to fix it. Even sizeof doesn't work because first one refers to array but second one to pointer.
Richard MacCutchan 12-Feb-23 4:25am    
You need to pass the size of the string to the function. But you will always have this problem if you treat arrays of characters as strings.

Pretty much, you can't - in C, a null character is considered a string terminator - encountering one marks the end of the string.

If you want to include nulls in your data, you should be using unsigned char datatypes, and not trying to use "string based shortcuts" like this:
char chars[] = "0123456789abcdef";
as it adds an addition null to the end of the array.

I'd probably recommend using #define to make it obvious:
C
#define byte unsigned char
...
   byte data[noOfBytesNeeded];
Then treat your data as an array of values and print each as a character separately yourself instead of type to con the system into thinking it's a string.

But do be aware that a null character '\0' will print as nothing at all - not even a gap or space between the character before and after as it is defined by the C specification as a Control Character and thus unprintable! The only time you will be able to "see" it is if you print it to a file and use a hex editor to examine the file data.

What are you trying to do that you think you need this?
 
Share this answer
 
Comments
Navid Rigi Ebrahimi 12-Feb-23 3:49am    
I want to print non-printable chars in the middle of string. It's in a question I'm solving right now.
OriginalGriff 12-Feb-23 4:41am    
What as? They are control characters, they don't have a "shape" in any font!
And without a shape, you can't "print" them!

Have a look at a hex editor that provides a "text" visualization as well - PSPad in HEx Edit mode does that for example - and it will replace all control codes with a "filler character" like '.', or perhaps '█', or maybe '□' just to space it correctly.

Why do you want to print control characters? They are there to control output: add a new line, or a tab for example.
Navid Rigi Ebrahimi 12-Feb-23 5:06am    
All you said is correct and I know it, but as I said, it's in a question. It asks us to print those control characters.
OriginalGriff 12-Feb-23 6:07am    
But we can't read your assignment, so we have no idea what the question setter expects to see when you do!

Read the question again - it probably contains some info you missed ...
Navid Rigi Ebrahimi 12-Feb-23 6:57am    
Allowed functions : write
• Create a function that displays a string of characters onscreen. If this string contains characters that aren’t printable, they’ll have to be displayed in the shape of
hexadecimals (lowercase), preceeded by a "backslash".
• For example :
Coucou\ntu vas bien ?
• The function should display :
Coucou\0atu vas bien ?
• Here’s how it should be prototyped :
void ft_putstr_non_printable(char *str);
You should write a function accepting an array of char (or, better, unsigned char), say a, and its size, say size.
In the function, implement a loop, with n=0,1,..,(size-1):
  • If a[n] is printable (see isprint[^]) then print it (using, for instance putchar[^]).
  • On the other hand, if the a[n] then print, as requested, its hex value prefixed by the backslash, i.e.
    C
    printf("\\%02x", a[n]);

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900