Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
awesmoe
Nov 30, 2005

Pillbug
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?

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

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.

awesmoe
Nov 30, 2005

Pillbug

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.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

Let's not forget system("pause");
Well, if you know of a better way to set breakpoints I'd love to hear it :colbert:

ehnus
Apr 16, 2003

Now you're thinking with portals!

Dijkstracula posted:

Well, if you know of a better way to set breakpoints I'd love to hear it :colbert:

MSVC:

DebugBreak() or __debugbreak() or __asm int 3

GCC:

asm volatile ("int3")

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


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
}
Where I left the **** is causing my problem: according to the compiler errors I'm getting, it's trying to instantiate the templated B::doThings instead of using the specialization. What do I need to do to get the compiler to use the specialized form of B::doThings instead of instantiating a new templated form?

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
The explicit specializations need to be in the header, too. Until C++0x.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


:doh:

Thanks. The rules for using templates are so flighty to me sometimes :(

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
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++.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Why would you even fully specialize a function template? That's what overloads are for. :psyduck:

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

Lurchington
Jan 2, 2003

Forums Dragoon
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

fart barterer
Aug 24, 2006


David Byrne - Like Humans Do (Radio Edit).mp3

Avenging Dentist posted:

code:
        int main()
      {
    while(true)
  {
printf("Butts\n");
  }
      }

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:
#include <iostream>
// maybe windows.h when I'm not on this god-awful laptop
using namespace std;
int debug = 1;

//int for the x/y, then x/y resolution
int x, y;
const int xres = 4, yres = 4;
char map[xres][yres];



// Function for incrementing along X and Y
int incMap(){
              x++;
              if(x>xres){
                         y++;
                         x=0;
                         //The return is for printScreen to know when to endl
                         return(1);
                         }
     }



//Function for writing one slot
void writeMap(int a, int b, char c){
     map[a][b]=c;
     if(debug==1) cout << "Written " << c << " to " << a << "," << b << "." << endl;
     }



//Function for filling whole map.
void fillMap(int toFill){
     x=0;
     y=0;
     while(x<=xres && y<=yres){
                   writeMap(x,y,toFill);
                   incMap();
                   }
     }


     
/*-------------------------
/ Function for outputting map. For now will cout for every char,
/ in future maybe cout whole thing at once? Stick all the chars with
/ endl or ASCII equiv at end in char "screen" as [xr+1][yr]? What's the
/ best way of "streaming" that kind of data into one cout ?
-------------------------*/
void printMap(){
     x=0;
     y=0;
     while(y<=yres){
                   cout << map[x][y];
                   //And, if incmap has returned 1 on y++, make an endl
                   if(incMap() == 1) cout << endl;
                   }
     if(debug==1) cout << "printMap has been run!" << endl;
     }


     
main(){
       
       fillMap('0');
       writeMap(0,2,'1');
       printMap();
       
       }
So within main I fill the "map" with 0, write '1' to the middle of the top row, and print the "map". The output lists:

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!

:cry: Why is that loving 1 at 4,1? The problem has to be at the printMap function.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

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.

fart barterer
Aug 24, 2006


David Byrne - Like Humans Do (Radio Edit).mp3

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.

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.

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 :byodood:

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

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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.

If you declare an array, the number you specify is its size. I will leave it as an exercise to the reader to figure out what that implies.

Also, please stop using so many globals (especially globals with single-letter names). Lexical scoping is there for a reason.

Lurchington
Jan 2, 2003

Forums Dragoon

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).

Also, why would you use a dynamic bitset on statically-sized data?

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)
  • 440 bits for UNIT NAME
  • 1024 bits for CRYPTOGRAPHIC INITIALIZATION
  • 8192 bits for AUTHENTICATION DATA
  • 16384 bits for KEY TOKEN :psyduck:

these are all byte-aligned, so that's a bonus

Lurchington fucked around with this message at 19:00 on May 20, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Lurchington posted:

:words:

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.)

fart barterer
Aug 24, 2006


David Byrne - Like Humans Do (Radio Edit).mp3

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.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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.

Lurchington
Jan 2, 2003

Forums Dragoon

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.

(Also, I rather doubt that name fields are unaligned, but I'm drat sure not going to read the spec.)

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.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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:
class bit_stream
{
public:
    void push(int data,int length);
    void push(char *data,int length);
private:
    int current_bit_pos;
    char *buffer;
};

// ...

bit_stream data;
data.push(flag_1, 4);
data.push(flag_2, 3);
data.push(name, 50);

send(data);

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
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

Lurchington
Jan 2, 2003

Forums Dragoon

Avenging Dentist posted:

:words:

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.

Jewdicator
Oct 22, 2006
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:
#include <windows.h>

void TypeStr (char *lpszString)
{
  char cChar;
  while((cChar=*lpszString++)) // loops through chars
  {
    short vk=VkKeyScan(cChar); // keycode of char
    if((vk>>8)&1){keybd_event(VK_LSHIFT,0,0,0);} // hold shift if necessary
    keybd_event((unsigned char)vk,0,0,0); // key in
    keybd_event((unsigned char)vk,0,KEYEVENTF_KEYUP,0); // key out
    if((vk>>8)&1){keybd_event(VK_LSHIFT,0,KEYEVENTF_KEYUP,0);} // release shift if necessary
  }
}

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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)

Chuu
Sep 11, 2004

Grimey Drawer
Anyone know if there is a good free (or cheap) C++ decimal-arithmetic library that isn't restricted to non-commercial projects?

Lexical Unit
Sep 16, 2003

GMP is under the LGPL.

79
Dec 25, 2005

Melancholy.
decNumber is another library, it's BSD licensed.

Gxp
Jan 23, 2004

Son I'm afraid this is a stereo disaster.

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:
string hexString("0123456789abcdeffedcba98765432100123456789abcdeffedcba9876543210");

int myNumbers[64];

for(unsigned int i = 0; i < hexString.length(); i++)
{
    if(hexString[i] >= 48 && hexString[i] <= 57) //ASCII characters are in the range of normal numbers.
    {
        myNumbers[i] = atoi(hexString.substr(i, 1).c_str()); //ASCII to integer conversion on a single character.
    }
    else //ASCII characters are outside printable numbers.
    {
        switch(hexString[i])
        {
            case 'a': case 'A':
                myNumbers[i] = 10;
            break;

            case 'b': case 'B':
                myNumbers[i] = 11;
            break;

            case 'c': case 'C':
                myNumbers[i] = 12;
            break;

            case 'd': case 'D':
                myNumbers[i] = 13;
            break;

            case 'e': case 'E':
                myNumbers[i] = 14;
            break;

            case 'f': case 'F':
                myNumbers[i] = 15;
            break;

            default:
            myNumbers[i] = 0;
        }
    }
}
I know that can be done in a less code but it's really straightforward and you should be able to decipher what's going on if you weren't sure how to code this before. That just breaks it into an array of ints, though if you want it in a string (whish i believe you said you do) tack this on the end:

code:
    stringstream ss;
    for(unsigned int i = 0; i < 64; i++)
    {
        ss << myNumbers[i] << " ";
    }
    cout << ss.str() << endl;
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.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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:
for(;;)
    cout << "No" << endl;
(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).

Avenging Dentist fucked around with this message at 04:07 on May 22, 2009

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Avenging Dentist posted:

code:
for(;;)
    cout << "No" << endl;

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.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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.

shrughes
Oct 11, 2008

(call/cc call/cc)
gently caress me I suck at BBCode.

shrughes fucked around with this message at 04:23 on May 22, 2009

Gxp
Jan 23, 2004

Son I'm afraid this is a stereo disaster.

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).
:clint:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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.

awesmoe
Nov 30, 2005

Pillbug

Gxp posted:

:clint:

Yes, you are a cowboy, but thanks for the the attempt :)

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
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:
	void print(ostream & out, bool moreInfo = false) const; 
	// print the value to output stream. If moreInfo is true then also print numDigits and arraySize
	// Be sure that ZERO prints correctly.
	// your operator<<() function should call print().
I have no idea what "value" this is talking about.

Not to mention:
code:
    friend ostream& operator<<(ostream&, const counter&);
    // stream insertion operator
Yes, thank you, I know what that is. What am I supposed to use it for?

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?

Jewdicator
Oct 22, 2006

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.

Adbot
ADBOT LOVES YOU

awesmoe
Nov 30, 2005

Pillbug

Anunnaki posted:

Most of his commentary is not very helpful, if at all. I mean, things like:
code:
	void print(ostream & out, bool moreInfo = false) const; 
	// print the value to output stream. If moreInfo is true then also print numDigits and arraySize
	// Be sure that ZERO prints correctly.
	// your operator<<() function should call print().
I have no idea what "value" this is talking about.
...
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?

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply