Thread: hex in char arrays :S

  1. #16
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    I did not make the server so i cant change what packets it accepts, I am not sending this in hex because i need the length i would just use the strlen() operator. I am sending it in hex because its some pretty weird ascii chars and i dont think vc++ would accept them, can i do it in a string literal with ascii chars(that are weird)?

  2. #17
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Using a string literal would not solve your problem. The null character is there, and that's all there is to it. Prelude's advice is right on. The only way you're going to do this without changing what the servers excpects as input is to use functions that don't work with null-terminated strings. There're a number of different types of strings. The one Prelude is suggesting is quite easy to work with. It's just an array of chars, and a separate integer storing the number of elements in that array.

  3. #18
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    uhmm can you give me an example, i dont understand what your getting at if i try:
    char something[0] = "a";
    char something[1] = "b";
    I get a redefinition error.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It's as simple as:
    Code:
    char something[] = "string literal";
    But you don't seem to be paying attention to what we tell you if you're still going on about using a string literal instead of an initialization list.
    My best code is written with the delete key.

  5. #20
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Stop confusing me, what should i use? :'(

  6. #21
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    That's because when you declare an array, you need to declare all of the elements

    Code:
    char something[2]
    and then work with individual ones

    Code:
    something[0] = 'a'
    something[1] = 'b'
    You'd probably also run into problems using double quotes (") for a char. Double quotes are for strings. Use single quotes.

    An example:
    Code:
    int length = 80;
    char string[length];
    int i = 0;
    while( i < length) string[i] = '\0';
    This code creates a string of 80 null characters without a problem. The problem comes when you use standard output functions to send this string, as they will stop at the first null character. This is why you need to read Prelude's post. Her solution works, and is probably the easiest one.

  7. #22
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Stop confusing me, what should i use? :'(
    We're not confusing you. We're trying to help you. You've been told exactly what you need to do.

    Stop trying to use a C-style string and figure out another way to determine the length. For example, send the size of the string first, then the string itself, and don't try to use functions that expect a null terminated string.
    Beyond this, that's all we can tell you, because to find a complete solution and tell you what functions / APIs to use, we'd need to know the whole situation, and you're unlikely to find someone willing to take the time to do this. Can you contact the server administrator?

  8. #23
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    I have char something[50]; then
    char something[0] = 'a';
    and i get : error C2466: cannot allocate an array of constant size 0 and
    error C2075: 'something' : array initialization needs curly braces and
    error C2369: 'something' : redefinition; different subscripts

  9. #24
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    char something[0] = 'a';
    Remove the word char. You're not declaring it, you're defining it.

    Your first, second and probably third errors will be solved by this, because it thinks you're declaing something, and you can't declare and array of size 0.

    It appears to me that you have not yet mastered all of the basics of C++. Perhaps you should write some simpler programs before attempting networking and complicating Windows programming, as you have been doing.

  10. #25
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    still when i have something and do something[5] = ' ', the hex of " " is 0x20 not 0x00

  11. #26
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If you want to represent 0x00, you use '\0'. The space is represented as a different ASCII character. Apparently as 20 hex.

  12. #27
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    '\0' terminates the string.

  13. #28
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    So does 0x00. They're the same thing.

  14. #29
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    I want it to not terminate....

  15. #30
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    How many times have we told you? Use functions that use an array of chars and a variable holding the array length, instead of functions that use a null character to terminate the string!



    edit: Unless someone here can already suggest an alternate string API, do a search on google for string API's. They should advertize the fact that they use this alternate method.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 07:05 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM