java equivalents for c operations

Discussion in 'Java' started by jimgardener, Jun 21, 2008.

  1. jimgardener

    jimgardener Guest

    hi
    i have some doubts as to how some c types can be replaced in java
    1).
    in c i use a const unsigned char fonts [][16] ={
    {0xc0,0x7c,0x06},
    {0x38,0x76,0xdc}
    }

    if i was to reproduce this array in java what data type should i use?
    should i use
    class MyFonts{
    public static char fonts[][] ={
    {0xc0,0x7c,0x06},
    {0x38,0x76,0xdc}
    };
    }

    2).i want to read one line at a time from a textfile .In c i would do
    this by
    FILE *inputfile=infile = fopen("somefile.txt","r")
    unsigned char *text_line = (unsigned char *) malloc (SomeSIZE) ;
    while (fgets ((char *)text_line,LINESIZE,inputfile)){
    ....
    }

    how can i do this in java? should i use FileReader or some other?
     
    jimgardener, Jun 21, 2008
    #1
    1. Advertising

  2. jimgardener wrote:
    > hi
    > i have some doubts as to how some c types can be replaced in java
    > 1).
    > in c i use a const unsigned char fonts [][16] ={
    > {0xc0,0x7c,0x06},
    > {0x38,0x76,0xdc}
    > }
    >
    > if i was to reproduce this array in java what data type should i use?
    > should i use
    > class MyFonts{
    > public static char fonts[][] ={
    > {0xc0,0x7c,0x06},
    > {0x38,0x76,0xdc}
    > };
    > }


    Maybe you want add the 'final' keyword:
    public static final char[][] fonts ={
    {0xc0,0x7c,0x06},
    {0x38,0x76,0xdc}
    };

    'final' roughly means "assignable only one time", is something similar
    to C/C++ const keyword, but is different for some peculiar aspects.

    >
    > 2).i want to read one line at a time from a textfile .In c i would do
    > this by
    > FILE *inputfile=infile = fopen("somefile.txt","r")
    > unsigned char *text_line = (unsigned char *) malloc (SomeSIZE) ;
    > while (fgets ((char *)text_line,LINESIZE,inputfile)){
    > ...
    > }
    >
    > how can i do this in java? should i use FileReader or some other?


    from http://www.javapractices.com/topic/TopicAction.do?Id=42

    BufferedReader input = new BufferedReader(new FileReader(filename));

    String line;
    while (( line = input.readLine()) != null){
    ...
    }



    --
    Andrea Francia
    http://andreafrancia.blogspot.com/2008/06/relazioni-molti-molti-con-jpa.html
     
    Andrea Francia, Jun 21, 2008
    #2
    1. Advertising

  3. jimgardener

    Roedy Green Guest

    On Sat, 21 Jun 2008 00:17:43 -0700 (PDT), jimgardener
    <> wrote, quoted or indirectly quoted someone who
    said :

    >i have some doubts as to how some c types can be replaced in java
    >1).
    >in c i use a const unsigned char fonts [][16] ={
    >{0xc0,0x7c,0x06},
    >{0x38,0x76,0xdc}
    >}


    that would be handled by byte[][] in Java.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 21, 2008
    #3
  4. jimgardener

    Roedy Green Guest

    On Sat, 21 Jun 2008 00:17:43 -0700 (PDT), jimgardener
    <> wrote, quoted or indirectly quoted someone who
    said :

    >if i was to reproduce this array in java what data type should i use?
    >should i use
    >class MyFonts{
    >public static char fonts[][] ={
    > {0xc0,0x7c,0x06},
    > {0x38,0x76,0xdc}
    > };
    >}


    if you meant unsigned 8-bit quantities, use byte[][] and see
    http://mindprod.com/jgloss/unsigned.html for how to fudge the
    unsignedness.

    If you meant characters (16-bit unicode), use char[][].
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 21, 2008
    #4
  5. jimgardener

    Roedy Green Guest

    On Sat, 21 Jun 2008 00:17:43 -0700 (PDT), jimgardener
    <> wrote, quoted or indirectly quoted someone who
    said :

    >2).i want to read one line at a time from a textfile .In c i would do
    >this by
    >FILE *inputfile=infile = fopen("somefile.txt","r")
    >unsigned char *text_line = (unsigned char *) malloc (SomeSIZE) ;
    >while (fgets ((char *)text_line,LINESIZE,inputfile)){


    see http://mindprod.com/applet/fileio.html
    to generate you sample code.

    Java is much more encoding-aware than C. C pretty well just assumes
    the file is 8-bit in local encoding. Java does not. Java gives you
    many more options which lets you exchange files with people in other
    countries.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 21, 2008
    #5
  6. jimgardener

    Mark Space Guest

    jimgardener wrote:
    > hi
    > i have some doubts as to how some c types can be replaced in java
    > 1).
    > in c i use a const unsigned char fonts [][16] ={
    > {0xc0,0x7c,0x06},
    > {0x38,0x76,0xdc}
    > }
    >
    > if i was to reproduce this array in java what data type should i use?
    > should i use
    > class MyFonts{
    > public static char fonts[][] ={
    > {0xc0,0x7c,0x06},
    > {0x38,0x76,0xdc}
    > };
    > }


    This works ok, but be aware that you don't have 16 elements here in the
    first array dimension, unlike the C example.

    public static final char fonts[][] = new char[16][];

    This might be a bit more what you intended, although it's a bit harder
    to initialize.

    private static final char DEFAULT_FONTS[][] =
    {
    {0xc0,0x7c,0x06},
    {0x38,0x76,0xdc}
    };
    for( int i = 0; i < DEFAULT_FONTS.length; i++ )
    {
    for( int j = 0; j < DEFAULT_FONTS.lentgh; j++ )
    {
    fonts[j] = DEFAULT_FONTS[j];
    }
    }

    (Not compiled.)

    >
    > 2).i want to read one line at a time from a textfile .In c i would do
    > this by
    > FILE *inputfile=infile = fopen("somefile.txt","r")
    > unsigned char *text_line = (unsigned char *) malloc (SomeSIZE) ;
    > while (fgets ((char *)text_line,LINESIZE,inputfile)){
    > ...
    > }
    >
    > how can i do this in java? should i use FileReader or some other?


    I didn't see anyone else answer this. All of the *Reader classes can be
    wrapped in a BufferedReader to get a readLine() method. That's the best
    way I think.

    <http://java.sun.com/docs/books/tutorial/essential/io/charstreams.html>

    <http://java.sun.com/docs/books/tutorial/essential/io/buffers.html>

    <http://java.sun.com/javase/6/docs/api/java/io/BufferedReader.html#readLine()>

    Note: Not a Rickroll. ;-)
     
    Mark Space, Jun 21, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Peters

    Special character to &abc equivalents

    Colin Peters, May 7, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    2,333
    =?Utf-8?B?UGF1bCBQYXJraW5zb24=?=
    May 9, 2005
  2. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,321
    robert
    Feb 11, 2006
  3. Jeff Thies

    CSS equivalents for attributes

    Jeff Thies, Jul 13, 2004, in forum: HTML
    Replies:
    36
    Views:
    1,325
    Mark Parnell
    Jul 15, 2004
  4. Allen F.
    Replies:
    3
    Views:
    503
    Nathaniel L. Walker
    Feb 9, 2005
  5. Code_Dark

    New to Python; Command equivalents

    Code_Dark, Nov 5, 2003, in forum: Python
    Replies:
    2
    Views:
    456
    Dang Griffith
    Nov 5, 2003
Loading...

Share This Page