Results 1 to 3 of 3

Thread: Converting String Array into int array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Converting String Array into int array

    Hi all,
    I have a string array
    Receivedbyte[0]=5A
    Receivedbyte[1]=3A
    Receivedbyte[2]=7A
    Receivedbyte[3]=60


    I want to treat them as hex numbers and xor each of the value by 0x20.
    so I want my data to be
    0x5A ^0x20 in the 0th location. and so on.

    I Tried the following , but an error comes which says,input string is not in correct format.
    uint temp = Convert.ToUInt32(Receivedbyte[1]);
    temp = temp ^ 0x20;
    Receivedbyte[0] = Convert.ToString(temp);
    Receivedbyte[1] = Receivedbyte[2];
    Receivedbyte[2] = Receivedbyte[3];

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Converting String Array into int array

    Unless you have a specific reason to use an unsigned number, I'd suggest calling Convert.ToInt32 rather than .ToUInt32. As for the conversion, if you just pass the String then it is assumed that it is a decimal number. If you want it to be parsed as hexadecimal then you need to use the overload that takes the base (2, 8, 10 or 16) as a second argument. Obviously you need to pass 16. If you want to convert back to a hexadecimal String then you need to pass the base to Convert.ToString as well.

    By the way, if your values are all bytes then you probably ought to call Convert.ToByte. It has an overload that takes a base too.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Converting String Array into int array

    yea..adding base to the Convert.ToString solved my problem.
    What should I do if I have Receivedbyte[0]=122
    and I want to convert this 122 into hex and put Receivedbyte[0]=7A.
    I want to treat it as decimal and then convert it into hex.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width