-4

Thanks in advance

I'm wondering how in C/C++ I would achieve something such as:

ASCII Character Array: enter image description here

Convert that into a BYTE Array of: enter image description here

Resulting in a 0x10 array instead of 0x20.

Cheers

Snazzy
  • 35
  • 5
  • You mean how you can interpret the hex ASCII representations as plain values? – πάντα ῥεῖ Mar 28 '16 at 15:25
  • Yh.......... :> I looked around a while before asking but couldn't find anything. – Snazzy Mar 28 '16 at 15:27
  • Actually the first dump you show is a dump of a hex code and you want to encode the hex digits into binary nibbles. That's basically what your algorithm reduces to: How do you encode a hex digit into a binary nibble and where do you place the nibble (high or low)? – ikrabbe Mar 28 '16 at 15:28
  • @TheUnknownCoder `std::istringstream` and the `std::hex` I/O manipulator might come in handy. – πάντα ῥεῖ Mar 28 '16 at 15:30
  • If we narrow it down to 1 Hex byte if I take the first hex byte as an example I want to convert the "A5" ASCII into 0xA5 so if that's what you mean yes. – Snazzy Mar 28 '16 at 15:32
  • @TheUnknownCoder Again, the mentioned standard classes might come in handy to do so. – πάντα ῥεῖ Mar 28 '16 at 15:35
  • @πάνταῥεῖ The thing is the system I am working on doesn't seem to like the lib so if their was a way to avoid using it, it would be best. – Snazzy Mar 28 '16 at 15:37
  • @TheUnknownCoder You probably don't want to ask for c++ solutions then. However the duplicate link provided, doesn't need the classes I mentioned. – πάντα ῥεῖ Mar 28 '16 at 15:38
  • There is no language C/C++, only the two **different** languages C and C++! Pick the one you use. – too honest for this site Mar 28 '16 at 16:05

1 Answers1

-1

Encoding a hex digit

#define HEX2BIN(D) ((('0' <= D) && (D <= '9')) ? D-'0' : (('a' <= D) && (D <= 'f')) ? D-'a'+10 : (('A' <= D) && (D <= 'F')) ? D-'A'+10 : -1)

and place it high or low

#define NIBBLE2BYTE(HI,BYTE,NIBBLE) do { if (HI) { BYTE = ((BYTE & 0xf0) | NIBBLE<<4); HI = 0; } else { BYTE = ((BYTE & 0x0f) | NIBBLE); HI=1; } } while (0)

. Now you can loop over your input and output with HI=1 initially. Test for HEX2BIN(D) > 0 to catch input errors!

ikrabbe
  • 1,909
  • 12
  • 25
  • Never use a macro if it can be done the same way with a function. – too honest for this site Mar 28 '16 at 15:42
  • 1
    I just wanted to show the concept. Everyone who has learned his first lessons in C programming should be able to do this encoding in nearly anything, let it be a function, a macro, a bit of assembler code or just some plain loop directly written into the code and macros can be very useful for such simple things. – ikrabbe Mar 28 '16 at 15:44
  • 1
    You are effectively teaching bad (and error-prone) coding style. This is read by beginners, who will take this for the normal way. You can show this also with a function. Not to forget using all-uppercase is commonly used for macros and _enum-constants_. – too honest for this site Mar 28 '16 at 15:46
  • Macros have their own right to exist and HEX2BIN is exactly what macros can be good for. If you implement such things as a function you will get even more complexity for the parameter transfer. Using C++ with exceptions will be much more error prone in the end. Learning to deal with macros can be very useful, though the younger programmes don't seem to like them. Possibly because they all got confused by managed programming languages. – ikrabbe Mar 28 '16 at 15:57
  • Nonsense! I did not say macros are useless! Just the usage you show here has become obsolete 17 years ago with C99. A modern compiler will inline functions like that with local linkage. And you can explicitly use an `inline` function, which will be integrated just like the macro code. Oh, and I'm definitvely not a "younger programmer" just an older one continously learning new techniques if they improve code quality. – too honest for this site Mar 28 '16 at 16:03
  • If this is not a use case for a macro, what else? I'm not talking about inlining of functions but of the parameter transfer. You would have to fiddle with `unsigned char*` and/or `int` here, which makes things even more complex. Post a better solution if you think you can. – ikrabbe Mar 28 '16 at 16:08
  • Let alone the fact there are standard functions to be used for this conversion, you just don't show how you intend this to be used in comparison to a function. Not sure what you problem is with passing a `char` *`, etc., you have to use this in a function anyway. I really wonder what your problem with a function here is. Feel free to elaborate by providing sample code for both and pointing out why the macro is superiour. – too honest for this site Mar 28 '16 at 16:12
  • try to implement it as a function and you will see. – ikrabbe Mar 28 '16 at 16:18
  • Oh, I already have! In various languages, inlcuding Assembler and C in the last decades. – too honest for this site Mar 28 '16 at 16:23