You are here:

C++/String to binary

Advertisement


Question
QUESTION: Hey. I have this in my code:

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;

// checksum of timestamp - you must type in manually, sorry. To be fixed later...)
u8 ts_checksum[16]={ 0x02, 0xE8, 0x37, 0xD0, 0x6B, 0x2A, 0xC7, 0x68, 0x91, 0xF3, 0x88, 0xD9, 0xCC, 0x36, 0xC6, 0x7A };

// encrypted timestamp - you must type in manually, sorry. To be fixed later...)
u8 enc_data[36]={    0x2A, 0x97, 0x85, 0xBF, 0x50, 0x36, 0xC4, 0x5D, 0x38, 0x43, 0x49, 0x0B, 0xF9, 0xC2, 0x28, 0xE8,0xC1, 0x86, 0x53, 0xE1, 0x0C, 0xE5, 0x8D, 0x7F, 0x8E, 0xF1, 0x19, 0xD2, 0xEF, 0x4F, 0x92, 0xB1, 0x80, 0x3B, 0x14, 0x51 };

u8 clear_data[64];
u8 K[16], K1[16], K2[16], K3[16];
u32 T = 1;
RC4_KEY data_key;
u16 uni_pwd[128]={0};
//u32 pass_len = 8;
MD4_CTX ctx1;
MD5_CTX ctx;
u8 k_ipad[65];
u8 k_opad[65];
u8 tk[16];

but the problem is that each time I want to change the contents of the encrypted timestamp or the checksum of the timestamp, I have to type it in manually and recompile the source code. I would like to supply these parameters as  strings, that way I could give them  on the command line as arguments to main.  Someone suggested this code:

the string to binary function is like this:

it converts szHash(string) to binary storing in buffer: hash(binary)
before returning length of bytes
if theres invalid hexadecimal string, it returns zero

size_t getHash(char szHash[], unsigned char hash[])
{
       unsigned int number;
       size_t nHashLen;

       if(((nHashLen = strlen(szHash)) % 2) != 0)
         return(0);

       for(size_t i = 0;i < nHashLen;i++) {
         if(isxdigit(szHash[i]) == 0)
         return(0);
       }

       nHashLen /= 2;

       for(size_t i = 0;i < nHashLen;i++) {
         sscanf(&szHash[i*2],L"%2x",&number);
         hash[i] = (unsigned char)number;
       }
       return(nHashLen);
}

but I couldn’t understand it quite well enough to implement it. I just want to convert a string to this “u8” (I think it is binary). Can you help?


ANSWER: Hello,

getHash's name is misleading because it doesn't get a hash.  It converts a HEX ASCII string to a byte array.  It does what you want.

If getHash is called with "12345678" it will return bytes in hash of { 0x12, 0x34, 0x56, 0x78 } and a length of 4.

Bill

---------- FOLLOW-UP ----------

QUESTION: I know this probably sounds dumb, but could you show me how I would go about implementing this for this particular scenario? I'm not quite sure how to go about making the actual function call. I'm sure that whatever I am not understanding is simple, but I need to see it in demonstration.

ANSWER: Hello,

If you had the following string and output buffer, this is how getHash would be called:

u8 input[] = { "111291A1BC34" };
u8 output[6];  // input has 12 chars so the output will be 6.  In practice, you would use large buffers enough for any input you expect

int len;

len = getHash( input, output );

len will be 6 and output will have the binary values 0x11, 0x12, 0x91, 0xA1, 0xBC, 0x34.

Hope this is clearer,
Bill

---------- FOLLOW-UP ----------

QUESTION: sorry, but I still can't get the string to binary function working. I tried to implement the code you gave me:

#include <string>
#include <ctime>
#include <stdlib.h>
#include <iostream>

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;

size_t getHash(char szHash[], unsigned char hash[])
{
      unsigned int number;
      size_t nHashLen;

      if(((nHashLen = strlen(szHash)) % 2) != 0)
         return(0);

      for(size_t i = 0;i < nHashLen;i++) {
         if(isxdigit(szHash[i]) == 0)
         return(0);
      }

      nHashLen /= 2;

      for(size_t i = 0;i < nHashLen;i++) {
         sscanf(&szHash[i*2],L"%2x",&number);
         hash[i] = (unsigned char)number;
      }
      return(nHashLen);
}
         
int main()
{
   
u8 input[] = { "111291A1BC34" };
u8 output[6];  // input has 12 chars so the output will be 6.  In practice, you would use large buffers enough for any input you expect

int len;

len = getHash( input, output );

//print the hash in hex format
   for (int i = 0; i < 12; i++)
     printf ("%02x", output[i]);
     
return 0;
}

I tried to compile this code in Dev-C++ 4.9.9.2 and Visual C++ 2008 Express Edition.
I get the errors:

'sscanf' : cannot convert parameter 2 from 'const wchar_t [4]' to 'const char *'

'getHash' : cannot convert parameter 1 from 'u8 [13]' to 'char []'

what I want to do is take an MD4 hash on the command line in the format "FC7B71B67E964466CEC486AB12F4B558" and store it in binary u8 md4_hash[16]. I am trying to compile this as .cpp. Does that have anything to do with it? I tried to compile it as C, but got even more errors. Any help would be greatly appreciated. Thanks!

Answer
Hello,

The problem is you're compiling with UNICODE support which enables wide characters.  Use the MS provided wrapper on strings T.  Like T("111291A1BC34").  Where you have u8, use char - it's complaining about unsigned and signed conversions, as well as the UNICODE problem.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.