|
I've got a 64 character string containing hex digits ("aaffb0b" etc) that I want to convert to a string of decimal digits. Can anyone suggest a way to do this without handcrafting my own brutal loop, full of horrible arithmetic mistakes?
|
# ? May 20, 2009 05:04 |
|
|
# ? Jun 7, 2024 08:19 |
|
awesmoe posted:I've got a 64 character string containing hex digits ("aaffb0b" etc) that I want to convert to a string of decimal digits. Can anyone suggest a way to do this without handcrafting my own brutal loop, full of horrible arithmetic mistakes? Use an arbitrary-precision integer library like GMP, maybe. I'm curious why you need to convert a big hexadecimal number into a big decimal number, though.
|
# ? May 20, 2009 06:50 |
|
rjmccall posted:Use an arbitrary-precision integer library like GMP, maybe. Thanks, I was looking at GMP, or maybe using crypto++ (which we already use in other products), but just thought I'd see if I was missing something. quote:I'm curious why you need to convert a big hexadecimal number into a big decimal number, though. Because we've got a retarded customer who's willing to pay an arm and a leg for it, is the short answer The detailed answer is to convert a bloody-great message identifier block from the one format to the other (and back), while maintaining compatibility with existing services which use it in hex yada yada yada. Nothing very exciting.
|
# ? May 20, 2009 07:05 |
|
Avenging Dentist posted:Let's not forget system("pause");
|
# ? May 20, 2009 16:48 |
|
Dijkstracula posted:Well, if you know of a better way to set breakpoints I'd love to hear it MSVC: DebugBreak() or __debugbreak() or __asm int 3 GCC: asm volatile ("int3")
|
# ? May 20, 2009 17:02 |
|
Having an odd template specialization problem. Probably something really basic or stupid I'm overlooking, knowing me. I've got code that looks like this (leaving out all the unrelated stuff and with made up names): pre:A.hpp class A { public: // blah template <class T> void setValue(const T& val); }; template <class T> void A::setValue(const T& val) { // do poo poo with numeric T } A.cpp #include "A.hpp" #include "B.hpp" #include <string> template <> void A::setValue<std::string>(const std::string& val) { // for non-numeric val **** B b; b.doThings<std::string>(val); } B.hpp class B { public: template <class T> void doThings(const T& data); }; template <class T> void B::doThings(const T& data) { // do something with numeric data } B.cpp #include "B.hpp" #include <string> template <> void B::doThings<std::string>(const std::string& data) { // do something with string data }
|
# ? May 20, 2009 17:26 |
|
The explicit specializations need to be in the header, too. Until C++0x.
|
# ? May 20, 2009 17:28 |
|
Thanks. The rules for using templates are so flighty to me sometimes
|
# ? May 20, 2009 17:40 |
|
The rule is extremely simple: if file A uses template B, then all information relevant to file A's use of template B must be available to the compiler when it's compiling file A. In C++, that means it needs to be either in file A or in some file included (perhaps indirectly) by file A. Since B.cpp doesn't include A.cpp, the compiler doesn't know about the template specialization there, so it resolves the template to the only thing it can see, i.e. the general template. For the specific case of fully-specialized function templates, you may be able to get away with simply a declaration of the template specialization in the header file. I don't care to check whether it's specified behavior, but it works in G++.
|
# ? May 20, 2009 18:22 |
|
Why would you even fully specialize a function template? That's what overloads are for. Also, technically the C++ standard doesn't allow explicit specializations of member function templates of (non-template) classes, though this is probably an accidental omission. EDIT: for the pedant ISO/IEC 14882:2003 §14.7.3 ¶6 posted:If a template, a member template or the member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required. For even further reading, here's a discussion on how overloading and specialization of function templates interact: http://www.gotw.ca/publications/mill17.htm Avenging Dentist fucked around with this message at 18:48 on May 20, 2009 |
# ? May 20, 2009 18:24 |
|
I've been tasked with writing some code to read in Joint Variable Message Format (JVMF) messages. JVMF is notable for not having non byte-aligned fields (for example, the header's version number is 4 bits, the "year" part of the time stamp is 7 bits), and since I don't have supreme confidence in my C++ expertise, I'd like some feedback on the possible approaches I was going to use:
For this project, I'm able to hardcode length at compile time without issue. Basically, I'm wondering if either of these libraries seem like a could candidate for any JVMF message class I end up making. I don't expect people to read this to answer my question, but if you're curious: Link to the MIL-STD-6017 spec
|
# ? May 20, 2009 18:30 |
|
Avenging Dentist posted:
RUN GOB'S PROGRAM [Y/N]? Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Penus Really, though, if I declare myArray[2], is it going to have [0], [1], and [2]. Or just 0 and 1? I'm going to post the source code for my lovely program that I was talking about before. A couple things: the windows.h comment is because the compiler I'm using on my laptop is being a dick and is lagging when I try to compile with it. Also, the comment before printMap is just me musing a bit, because if I use one big cout instead of 25 of them, it will go faster when I print out larger arrays I guess. I'll post the code first, then the output: code:
Written 0 to 0,0 Written 0 to 1,0 ... Written 0 to 4,4 Written 1 to 0,2 00100 00000 00000 00000 01000 printMap has been run! Why is that loving 1 at 4,1? The problem has to be at the printMap function.
|
# ? May 20, 2009 18:42 |
|
Lurchington posted:JVMF is notable for not having non byte-aligned fields (for example, the header's version number is 4 bits, the "year" part of the time stamp is 7 bits), and since I don't have supreme confidence in my C++ expertise, I'd like some feedback on the possible approaches I was going to use: Use neither. It's just a little bit of bit-shifting, and using bitsets to store multi-bit fields isn't going to give you any benefit unless the sequence of all the fields doesn't fit into an integral datatype (i.e. is greater than 64 bits). Also, why would you use a dynamic bitset on statically-sized data?
|
# ? May 20, 2009 18:43 |
|
androo posted:Really, though, if I declare myArray[2], is it going to have [0], [1], and [2]. Or just 0 and 1? It's the length of the array, not the max valid index. Indexes start at 0, so for length 2 you'll have just 0 and 1. Seriously, you could have looked that up in any of a thousand references instead of assuming against all evidence (and counter to several posts here) that it would work the way you thought for the last however many days.
|
# ? May 20, 2009 18:49 |
|
rjmccall posted:It's the length of the array, not the max valid index. Indexes start at 0, so for length 2 you'll have just 0 and 1. I figured it might have been why my program was doing that weird poo poo with the last row, so I just wanted to clarify. Really, I'd like to figure out why the program is making that output, I haven't been able to figure it out EDIT: Hmph, after playing around with the max size of the array vs the "resolution" that printMap sees, it seems to be working. fart barterer fucked around with this message at 18:58 on May 20, 2009 |
# ? May 20, 2009 18:53 |
|
androo posted:Really, though, if I declare myArray[2], is it going to have [0], [1], and [2]. Or just 0 and 1? I'm going to post the source code for my lovely program that I was talking about before. Also, please stop using so many globals (especially globals with single-letter names). Lexical scoping is there for a reason.
|
# ? May 20, 2009 18:55 |
|
Avenging Dentist posted:Use neither. It's just a little bit of bit-shifting, and using bitsets to store multi-bit fields isn't going to give you any benefit unless the sequence of all the fields doesn't fit into an integral datatype (i.e. is greater than 64 bits). The JVMF header alone has about 20% of the fields with lengths in excess of 64 bits (names of units, cryptographic keys/data/etc.*) and it seems like it'd be beneficial to have all the fields as the same unit template. If that's not really a benefit then I'll need to reconsider some approaches. As for your second point, if I stayed with hardcoded lengths for each message type (probably 70-80 different ones would be relevant) I'd end up with a class for each one, which would be ok, but I'd also toyed with specifying a message using a resource file (xml or similar) and that message being dynamically instantiated on program start. This would primarily be about saving the compile time hit if I was just going to add support to another message type. Which isn't a huge deal, but if I got feedback on boost's dynamic bitset library being a breeze to use, I'd consider looking into harder. * lengths in question (repeated several times)
these are all byte-aligned, so that's a bonus Lurchington fucked around with this message at 19:00 on May 20, 2009 |
# ? May 20, 2009 18:55 |
|
Lurchington posted:Really, you should just make your own class that has a better interface. One that works like I/O streams on the bit level, for instance. (Also, I rather doubt that name fields are unaligned, but I'm drat sure not going to read the spec.)
|
# ? May 20, 2009 18:57 |
|
Avenging Dentist posted:Also, please stop using so many globals (especially globals with single-letter names). Lexical scoping is there for a reason. Yeah, I know that's pretty dangerous, but it's working for now since only one function is even accessing them at once.
|
# ? May 20, 2009 19:00 |
|
androo posted:Yeah, I know that's pretty dangerous, but it's working for now since only one function is even accessing them at once. Well, stop it. "It's working now" isn't really a good excuse to misuse globals, and just encourages bad design and adds to difficulty in debugging.
|
# ? May 20, 2009 19:01 |
|
Avenging Dentist posted:Really, you should just make your own class that has a better interface. One that works like I/O streams on the bit level, for instance. That's something I'm already on the hook for. We have I/O libraries we use for all manner of byte-aligned data, but this is a bit new. And yes, the larger ones are aligned, I edited it in when before I saw this post. I also hate that spec. Especially since this is something I'm not able able to charge hours against.
|
# ? May 20, 2009 19:04 |
|
Lurchington posted:these are all byte-aligned, so that's a bonus Yup, that's what I figured. The "easy" way is to pack the headers up using standard bitshifting (you'll have to do that at some level) and then insert them into your bytestream and then add all the other stuff. The "thorough" way would be something like this: code:
|
# ? May 20, 2009 19:06 |
|
Lurchington, you're talking about parsing a message format whose authors brag about the parser being written in 204,000 lines of Ada. Are you absolutely sure you cannot re-use some existing code here? EDIT: meant to address Lurchington rjmccall fucked around with this message at 19:18 on May 20, 2009 |
# ? May 20, 2009 19:08 |
|
Avenging Dentist posted:Thanks, you've given me a lot to think harder about. I'll look at how best to implement that info our existing I/O stream libraries.
|
# ? May 20, 2009 19:08 |
|
How do I send keystrokes to any application of my choosing without losing focus on the current application? I want to be able to have my program type in another window while I send out emails or whatever. Currently, I use the following code to simulate keystrokes: code:
|
# ? May 21, 2009 05:09 |
|
Jewdicator posted:How do I send keystrokes to any application of my choosing without losing focus on the current application? I want to be able to have my program type in another window while I send out emails or whatever. http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx (probably)
|
# ? May 21, 2009 05:15 |
|
Anyone know if there is a good free (or cheap) C++ decimal-arithmetic library that isn't restricted to non-commercial projects?
|
# ? May 21, 2009 07:02 |
|
GMP is under the LGPL.
|
# ? May 21, 2009 14:36 |
|
decNumber is another library, it's BSD licensed.
|
# ? May 22, 2009 03:10 |
|
awesmoe posted:I've got a 64 character string containing hex digits ("aaffb0b" etc) that I want to convert to a string of decimal digits. Can anyone suggest a way to do this without handcrafting my own brutal loop, full of horrible arithmetic mistakes? code:
code:
|
# ? May 22, 2009 03:50 |
|
Gxp posted:This seems to works fine. Encapsulate that baby in a function that takes in a string and returns ss.str() and you're good to go. code:
Avenging Dentist fucked around with this message at 04:07 on May 22, 2009 |
# ? May 22, 2009 03:58 |
|
Avenging Dentist posted:
Now, to be fair, awesmoe did specify "a string of decimal digits" rather than "the equivalent number in base 10" so having 0x10 convert to "1 0" instead of "16" is technically solving the problem. Well, except for the space.
|
# ? May 22, 2009 04:05 |
|
ShoulderDaemon posted:Now, to be fair, awesmoe did specify "a string of decimal digits" rather than "the equivalent number in base 10" so having 0x10 convert to "1 0" instead of "16" is technically solving the problem. Well, except for the space. Even so, using atoi to convert a single-digit number is just awful, especially when he special-cases [A,F]. Even if you were paranoid and thought you'd be working on a non-ASCII system, you could at least use strtol and supply the base.
|
# ? May 22, 2009 04:10 |
|
gently caress me I suck at BBCode.
shrughes fucked around with this message at 04:23 on May 22, 2009 |
# ? May 22, 2009 04:13 |
|
Avenging Dentist posted:(To preempt the "huh?", he's talking about base-conversion of numbers that don't fit in a primitive type, which is non-trivial unless your two bases are perfect powers of a common base (e.g. 8 = 23 and 16 = 24).
|
# ? May 22, 2009 04:19 |
|
shrughes posted:Your code turns the substrings "b1" and "1b" into the same thing. Do you really think that's what the other guy wanted? And your code is horrible and retarded; perhaps you should kill yourself. No, there's a space.
|
# ? May 22, 2009 04:21 |
|
Gxp posted:Yes, you are a cowboy, but thanks for the the attempt
|
# ? May 22, 2009 05:47 |
|
Is it pretty standard in school for assignments to be just a header file, with no explanation of any kind? Our last assignment in my C++ level two course is just confusing as all hell to me, because our teacher doesn't provide any documentation, other than the comments, and hasn't talked about it in class at all. Literally, other than what's in the file itself, his web site just said "Implement the member functions for the attached counter class. Upload your .cpp file and your .h file." Most of his commentary is not very helpful, if at all. I mean, things like: code:
Not to mention: code:
Here's the code in its entirety, if you're so inclined to read it. Is he as worthless as he seems to me, or am I just dumb and should shut up?
|
# ? May 22, 2009 08:43 |
|
Avenging Dentist posted:http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx (probably) Unless I missed something, "The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed." I'm trying to get it to send keys to an arbitrary window of my choosing. There doesn't seem to be a parameter to change that.
|
# ? May 22, 2009 08:51 |
|
|
# ? Jun 7, 2024 08:19 |
|
Anunnaki posted:Most of his commentary is not very helpful, if at all. I mean, things like: If its not clear, ask a tutor (TA? dono what you guys call em) and document with comments any assumptions you're making when you're not sure about something. Value, I think, refers to eg the number 123 stored as {3,2,1} that he mentions in the top comment.
|
# ? May 22, 2009 09:04 |