Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about C programming
#1
I have an "unsigned char* unpackedZlibBlock;" string, but I want to be able to read values from that string (ints, doubles, floats). So let's say that the first four bytes of this string is supposed to be interpreted as an integer. What would be the best way to fetch those four bytes from the string? "strncpy"? "memcpy"?
Reply
#2
Fiel Wrote:I have an "unsigned char* unpackedZlibBlock;" string, but I want to be able to read values from that string (ints, doubles, floats). So let's say that the first four bytes of this string is supposed to be interpreted as an integer. What would be the best way to fetch those four bytes from the string? "strncpy"? "memcpy"?
unsigned char* unpackedZlibBlock = getUnpackedZlibBlock();
unsigned char* zlibBlockPos = unpackedZlibBlock;
unsigned int numberOfChinchillas = *((unsigned int *) (zlibBlockPos));
zlibBlockPos += 4;

You can just cast the unsigned char pointer to an unsigned int (or whatever) pointer.
Reply
#3
Perfect. Just what I needed. Thank you very much.
Reply
#4
I have another question and I didn't want to bother making a new topic for it:

if I have 'a' assigned to "char foo", what command can I give such that "char* bar" equals "aaaaaa"? How do I copy strings like that without using a loop?
Reply
#5
O_o, english plox.
you guys use numbers to much. And sometimes letters.
Reply
#6
^ There's only one number posted in this thread, lol.
This isn't a thread about English Wink
Reply
#7
kingdj333 Wrote:O_o, english plox.
you guys use numbers to much. And sometimes letters.

Isn't the whole point of this thread to discuss a different language?
Reply
#8
Nevermind. I just figured it out.
Reply
#9
kingdj333 Wrote:O_o, english plox.
you guys use numbers to much. And sometimes letters.

So you ask for english, then say that they sometimes use letters too much? Isn't english made up of letters?

Seriously though, if you read the thread title it says "Programming". SURELY you would have at least a small idea what that is, and if not you probably could have realised by seeing this thread/ other threads with programming in them.

And the only number I can see is "zlibBlockPos += 4 o_o
Reply
#10
lol it's a C programming thread, if you can't tell a char* from an operator just don't bother posting.

I'm interested in what the solution was to your second question, Fiel.
Reply
#11
Well, it's a cheating way of doing it.

Since I'll eventually be using the char array to write to a file, just do this...

fwrite(myCharArray, 1, 7, MyFile);
fseek(MyFile, -7, SEEK_CUR);
//define a new char type, then fread it
Reply


Forum Jump:


Users browsing this thread: