#trying to make a hexadecimal reader, is there no padding option for %02X?

83 messages · Page 1 of 1 (latest)

queen magnetBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

latent hazel
#

is there a %02X that does no padding?
%X

severe yoke
#

i dont think my scanf is reading the next two characters after the three

#

and only reads the one

#

but it works in line 2,3 and 6?

#

so im confused

#

also commented the code

latent hazel
#

what are you trying to do

#

what format is the input

#

and what format do you want to convert it to

severe yoke
#

so say i have like

#

some characters

#

like 23fdb

#

all those are only once so just print as normal

#

but say 2 is repeated 3 times

#

it will always have a hexademical after it

#

222063fdb

#

for example

#

so 17___05

#

should turn into _____

latent hazel
#

so its a form of run length encoding ?

severe yoke
#

yes

latent hazel
#

!f

queen magnetBOT
#

is there a %02X that does no padding?

`

void expand() {
  // Setup
  char character, last;
  int count;

  // While there is no break condition
  while (1) {
    // Gets the character in the stream
    character = (char)getchar();
    // Checks if 3 characters are the same to start reading hexadecimal value
    if (character == (last = (char)getchar()) && character == (char)getchar()) {
      // reads the input as a hexadecimal format, stores it in count
      scanf("%X", &count);
      // Prints the character for count times by iteration
      for (int i = 0; i < count; i++) {
        // Prints character
        putchar(character);
      }
    } else {
      // Prints character if under 3
      putchar(character);
      if (last != '\xff') {
        putchar(last);
      }
    }
    if (character == '\xff')
      break;
  }
}

`

this is my code, and it works only in some places as shown in the images. For context this is uncompressing text by reading 3 values then reads the two hexadecimal values after. any help would be appreciated, thank you.

Kae
latent hazel
#

!f

queen magnetBOT
#

void expand() {
  // Setup
  char character, last;
  int count;

  // While there is no break condition
  while (1) {
    // Gets the character in the stream
    character = (char)getchar();
    // Checks if 3 characters are the same to start reading hexadecimal value
    if (character == (last = (char)getchar()) && character == (char)getchar()) {
      // reads the input as a hexadecimal format, stores it in count
      scanf("%X", &count);
      // Prints the character for count times by iteration
      for (int i = 0; i < count; i++) {
        // Prints character
        putchar(character);
      }
    } else {
      // Prints character if under 3
      putchar(character);
      if (last != '\xff') {
        putchar(last);
      }
    }
    if (character == '\xff')
      break;
  }
}

nekro
severe yoke
#

thats much nicer to look at i didnt know i could do that lmao

latent hazel
#

!code

queen magnetBOT
#
How to Format Code on Discord
Markup

```c
int main() {}
```

Result
int main() {}
latent hazel
#

do you have a specification of the format ?

severe yoke
#

how do you mean?

#

like how it needs to be output?

latent hazel
#

how to read it

severe yoke
#

no

#

i dont need to do stream input if i dont want to but he said it needs to be efficient

#

i have working code for a compression to that format but cant get this reverse function to work for some reason

latent hazel
#

when do you know a character needs to be repeated n times ?

severe yoke
#

when its repeated 3 times

latent hazel
#

and what determines the n ?

#

111 how often is 1 repeated ?

severe yoke
#

so say if its 11105

#

the output should be 11111

latent hazel
#

i see

#

does the input have newlines ?

severe yoke
#

im not quite sure what counts as a newline but

latent hazel
#

\n or Return

severe yoke
#

no newlines no

#

it keeps reading until no characters are given

#

though im not sure if what might be messing it up is

#

i have the firstline as either C or E

#

then it calls the function

latent hazel
#

if (character == (last = (char)getchar()) && character == (char)getchar()) {
that looks quite sus to me

#

you could like

#
char c0 = getchar();
char c1 = getchar();
if (c0 == c1) {
    char c2 = getchar();
    if (c1 == c2) {
        parse hex
        print c0 n times
    } else {
        ungetc(c2, stdin);
        ungetc(c1, stdin);
    }
} else {
    ungetc(c1, stdin);
}
print c0
#

ungetc puts the read character back

latent hazel
#

when does it tell you to use a newline ?

latent hazel
severe yoke
#

i see

#

then yes i think so

#

wait got confused mid way lmao

#

there bit easier on eyes i thinks

#

OK FIXED IT

#

YUPE

#

@latent hazel thank you a lot :3

queen magnetBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

median shell
#

oh it's solved already. nevermind 🙂

latent hazel
severe yoke
median shell
severe yoke
#

thank you for pointing that out

#

it helps as im vvv new to c

median shell
severe yoke
#

not sure if youre still around

#

at the end of my output i dont think it breaks properly

#

not quite sure what would be causing it

#

since it checks if c0 == '\xff' it breaks