You are here:

C++/string to binary - wrappers?

Advertisement


Question
Alright. I converted u8 to char for input and eliminated the second error. But I didn't understand the part about the MS provided wrapper. Sorry I'm making you spell the whole thing out, but I'm a self-taught programmer and never used wrappers before. Here is the modified code:

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

using namespace std;

typedef unsigned char u8;

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()
{

char input[] = "111291A1BC34";    
u8 output[6];

int len;

len = getHash( input, output );

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


and the only error I get is:

string2binary.cpp In function `size_t getHash(char*, unsigned char*)':

37 string2binary.cpp cannot convert `const wchar_t*' to `const char*' for argument `2' to `int sscanf(const char*, const char*, ...)'

which you said was due to UNICODE and a wrapper could somehow fix? Perhaps you could demonstrate how to use the wrapper? I tried to experiment, but I'm lost.

One last question- if the wrapper is MS-provided, does that mean I can't compile the code under g++? Do I lose Linux compatibility? If so, is there any other way to may this work? I would like to keep maximum portability, if possible.

Thank you!

Answer
Hi,

The wrapper so to speak is a macro T which takes a string and makes it with normal or wide characters depending on compiler settings for the project.  So T("12345ABCD") compiles correctly (in MS) when the project is set for normal or UNICODE (wide char) strings.

You would lose portability to use the T wrapper, but it's easy to correct:

#define T(x) x

Oh, I didn't see the L in the sscanf call.  This is what the compiler doesn't like.  You're telling it to use a wide char string.  Take the L off of "%2x".  Sorry I missed that.  You might then be able to just ignore the T macro wrapper discussion.

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.