View Full Version : Question about C programming
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"?
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.
Perfect. Just what I needed. Thank you very much.
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?
kingdj333
2009-08-14, 08:22 PM
O_o, english plox.
you guys use numbers to much. And sometimes letters.
Russt
2009-08-14, 08:26 PM
^ There's only one number posted in this thread, lol.
This isn't a thread about English :wink:
Turtally
2009-08-14, 08:28 PM
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?
Nevermind. I just figured it out.
Heidi
2009-08-14, 09:37 PM
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
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.
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
Powered by vBulletin® Version 4.1.10 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.