convert

This is a discussion on convert within the C++ Programming forums, part of the General Programming Boards category; How would you convert a const char to a BYTE array?...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    convert

    How would you convert a const char to a BYTE array?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what do you have, a single char? And you want to make it into an array of BYTE?

    Shouldn't require much more than declaring an array with one element and putting the char in that - you may have to cast the char, depending on the declaration of BYTE (it may be an unsigned char).

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    example:

    (something like this)

    now listens

    to

    0x77 0x20 0x6C 0x69 0x73 0x74 0x65 0x6E 0x73

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure your new comment actually adds anything useful to your previous question.

    Are you asking how to print "w listens" as "0x77 0x20 ..."?

    Using printf("0x%x", some_char); will do that..

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    This seems to have done it...

    Code:
    	BYTE *bArray;
    
    	for ( int x = 0; x < iLength; x++) 
    	{
    		bArray[x] = szBuf[x];
    
    		Log("[Packet]: 0x%X [Array]: 0x%X", szBuf[x], bArray[x]);
    	}
    Thanks.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I hope you are allocating some memory for bArray to point to...

    --
    Mats

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
    	for ( int x = 0; x < iLength; x++) 
    	{
    		bArray[x] = szBuf[x];
    	}
    This loop maybe better to replace with one call to memcpy wich probably will do the same work faster...
    Logging - you'll need to do as it is
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Convert to title case
    By gertie in forum C Programming
    Replies: 18
    Last Post: 07-06-2008, 10:58 PM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM

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