hex in char arrays :S

This is a discussion on hex in char arrays :S within the C++ Programming forums, part of the General Programming Boards category; I have something like this Code: char something[] = {'\x00', '\x02', '\x05', '\xB4', '\x06', '\x1A', '\xB5', '\x3D', '\xB0', '\x4A', '\x9E', ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    38

    hex in char arrays :S

    I have something like this
    Code:
    char something[] = {'\x00', '\x02', '\x05', '\xB4', '\x06', '\x1A', '\xB5', '\x3D', '\xB0', '\x4A', '\x9E', '\x67', '\x6C', '\xE0', '\x57', '\x4A', '\x61', '\x73', '\x01', '\x00', '\x00', '\x40', '\xC2', '\x18', '\xDE', '\x54', '\x19', '\xDE', '\x35', '\xA8', '\x61', '\x00', '\x00', '\x57', '\x41', '\x43', '\x43', '\x00', '\x41', '\x72', '\x65', '\x73', '\x4C', '\x69', '\x74', '\x65', '\x20', '\x31', '\x2E', '\x38', '\x2E', '\x31', '\x2E', '\x32', '\x39', '\x34', '\x34', '\x00', '\xC0', '\xA8', '\x01', '\x64', '\x18', '\xB8', '\xCA', '\xC1', '\x03', '\x00', '\x04', '\x00'}
    I am using VC++, I tried adding a '\0' to the end it also didnt work. The problem is if i do something simple like char something[] = {'\x39'}; that would assign the ascii character. But when i did that big thing up there and outputted it to some edit box just to check if it assigned correctly it didnt print anything. Please help, thanks

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    To represent hex you must use 0x in front of the hex number.
    So this would represent two hex numbers with the values 0 and 2
    Code:
    	char something[] = {'\0x00', '\0x02'};

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    So i which should i use?
    char Something[] = {'\0x00'};
    char Something[] = {'0x00'};
    char Something[] = {'\x00'};

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by Elyubarov
    So i which should i use?
    char Something[] = {'\0x00'};
    char Something[] = {'0x00'};
    char Something[] = {'\x00'};
    You should use this form
    Code:
    char Something[] = {'0x00'};
    Here are some links that might help.
    The links also give a little info on this form '\x00'
    C++ Integer Constants

    http://msdn.microsoft.com/library/en..._constants.asp

    Character Escapes
    http://msdn.microsoft.com/library/en...terescapes.asp

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    that msdn thing didnt help at all since i cant understand how they explain things, anymawho
    can i do
    char something[] = {'0x00', '0x39'};
    ^as an example, with alot more chars.
    EDIT:
    Why wont this work then?
    char something[] = {'0x44', '0x00', '0x02', '0x05', '0xB4', '0x06', '0x1A', '0xB5', '0x3D', '0xB0', '0x4A', '0x9E', '0x67', '0x6C', '0xE0', '0x57', '0x4A', '0x61', '0x73', '0x00', '0x00', '0x00', '0x40', '0xC2', '0x89', '0xBA', '0xA4', '0x91', '0xC0', '0xE6', '0xA8', '0x61', '0x00', '0x00', '0x57', '0x41', '0x43', '0x43', '0x00', '0x41', '0x72', '0x65', '0x73', '0x4C', '0x69', '0x74', '0x65', '0x20', '0x31', '0x2E', '0x38', '0x2E', '0x31', '0x2E', '0x32', '0x39', '0x34', '0x34', '0x00', '0xC0', '0xA8', '0x01', '0x64', '0x18', '0xB8', '0xCA', '0xC1', '0x03', '0x00', '0x04', '0x00'};

    Its not printing the hex that i specified, i dont know were it is getting it from , ascii of 0x44 is 'D' it is giving me '4'.
    Last edited by Elyubarov; 12-26-2004 at 12:28 AM.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    @Elyubarov: What is it that you want to obtain? Why hex instead of decimal? Are you trying to store the representation or the value?

    @Anubis: Please make sure you test your advice before giving it:
    Code:
    char something[] = {'0x00'};
    produced the following:
    warning: multi-character character constant
    and of course it produced undefined output.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    it doesnt have to be hex, i could use some online thing to convert it to decimal, does that make a difference? [sorry about the double post i got this reply after i posted the other]

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok so you want the values right? Basically it sounds like you want an array of small integers? Is this what you are trying to do?

    Edit:
    Or do you just want to initalize a string using the character's value instead of a string literal?
    Last edited by Thantos; 12-26-2004 at 12:41 AM.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Or do you just want to initalize a string using the character's value instead of a string literal?
    ^that one

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok then
    Code:
    char name[] = "Bob";
    Would be:
    Code:
    char name[] = { 0x42, 0x6f, 0x62, 0x00 };
    Of course this will only work on systems that have the same values for those letters.

    The problem was that you were confusing the representation (using ' ' ) with the value. When you have the representation you use ' ' (or " " in the case of string literals). When you have the value you just put in the value.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    char something[] = {0x44, 0x00, 0x02, 0x05, 0xB4, 0x06, 0x1A, 0xB5, 0x3D, 0xB0, 0x4A, 0x9E, 0x67, 0x6C, 0xE0, 0x57, 0x4A, 0x61, 0x73, 0x00, 0x00, 0x00, 0x40, 0xC2, 0x89, 0xBA, 0xA4, 0x91, 0xC0, 0xE6, 0xA8, 0x61, 0x00, 0x00, 0x57, 0x41, 0x43, 0x43, 0x00, 0x41, 0x72, 0x65, 0x73, 0x4C, 0x69, 0x74, 0x65, 0x20, 0x31, 0x2E, 0x38, 0x2E, 0x31, 0x2E, 0x32, 0x39, 0x34, 0x34, 0x00, 0xC0, 0xA8, 0x01, 0x64, 0x18, 0xB8, 0xCA, 0xC1, 0x03, 0x00, 0x04, 0x00};
    ^why does this only produce the letter 'D'(0x44), and nothing more. by the way, I appreciate all the help!

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Remember that you are working with a null terminated string. Basically as soon as it sees the null character it stops reading the string. The null character has a value of 0. What is the second value in the array?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why does this only produce the letter 'D'(0x44), and nothing more.
    Because you told it to:
    Code:
    char something[] = {0x44, 0x00
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    heh, im a n00b:-p, but I need that 0x00 since Im sending this as a packet and the server at the other end interprets the packet in a strict way, it reads the 0x00 as a 'seperator', so what should i do?

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so what should i 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.
    My best code is written with the delete key.

Page 1 of 3 123 LastLast
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, 04: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, 11:28 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 09:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21