Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


BASICFreak

Member Since 07 May 2006
Offline Last Active Jun 15 2014 10:11 PM

Posts I've Made

In Topic: Returning a Char Array from function in C

27 September 2013 - 12:18 PM

 

If you can suggest an easier and\or better way of doing this please point me in the right direction. I'm only on my second build here and I know I have at least two more before I get all the features I want such as fragmentation (yea I know breaking files into parts is a bad idea [Microsoft] but I want the ability if it is needed)


Sure;
  • Always* keep the null-terminator in mind when dealing with strings: ret isn't large enough to hold 16 characters and the null terminator; sprintf is writing the null terminator in memory that doesn't belong to ret!
  • Always* look for ways to reduce and refactor duplicate code: Notice the format parameter for sprintf in BinHex is twice that of bytes? You can reduce this whole case statement by adding another level of indirection; try using another sprintf to prepare the format parameter. The switch case in HexBin can also be reduced to a loop.
  • Always* enforce parameter constraints. The bytes parameter can only be between 1 and 8, but you have a special behavior defined when it is not. It may be better to throw a fatal error in this case by using assert.
*Be wary of statements such as "always do this" or "never do that", it's "always" situational and may not fit every use case.

Here's the reduced code:
Spoiler


Hope this helps!

EDIT: Formatting/editing

 

That helped a lot, thanks. I did have to edit your code to the following BinHex was slightly wrong:

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include "MyFS.h"


// 17 characters, to make room for the null-terminator
void BinHex(char Bin[17], char ret[17], int bytes)
{
    // Enforce constraints
    assert( 1 <= bytes && bytes <= 8 );


    // Here, we use pointers to read and write to and from ret and Bin respectively
    char* dst = &ret[0];
    char* src = &Bin[0];


    // Equivalent to "for ( ; bytes > 0; bytes-- )"
    while ( bytes-- )
    {
        // sprintf returns the number of characters written, so can use this
        // to our advantage and move the write pointer 2 characters for 
        // every 2 characters written
        dst += sprintf(dst,"%02x",(unsigned int)(src[bytes]) );   // "src[bytes]" read a charater from src and moves it to the previous character
    }
}


void DecHex(unsigned int Dec, char ret[17], int bytes)
{
    assert( 1 <= bytes && bytes <= 8 );


    char fmt[5];
    // "%%" -> "%"
    // "%02i" -> zero-padded bytes*2, width of 2
    // "x" -> "x"
    sprintf(fmt,"%%%02ix",bytes*2); // When bytes=4 and Dec=65534, "%%%02ix" -> "%08x"
    sprintf(ret,fmt,Dec);  // "%08x" -> 0000FFFE
}


unsigned int HexDec(char* Hx)
{
    unsigned int ret;
    ret = (unsigned int)strtol(Hx, NULL, 16);
    return ret;
}

But it defiantly helped with readability 

 

Thanks,

 

Brian T Hoover


In Topic: Returning a Char Array from function in C

23 September 2013 - 05:03 PM

 

ಠ_ಠ

 

LOL!

 

How the hell did you do that???

 

Anyway, i dont want to be mean, we all started like this, me included, but this should go in the coding horror section smile.png

 

Your code is flawed in so many way, im surprised it even work.

 

All this code could be baked down using loops to something like 8 lines of code...

 

May I suggest working on something more easier first, like strings manipulation, and integer to strings conversion and vice versa, and loops.

 

first Alt +0CA0 in windows or &#3232; HTML code or "\u0CA0" C(++) and Java for the ಠ character

 

Yes, I know my code is way off but it compiles and links with no errors or warnings lol and works.

For me I am not used to standard libraries - or protected mode, you should see how bad the actual code looks, you just saw me trying to convert data and my code above was wrong too, BinHex was completely backwards

 

If you can suggest an easier and\or better way of doing this please point me in the right direction.  I'm only on my second build here and I know I have at least two more before I get all the features I want such as fragmentation (yea I know breaking files into parts is a bad idea [Microsoft] but I want the ability if it is needed)

 

FYI:

I am mostly doing this because I was having such a hard time trying to read the FAT12 File System and I wanted to expand my knowledge more and the best way I could think of was make my own so that I can get the in's and out's using the FAT File System as my base and changing very little.

I have one more function to get working and V 0.2 shall be done and if anyone wants it just post and I'll release it.


In Topic: Returning a Char Array from function in C

21 September 2013 - 07:07 PM

Because someone else posted in response, I'll show you my solution:

 

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


unsigned int HexDec(char* Hx)
{
    unsigned int ret;
    ret = (unsigned int)strtol(Hx, NULL, 16);
    return ret;
}
void DecHex(unsigned int Dec, char ret[16], int bytes)        //1-8 bytes
{
    switch(bytes)
    {
        case 1:
            sprintf(ret,"%02x", Dec);
            break;
        case 2:
            sprintf(ret,"%04x", Dec);
            break;
        case 3:
            sprintf(ret,"%06x", Dec);
            break;
        case 4:
            sprintf(ret,"%08x", Dec);
            break;
        case 5:
            sprintf(ret,"%10x", Dec);
            break;
        case 6:
            sprintf(ret,"%12x", Dec);
            break;
        case 7:
            sprintf(ret,"%14x", Dec);
            break;
        case 8:
            sprintf(ret,"%16x", Dec);
            break;
        default:
            sprintf(ret,"%08x", Dec);
            break;
    }
    return;
}
void BinHex(char Bin[16], char ret[16], int bytes)        //1-8 Bytes
{
    //int bytes = sizeof(Bin);
    switch(bytes)
    {
        case 1:
            sprintf(ret, "%02x", (unsigned int) Bin[0] & 0xFF);
            break;
        case 2:
            sprintf(ret, "%02x%02x", (unsigned int) Bin[0] & 0xFF, (unsigned int) Bin[1] & 0xFF);
            break;
        case 3:
            sprintf(ret, "%02x%02x%02x", (unsigned int) Bin[0] & 0xFF, (unsigned int) Bin[1] & 0xFF, (unsigned int) Bin[2] & 0xFF);
            break;
        case 4:
            sprintf(ret, "%02x%02x%02x%02x", (unsigned int) Bin[0] & 0xFF, (unsigned int) Bin[1] & 0xFF, (unsigned int) Bin[2] & 0xFF, (unsigned int) Bin[3] & 0xFF);
            break;
        case 5:
            sprintf(ret, "%02x%02x%02x%02x%02x", (unsigned int) Bin[0] & 0xFF, (unsigned int) Bin[1] & 0xFF, (unsigned int) Bin[2] & 0xFF, (unsigned int) Bin[3] & 0xFF, (unsigned int) Bin[4] & 0xFF);
            break;
        case 6:
            sprintf(ret, "%02x%02x%02x%02x%02x%02x", (unsigned int) Bin[0] & 0xFF, (unsigned int) Bin[1] & 0xFF, (unsigned int) Bin[2] & 0xFF, (unsigned int) Bin[3] & 0xFF, (unsigned int) Bin[4] & 0xFF, (unsigned int) Bin[5] & 0xFF);
            break;
        case 7:
            sprintf(ret, "%02x%02x%02x%02x%02x%02x%02x", (unsigned int) Bin[0] & 0xFF, (unsigned int) Bin[1] & 0xFF, (unsigned int) Bin[2] & 0xFF, (unsigned int) Bin[3] & 0xFF, (unsigned int) Bin[4] & 0xFF, (unsigned int) Bin[5] & 0xFF, (unsigned int) Bin[6] & 0xFF);
            break;
        case 8:
            sprintf(ret, "%02x%02x%02x%02x%02x%02x%02x%02x", (unsigned int) Bin[0] & 0xFF, (unsigned int) Bin[1] & 0xFF, (unsigned int) Bin[2] & 0xFF, (unsigned int) Bin[3] & 0xFF, (unsigned int) Bin[4] & 0xFF, (unsigned int) Bin[5] & 0xFF, (unsigned int) Bin[6] & 0xFF, (unsigned int) Bin[7] & 0xFF);
            break;
        default:
            sprintf(ret, "%02x%02x%02x%02x", (unsigned int) Bin[0] & 0xFF, (unsigned int) Bin[1] & 0xFF, (unsigned int) Bin[2] & 0xFF, (unsigned int) Bin[3] & 0xFF);
            break;
    }
    return;
}

works flawlessly ;) (for my situation at the very least)


In Topic: Returning a Char Array from function in C

20 September 2013 - 09:20 PM

so, my hackish way got around not being able to return arrays and changed all my char functions to just a function no return and added an argument of the variable to modify which is an array.

 

and FYI the 4-Bytes was referring to how many bytes I hard coded the section to process, remember this is reading an entire disk with a custom File System and I'm using 32 bit addressing which breaks down to 4 bytes, actually my code can handle 1-8 bytes, but I subtracted a lot of it to save on reading time of useless information

 

1 sector = 512 bytes

1 cluster = 19 sectors

 

sector 1 = boot sector

sector 2-19 = File Table

rest = File Data


In Topic: MySQL Database Structure and Data Lister

06 July 2013 - 03:26 AM

wow I can't believe I missed that for this long...

 

Thanks for the help

BASICFreak


PARTNERS