Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

c - convert unsigned int to char alphabets

I have the following code that converts a stream data of 16-bit integer to unsigned 8-bit integer.
I am looking to convert them to alphabetical data values and see what they contain.

#include<stdio.h>
   int main() {
        FILE *fp,*out;
        char buffer[256];
        size_t i = 0;
        fp=fopen("c:/Gosam/input.txt", "rb");
        if(fp != NULL) {
              fread(buffer, sizeof buffer,1, fp);
        }
        out = fopen("c:/Gosam/res.txt", "w");
        if(out != NULL) {
              // buffer = (char*) malloc (sizeof(char)*Size);
              for( i = 0; i < sizeof(buffer); i += 2)
              {
                    const unsigned int var = buffer[i] + 256 * buffer[i + 1];
                    fprintf(out, "%u
", var);
              }
              fclose(out);
        }
        fclose(fp);
    }

The following is the form of my output:

263  4294966987  4294967222  4294967032  64 4294967013  73  4294967004 90  
4294967028  83 4294966975   37  4294966961  5  4294966976   82  4294966942  
4294967022  4294966994 11 4294967024 29 4294966985 4294966986 4294966954 50  
4294966993  4294966974       4294967019 4294967007

This are the values I want to convert to alphabetical characters and see their content.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't know what you expect as an answer (you didn't ask a question), but there seems to be one suspicious thing in your code:

char buffer[256];

Here char means signed char. If your code does manipulations on them (like multiplying by 256), it probably doesn't do what you expect (though I can only guess what you expect - your question doesn't mention it).

Try the following:

unsigned char buffer[256];

Also please ask a question (that is, something with a question mark), and give some examples (input, output).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...